forked from sakura-editor/sakura
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-int2dec.cpp
103 lines (87 loc) · 3.09 KB
/
test-int2dec.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*! @file */
/*
Copyright (C) 2018-2022, Sakura Editor Organization
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "pch.h"
#include <limits>
#ifndef NOMINMAX
#define NOMINMAX
#endif /* #ifndef NOMINMAX */
#include <Windows.h>
#include <tchar.h>
#include "basis/primitive.h"
#include "util/string_ex2.h"
template <typename T>
void test_int2dec(T value, ptrdiff_t lenExpected, const wchar_t* strExpected)
{
wchar_t buff[int2dec_destBufferSufficientLength<T>()];
ptrdiff_t len = int2dec(value, buff);
EXPECT_EQ(len, lenExpected);
EXPECT_STREQ(buff, strExpected);
}
template <typename T>
void test_plusminus(T plusValue, ptrdiff_t lenExpected, const wchar_t* strExpected)
{
test_int2dec(plusValue, lenExpected, strExpected);
test_int2dec(-plusValue, 1+lenExpected, (std::wstring(L"-")+strExpected).c_str());
}
static
void test_32_64_plus_minus(int value, ptrdiff_t lenExpected, const wchar_t* strExpected)
{
test_plusminus<int32_t>(value, lenExpected, strExpected);
test_plusminus<int64_t>(value, lenExpected, strExpected);
}
TEST(int2dec_test, zero)
{
test_int2dec<int32_t>(0, 1, L"0");
test_int2dec<int64_t>(0, 1, L"0");
}
TEST(int2dec_test, digits)
{
test_32_64_plus_minus(2, 1, L"2");
test_32_64_plus_minus(3, 1, L"3");
test_32_64_plus_minus(4, 1, L"4");
test_32_64_plus_minus(5, 1, L"5");
test_32_64_plus_minus(6, 1, L"6");
test_32_64_plus_minus(7, 1, L"7");
test_32_64_plus_minus(8, 1, L"8");
test_32_64_plus_minus(9, 1, L"9");
}
TEST(int2dec_test, max)
{
test_int2dec<int32_t>(std::numeric_limits<int32_t>::max(), 10, L"2147483647");
test_int2dec<int64_t>(std::numeric_limits<int64_t>::max(), 19, L"9223372036854775807");
}
TEST(int2dec_test, min)
{
test_int2dec<int32_t>(std::numeric_limits<int32_t>::min(), 11, L"-2147483648");
test_int2dec<int64_t>(std::numeric_limits<int64_t>::min(), 20, L"-9223372036854775808");
}
TEST(int2dec_test, group_sequence)
{
test_32_64_plus_minus(1, 1, L"1");
test_32_64_plus_minus(12, 2, L"12");
test_32_64_plus_minus(123, 3, L"123");
test_32_64_plus_minus(1234, 4, L"1234");
test_32_64_plus_minus(12345, 5, L"12345");
test_32_64_plus_minus(123456, 6, L"123456");
test_32_64_plus_minus(1234567, 7, L"1234567");
test_32_64_plus_minus(12345678, 8, L"12345678");
test_32_64_plus_minus(123456789, 9, L"123456789");
test_32_64_plus_minus(1234567890, 10, L"1234567890");
}