1 | //===-- Unittests for l64a ------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "src/__support/CPP/limits.h" |
10 | #include "src/stdlib/l64a.h" |
11 | #include "test/UnitTest/Test.h" |
12 | |
13 | TEST(LlvmLibcL64aTest, Zero) { |
14 | ASSERT_STREQ(LIBC_NAMESPACE::l64a(0), "......" ); |
15 | } |
16 | TEST(LlvmLibcL64aTest, Max) { |
17 | ASSERT_STREQ(LIBC_NAMESPACE::l64a( |
18 | LIBC_NAMESPACE::cpp::numeric_limits<uint32_t>::max()), |
19 | "zzzzz1" ); |
20 | } |
21 | |
22 | constexpr char B64_CHARS[64] = { |
23 | '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', |
24 | 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', |
25 | 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', |
26 | 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', |
27 | 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
28 | }; |
29 | |
30 | TEST(LlvmLibcL64aTest, OneCharacter) { |
31 | // The trailing null is technically unnecessary, but it means it won't look |
32 | // bad when we print it. |
33 | char expected_str[7] = {'\0', '.', '.', '.', '.', '.', '\0'}; |
34 | |
35 | for (size_t i = 0; i < 64; ++i) { |
36 | expected_str[0] = B64_CHARS[i]; |
37 | ASSERT_STREQ(LIBC_NAMESPACE::l64a(i), expected_str); |
38 | } |
39 | } |
40 | |
41 | TEST(LlvmLibcL64aTest, TwoCharacters) { |
42 | char expected_str[7] = {'\0', '\0', '.', '.', '.', '.', '\0'}; |
43 | |
44 | for (size_t first = 0; first < 64; ++first) { |
45 | expected_str[0] = B64_CHARS[first]; |
46 | for (size_t second = 0; second < 64; ++second) { |
47 | expected_str[1] = B64_CHARS[second]; |
48 | |
49 | ASSERT_STREQ(LIBC_NAMESPACE::l64a(first + (second * 64)), expected_str); |
50 | } |
51 | } |
52 | } |
53 | |
54 | TEST(LlvmLibcL64aTest, FiveSameCharacters) { |
55 | // Only using 5 because those are the only digits that can be any character. |
56 | char expected_str[7] = {'\0', '\0', '\0', '\0', '\0', '.', '\0'}; |
57 | |
58 | // set every 6th bit |
59 | const long BASE_NUM = 0b1000001000001000001000001; |
60 | |
61 | for (size_t char_val = 0; char_val < 64; ++char_val) { |
62 | for (size_t i = 0; i < 5; ++i) |
63 | expected_str[i] = B64_CHARS[char_val]; |
64 | |
65 | const long input_num = BASE_NUM * char_val; |
66 | |
67 | ASSERT_STREQ(LIBC_NAMESPACE::l64a(input_num), expected_str); |
68 | } |
69 | } |
70 | |
71 | TEST(LlvmLibcL64aTest, OneOfSixCharacters) { |
72 | char expected_str[7] = {'\0', '\0', '\0', '\0', '\0', '\0', '\0'}; |
73 | |
74 | for (size_t cur_char = 0; cur_char < 6; ++cur_char) { |
75 | // clear the string, set all the chars to b64(0) |
76 | for (size_t i = 0; i < 6; ++i) |
77 | expected_str[i] = B64_CHARS[0]; |
78 | |
79 | for (size_t char_val = 0; char_val < 64; ++char_val) { |
80 | // Since each base64 character holds 6 bits and we're only using 32 bits |
81 | // of input, the 6th character only gets 2 bits, so it can never be |
82 | // greater than 3. |
83 | if (char_val > 3 && cur_char == 5) |
84 | break; |
85 | expected_str[cur_char] = B64_CHARS[char_val]; |
86 | |
87 | // Need to limit to 32 bits, since that's what the standard says the |
88 | // function does. |
89 | const long input_num = static_cast<int32_t>(char_val << (6 * cur_char)); |
90 | |
91 | ASSERT_STREQ(LIBC_NAMESPACE::l64a(input_num), expected_str); |
92 | } |
93 | } |
94 | } |
95 | |