Debian Codegraph @

unittest/gunit/integer_digits-t.cc

raw symbols SCIP
1 /*
2 Copyright (c) 2019, 2026, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include "integer_digits.h"
27
28 #include <gtest/gtest.h>
29 #include <inttypes.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <iterator>
33 #include <limits>
34 #include <tuple>
35
36 namespace integer_digits_unittest {
37
38 TEST(IntegerDigits, CountDigits) {
39 EXPECT_EQ(1, count_digits(0U));
40 EXPECT_EQ(1, count_digits(1U));
41 EXPECT_EQ(3, count_digits(std::numeric_limits<uint8_t>::max()));
42 EXPECT_EQ(5, count_digits(std::numeric_limits<uint16_t>::max()));
43 EXPECT_EQ(10, count_digits(std::numeric_limits<uint32_t>::max()));
44 EXPECT_EQ(20, count_digits(std::numeric_limits<uint64_t>::max()));
45
46 uint64_t val = 1;
47 for (int i = 1; i <= std::numeric_limits<uint64_t>::digits10; ++i) {
48 val *= 10;
49 EXPECT_EQ(i, count_digits(val - 1));
50 EXPECT_EQ(i + 1, count_digits(val));
51 }
52 }
53
54 TEST(IntegerDigits, WriteTwoDigits) {
55 for (int i = 0; i < 100; ++i) {
56 char buffer[2];
57 EXPECT_EQ(std::end(buffer), write_two_digits(i, buffer));
58 EXPECT_EQ('0' + i / 10, buffer[0]);
59 EXPECT_EQ('0' + i % 10, buffer[1]);
60 }
61 }
62
63 class IntegerDigits : public testing::TestWithParam<std::tuple<uint64_t, int>> {
64 };
65
66 TEST_P(IntegerDigits, WriteDigits) {
67 const uint64_t value = std::get<0>(GetParam());
68 const int digits = std::get<1>(GetParam());
69
70 ASSERT_GT(digits, 0);
71
72 // Without this, GCC 10 warns in release builds that write_digits() might
73 // write outside the buffer, because it doesn't see that digits is always
74 // positive.
75 if (digits <= 0) return;
76
77 char buffer[100];
78 char *end = write_digits(value, digits, buffer);
79 *end = '\0'; // write_digits does not zero-terminate
80
81 char expected[100];
82 int expected_length =
83 snprintf(expected, sizeof(expected), "%0*" PRIu64, digits, value);
84
85 EXPECT_EQ(buffer + expected_length, end);
86 EXPECT_STREQ(expected, buffer);
87 }
88
89 // Test write_digits() with all input values from 0 to 1024, with a sufficiently
90 // large number of digits to hold all (4).
91 INSTANTIATE_TEST_SUITE_P(SmallValues, IntegerDigits,
92 testing::Combine(testing::Range(uint64_t{0},
93 uint64_t{1024}),
94 testing::Values(4)));
95
96 // Test write_digits() with the largest three values, with and without
97 // zero-padding.
98 INSTANTIATE_TEST_SUITE_P(
99 LargeValues, IntegerDigits,
100 testing::Combine(testing::Values(std::numeric_limits<uint64_t>::max() - 2,
101 std::numeric_limits<uint64_t>::max() - 1,
102 std::numeric_limits<uint64_t>::max()),
103 testing::Values(20, 21, 22)));
104
105 // Test write_digits() with all single-digit numbers, with and without
106 // zero-padding.
107 INSTANTIATE_TEST_SUITE_P(SingleDigits, IntegerDigits,
108 testing::Combine(testing::Range(uint64_t{0},
109 uint64_t{10}),
110 testing::Values(1, 2, 3)));
111
112 } // namespace integer_digits_unittest