meta-flow: Correctly set destination MAC in mf_set_flow_value().
[sliver-openvswitch.git] / tests / test-util.c
1 /*
2  * Copyright (c) 2011 Nicira Networks.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18
19 #include <inttypes.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include "random.h"
25 #include "util.h"
26
27 static void
28 check_log_2_floor(uint32_t x, int n)
29 {
30     if (log_2_floor(x) != n) {
31         fprintf(stderr, "log_2_floor(%"PRIu32") is %d but should be %d\n",
32                 x, log_2_floor(x), n);
33         abort();
34     }
35 }
36
37 static void
38 check_ctz(uint32_t x, int n)
39 {
40     if (ctz(x) != n) {
41         fprintf(stderr, "ctz(%"PRIu32") is %d but should be %d\n",
42                 x, ctz(x), n);
43         abort();
44     }
45 }
46
47 int
48 main(void)
49 {
50     int n;
51
52     for (n = 0; n < 32; n++) {
53         /* Check minimum x such that f(x) == n. */
54         check_log_2_floor(1 << n, n);
55         check_ctz(1 << n, n);
56
57         /* Check maximum x such that f(x) == n. */
58         check_log_2_floor((1 << n) | ((1 << n) - 1), n);
59         check_ctz(UINT32_MAX << n, n);
60
61         /* Check a random value in the middle. */
62         check_log_2_floor((random_uint32() & ((1 << n) - 1)) | (1 << n), n);
63         check_ctz((random_uint32() | 1) << n, n);
64     }
65
66     /* Check ctz(0).
67      * (log_2_floor(0) is undefined.) */
68     check_ctz(0, 32);
69
70     return 0;
71 }