]>
git.proxmox.com Git - mirror_ovs.git/blob - tests/test-util.c
2 * Copyright (c) 2011, 2012 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include "byte-order.h"
29 check_log_2_floor(uint32_t x
, int n
)
31 if (log_2_floor(x
) != n
) {
32 fprintf(stderr
, "log_2_floor(%"PRIu32
") is %d but should be %d\n",
33 x
, log_2_floor(x
), n
);
39 check_ctz(uint32_t x
, int n
)
42 fprintf(stderr
, "ctz(%"PRIu32
") is %d but should be %d\n",
48 /* Returns the sum of the squares of the first 'n' positive integers. */
52 return n
* (n
+ 1) * (2 * n
+ 1) / 6;
56 check_bitwise_copy(void)
64 for (n_bits
= 0; n_bits
<= 64; n_bits
++) {
65 for (src_ofs
= 0; src_ofs
< 64 - n_bits
; src_ofs
++) {
66 for (dst_ofs
= 0; dst_ofs
< 64 - n_bits
; dst_ofs
++) {
67 ovs_be64 src
= htonll(random_uint64());
68 ovs_be64 dst
= htonll(random_uint64());
69 ovs_be64 orig_dst
= dst
;
75 uint64_t mask
= (UINT64_C(1) << n_bits
) - 1;
76 expect
= orig_dst
& ~htonll(mask
<< dst_ofs
);
77 expect
|= htonll(((ntohll(src
) >> src_ofs
) & mask
)
81 bitwise_copy(&src
, sizeof src
, src_ofs
,
82 &dst
, sizeof dst
, dst_ofs
,
85 fprintf(stderr
,"copy_bits(0x%016"PRIx64
",8,%d, "
86 "0x%016"PRIx64
",8,%d, %d) yielded 0x%016"PRIx64
" "
87 "instead of the expected 0x%016"PRIx64
"\n",
89 ntohll(orig_dst
), dst_ofs
,
91 ntohll(dst
), ntohll(expect
));
100 if (n_loops
!= sum_of_squares(64)) {
106 check_bitwise_zero(void)
108 unsigned int n_loops
;
113 for (n_bits
= 0; n_bits
<= 64; n_bits
++) {
114 for (dst_ofs
= 0; dst_ofs
< 64 - n_bits
; dst_ofs
++) {
115 ovs_be64 dst
= htonll(random_uint64());
116 ovs_be64 orig_dst
= dst
;
122 uint64_t mask
= (UINT64_C(1) << n_bits
) - 1;
123 expect
= orig_dst
& ~htonll(mask
<< dst_ofs
);
126 bitwise_zero(&dst
, sizeof dst
, dst_ofs
, n_bits
);
128 fprintf(stderr
,"bitwise_zero(0x%016"PRIx64
",8,%d, %d) "
129 "yielded 0x%016"PRIx64
" "
130 "instead of the expected 0x%016"PRIx64
"\n",
131 ntohll(orig_dst
), dst_ofs
,
133 ntohll(dst
), ntohll(expect
));
141 if (n_loops
!= 64 * (64 + 1) / 2) {
151 for (n
= 0; n
< 32; n
++) {
152 /* Check minimum x such that f(x) == n. */
153 check_log_2_floor(1 << n
, n
);
154 check_ctz(1 << n
, n
);
156 /* Check maximum x such that f(x) == n. */
157 check_log_2_floor((1 << n
) | ((1 << n
) - 1), n
);
158 check_ctz(UINT32_MAX
<< n
, n
);
160 /* Check a random value in the middle. */
161 check_log_2_floor((random_uint32() & ((1 << n
) - 1)) | (1 << n
), n
);
162 check_ctz((random_uint32() | 1) << n
, n
);
166 * (log_2_floor(0) is undefined.) */
169 check_bitwise_copy();
171 check_bitwise_zero();