1 /*
2  * Copyright © 2018 Intel Corporation
3  * Copyright © 2019 Valve Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #undef NDEBUG
26 
27 #include <gtest/gtest.h>
28 #include "util/fast_urem_by_const.h"
29 
30 #define RAND_TEST_ITERATIONS 100000
31 
32 static uint32_t
rand_uint(unsigned min)33 rand_uint(unsigned min)
34 {
35    /* Make sure we get some small and large numbers and powers of two every
36     * once in a while
37     */
38    int k = rand() % 64;
39    if (k == 17) {
40       return min + (rand() % 16);
41    } else if (k == 42) {
42       return UINT32_MAX - (rand() % 16);
43    } else if (k == 9) {
44       uint32_t r;
45       do {
46          r = 1ull << (rand() % 32);
47       } while (r < min);
48       return r;
49    }
50 
51    if (min == 0) {
52       uint32_t r = 0;
53       for (unsigned i = 0; i < 4; i++)
54          r |= ((uint32_t)rand() & 0xf) << i * 8;
55       return r >> (31 - (rand() % 32));
56    } else {
57       uint64_t r;
58       do {
59          r = rand_uint(0);
60       } while (r < min);
61       return r;
62    }
63 }
64 
65 static void
test_case(uint32_t n,uint32_t d)66 test_case(uint32_t n, uint32_t d)
67 {
68    assert(d >= 1);
69    uint64_t magic = REMAINDER_MAGIC(d);
70    /* Note: there's already an assert inside util_fast_urem32(), so the
71     * EXPECT_EQ is only here to make sure the test fails with a wrong result
72     * even if asserts are disabled. Ideally we could disable the assert just
73     * for the test to get a better error message, but that doesn't seem too
74     * easy.
75     */
76    EXPECT_EQ(util_fast_urem32(n, d, magic), n % d);
77 }
78 
TEST(fast_urem_by_const,random)79 TEST(fast_urem_by_const, random)
80 {
81    for (unsigned i = 0; i < RAND_TEST_ITERATIONS; i++) {
82       uint64_t n = rand_uint(0);
83       uint64_t d = rand_uint(1);
84       test_case(n, d);
85    }
86 }
87 
TEST(fast_urem_by_const,special_cases)88 TEST(fast_urem_by_const, special_cases)
89 {
90    test_case(0, 1);
91    test_case(0, UINT32_MAX);
92    test_case(UINT32_MAX, 1);
93    test_case(1, UINT32_MAX);
94    test_case(UINT32_MAX, UINT32_MAX);
95    test_case(UINT32_MAX, UINT32_MAX - 1);
96    test_case(UINT32_MAX - 1, UINT32_MAX - 1);
97    test_case(UINT32_MAX - 2, UINT32_MAX - 1);
98 }
99 
100