~james-page/ubuntu/saucy/openvswitch/1.12-snapshot

« back to all changes in this revision

Viewing changes to tests/test-util.c

  • Committer: James Page
  • Date: 2013-08-21 10:16:57 UTC
  • mfrom: (1.1.20)
  • Revision ID: james.page@canonical.com-20130821101657-3o0z0qeiv5zkwlzi
New upstream snapshot

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (c) 2011, 2012 Nicira, Inc.
 
2
 * Copyright (c) 2011, 2012, 2013 Nicira, Inc.
3
3
 *
4
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
5
 * you may not use this file except in compliance with the License.
90
90
    check_ctz(0, 32);
91
91
}
92
92
 
 
93
/* Returns a random number in the range 'min'...'max' inclusive. */
 
94
static uint32_t
 
95
random_in_range(uint32_t min, uint32_t max)
 
96
{
 
97
    return min == max ? min : min + random_range(max - min + 1);
 
98
}
 
99
 
 
100
static void
 
101
check_rup2(uint32_t x, int n)
 
102
{
 
103
    uint32_t rup2 = ROUND_UP_POW2(x);
 
104
    if (rup2 != n) {
 
105
        fprintf(stderr, "ROUND_UP_POW2(%#"PRIx32") is %#"PRIx32" "
 
106
                "but should be %#"PRIx32"\n", x, rup2, n);
 
107
        abort();
 
108
    }
 
109
}
 
110
 
 
111
static void
 
112
test_round_up_pow2(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 
113
{
 
114
    int n;
 
115
 
 
116
    for (n = 0; n < 32; n++) {
 
117
        /* Min, max value for which ROUND_UP_POW2 should yield (1 << n). */
 
118
        uint32_t min = ((1u << n) >> 1) + 1;
 
119
        uint32_t max = 1u << n;
 
120
 
 
121
        check_rup2(min, 1u << n);
 
122
        check_rup2(max, 1u << n);
 
123
        check_rup2(random_in_range(min, max), 1u << n);
 
124
    }
 
125
    check_rup2(0, 0);
 
126
}
 
127
 
 
128
static void
 
129
check_rdp2(uint32_t x, int n)
 
130
{
 
131
    uint32_t rdp2 = ROUND_DOWN_POW2(x);
 
132
    if (rdp2 != n) {
 
133
        fprintf(stderr, "ROUND_DOWN_POW2(%#"PRIx32") is %#"PRIx32" "
 
134
                "but should be %#"PRIx32"\n", x, rdp2, n);
 
135
        abort();
 
136
    }
 
137
}
 
138
 
 
139
static void
 
140
test_round_down_pow2(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 
141
{
 
142
    int n;
 
143
 
 
144
    for (n = 0; n < 32; n++) {
 
145
        /* Min, max value for which ROUND_DOWN_POW2 should yield (1 << n). */
 
146
        uint32_t min = 1u << n;
 
147
        uint32_t max = ((1u << n) << 1) - 1;
 
148
 
 
149
        check_rdp2(min, 1u << n);
 
150
        check_rdp2(max, 1u << n);
 
151
        check_rdp2(random_in_range(min, max), 1u << n);
 
152
    }
 
153
    check_rdp2(0, 0);
 
154
}
 
155
 
93
156
static void
94
157
shuffle(unsigned int *p, size_t n)
95
158
{
96
159
    for (; n > 1; n--, p++) {
97
 
        unsigned int *q = &p[rand() % n];
 
160
        unsigned int *q = &p[random_range(n)];
98
161
        unsigned int tmp = *p;
99
162
        *p = *q;
100
163
        *q = tmp;
297
360
 
298
361
            /* Change a random 0-bit into a 1-bit. */
299
362
            do {
300
 
                bit = htonll(UINT64_C(1) << (random_uint32() % 64));
 
363
                bit = htonll(UINT64_C(1) << (random_range(64)));
301
364
            } while (x & bit);
302
365
            x |= bit;
303
366
 
346
409
 
347
410
static const struct command commands[] = {
348
411
    {"ctz", 0, 0, test_ctz},
 
412
    {"round_up_pow2", 0, 0, test_round_up_pow2},
 
413
    {"round_down_pow2", 0, 0, test_round_down_pow2},
349
414
    {"popcount", 0, 0, test_popcount},
350
415
    {"log_2_floor", 0, 0, test_log_2_floor},
351
416
    {"bitwise_copy", 0, 0, test_bitwise_copy},
363
428
    enum {
364
429
        VLOG_OPTION_ENUMS
365
430
    };
366
 
    static struct option long_options[] = {
 
431
    static const struct option long_options[] = {
367
432
        VLOG_LONG_OPTIONS,
368
433
        {NULL, 0, NULL, 0},
369
434
    };