Skip to content

Commit 3ee0975

Browse files
committed
updated
1 parent 5ac9c9f commit 3ee0975

1 file changed

Lines changed: 41 additions & 38 deletions

File tree

test/safe_arithmetic.cxx

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
#include "args.hxx"
2-
#include <cassert>
1+
#include "test_common.hxx"
2+
3+
#include <args.hxx>
4+
5+
#include "test_helpers.hxx"
6+
37
#include <iostream>
48
#include <limits>
5-
#include <cmath>
69

710
/** Test suite for checked arithmetic functions
811
*
@@ -16,27 +19,27 @@ void TestSafeAddSizeT()
1619
size_t result;
1720

1821
// Test normal addition
19-
assert(args::SafeAdd<size_t>(5, 10, result));
20-
assert(result == 15);
22+
test::require(args::SafeAdd<size_t>(5, 10, result));
23+
test::require(result == 15);
2124
std::cout << "PASS: SafeAdd normal case (5 + 10 = 15)" << std::endl;
2225

2326
// Test addition at limit boundary
2427
size_t max_val = std::numeric_limits<size_t>::max();
25-
assert(!args::SafeAdd<size_t>(max_val, 1, result));
28+
test::require_false(args::SafeAdd<size_t>(max_val, 1, result));
2629
std::cout << "PASS: SafeAdd overflow detection (SIZE_MAX + 1)" << std::endl;
2730

2831
// Test addition near limit
29-
assert(!args::SafeAdd<size_t>(max_val - 5, 10, result));
32+
test::require_false(args::SafeAdd<size_t>(max_val - 5, 10, result));
3033
std::cout << "PASS: SafeAdd near-overflow detection (SIZE_MAX - 5 + 10)" << std::endl;
3134

3235
// Test addition with zero
33-
assert(args::SafeAdd<size_t>(0, 100, result));
34-
assert(result == 100);
36+
test::require(args::SafeAdd<size_t>(0, 100, result));
37+
test::require(result == 100);
3538
std::cout << "PASS: SafeAdd with zero (0 + 100 = 100)" << std::endl;
3639

3740
// Test addition at exact boundary
38-
assert(args::SafeAdd<size_t>(max_val - 5, 5, result));
39-
assert(result == max_val);
41+
test::require(args::SafeAdd<size_t>(max_val - 5, 5, result));
42+
test::require(result == max_val);
4043
std::cout << "PASS: SafeAdd exact boundary (SIZE_MAX - 5 + 5 = SIZE_MAX)" << std::endl;
4144
}
4245

@@ -46,30 +49,30 @@ void TestSafeMultiplySizeT()
4649
size_t result;
4750

4851
// Test normal multiplication
49-
assert(args::SafeMultiply<size_t>(5, 10, result));
50-
assert(result == 50);
52+
test::require(args::SafeMultiply<size_t>(5, 10, result));
53+
test::require(result == 50);
5154
std::cout << "PASS: SafeMultiply normal case (5 * 10 = 50)" << std::endl;
5255

5356
// Test multiplication that would overflow
5457
size_t max_val = std::numeric_limits<size_t>::max();
55-
assert(!args::SafeMultiply<size_t>(max_val, 2, result));
58+
test::require_false(args::SafeMultiply<size_t>(max_val, 2, result));
5659
std::cout << "PASS: SafeMultiply overflow detection (SIZE_MAX * 2)" << std::endl;
5760

5861
// Test multiplication with zero
59-
assert(args::SafeMultiply<size_t>(0, 1000000, result));
60-
assert(result == 0);
62+
test::require(args::SafeMultiply<size_t>(0, 1000000, result));
63+
test::require(result == 0);
6164
std::cout << "PASS: SafeMultiply with zero (0 * 1000000 = 0)" << std::endl;
6265

6366
// Test large multiplications that overflow (for 64-bit systems)
6467
// Use values that will definitely overflow
6568
size_t large_val = 10000000000UL; // 10 billion
66-
assert(!args::SafeMultiply<size_t>(large_val, large_val, result));
69+
test::require_false(args::SafeMultiply<size_t>(large_val, large_val, result));
6770
std::cout << "PASS: SafeMultiply large value overflow (10B * 10B)" << std::endl;
6871

6972
// Test multiplication that doesn't overflow
7073
size_t safe_val = 1000000UL; // 1 million
71-
assert(args::SafeMultiply<size_t>(safe_val, safe_val, result));
72-
assert(result == safe_val * safe_val);
74+
test::require(args::SafeMultiply<size_t>(safe_val, safe_val, result));
75+
test::require(result == safe_val * safe_val);
7376
std::cout << "PASS: SafeMultiply safe value success (1M * 1M)" << std::endl;
7477
}
7578

@@ -79,22 +82,22 @@ void TestSafeAddInt()
7982
int result;
8083

8184
// Test normal addition
82-
assert(args::SafeAdd<int>(100, 200, result));
83-
assert(result == 300);
85+
test::require(args::SafeAdd<int>(100, 200, result));
86+
test::require(result == 300);
8487
std::cout << "PASS: SafeAdd int normal case (100 + 200 = 300)" << std::endl;
8588

8689
// Test negative number handling
87-
assert(args::SafeAdd<int>(-100, 50, result));
88-
assert(result == -50);
90+
test::require(args::SafeAdd<int>(-100, 50, result));
91+
test::require(result == -50);
8992
std::cout << "PASS: SafeAdd int with negative (-100 + 50 = -50)" << std::endl;
9093

9194
// Test negative operand underflow detection
92-
assert(!args::SafeAdd<int>(std::numeric_limits<int>::min(), -1, result));
95+
test::require_false(args::SafeAdd<int>(std::numeric_limits<int>::min(), -1, result));
9396
std::cout << "PASS: SafeAdd int underflow detection (INT_MIN + -1)" << std::endl;
9497

9598
// Test int overflow
9699
int max_int = std::numeric_limits<int>::max();
97-
assert(!args::SafeAdd<int>(max_int, 1, result));
100+
test::require_false(args::SafeAdd<int>(max_int, 1, result));
98101
std::cout << "PASS: SafeAdd int overflow detection (INT_MAX + 1)" << std::endl;
99102
}
100103

@@ -104,18 +107,18 @@ void TestSafeMultiplyInt()
104107
int result;
105108

106109
// Test normal multiplication
107-
assert(args::SafeMultiply<int>(10, 20, result));
108-
assert(result == 200);
110+
test::require(args::SafeMultiply<int>(10, 20, result));
111+
test::require(result == 200);
109112
std::cout << "PASS: SafeMultiply int normal case (10 * 20 = 200)" << std::endl;
110113

111114
// Test multiplication overflow
112115
int max_int = std::numeric_limits<int>::max();
113-
assert(!args::SafeMultiply<int>(max_int, 2, result));
116+
test::require_false(args::SafeMultiply<int>(max_int, 2, result));
114117
std::cout << "PASS: SafeMultiply int overflow detection (INT_MAX * 2)" << std::endl;
115118

116119
// Test negative multiplication
117-
assert(args::SafeMultiply<int>(-10, 20, result));
118-
assert(result == -200);
120+
test::require(args::SafeMultiply<int>(-10, 20, result));
121+
test::require(result == -200);
119122
std::cout << "PASS: SafeMultiply int negative (-10 * 20 = -200)" << std::endl;
120123
}
121124

@@ -127,29 +130,29 @@ void TestWrapBoundaryConditions()
127130

128131
// This should not cause overflow or crash
129132
auto result = args::Wrap(words.begin(), words.end(), std::numeric_limits<size_t>::max());
130-
assert(!result.empty());
133+
test::require(!result.empty());
131134
std::cout << "PASS: Wrap with SIZE_MAX width" << std::endl;
132135

133136
// Test with width of 1 (minimal width)
134137
result = args::Wrap(words.begin(), words.end(), 1);
135-
assert(!result.empty());
138+
test::require(!result.empty());
136139
std::cout << "PASS: Wrap with width = 1" << std::endl;
137140

138141
// Test with width of 0 (edge case)
139142
result = args::Wrap(words.begin(), words.end(), 0);
140-
assert(!result.empty());
143+
test::require(!result.empty());
141144
std::cout << "PASS: Wrap with width = 0" << std::endl;
142145

143146
// Test with empty words vector
144147
std::vector<std::string> empty_words;
145148
result = args::Wrap(empty_words.begin(), empty_words.end(), 80);
146-
assert(result.empty());
149+
test::require(result.empty());
147150
std::cout << "PASS: Wrap with empty words vector" << std::endl;
148151

149152
// Test with newline separator
150153
std::vector<std::string> words_with_newline = {"hello", "\n", "world"};
151154
result = args::Wrap(words_with_newline.begin(), words_with_newline.end(), 80);
152-
assert(result.size() >= 2);
155+
test::require(result.size() >= 2);
153156
std::cout << "PASS: Wrap with newline separator" << std::endl;
154157
}
155158

@@ -160,17 +163,17 @@ void TestWrapLargeStringInput()
160163
std::string long_string(1000, 'a');
161164

162165
auto result = args::Wrap(long_string, 80);
163-
assert(!result.empty());
166+
test::require(!result.empty());
164167
std::cout << "PASS: Wrap long string (1000 chars) with width 80" << std::endl;
165168

166169
// Test with extremely large width
167170
result = args::Wrap(long_string, std::numeric_limits<size_t>::max());
168-
assert(result.size() >= 1);
171+
test::require(result.size() >= 1);
169172
std::cout << "PASS: Wrap long string with SIZE_MAX width" << std::endl;
170173

171174
// Test with width of 1
172175
result = args::Wrap(long_string, 1);
173-
assert(!result.empty());
176+
test::require(!result.empty());
174177
std::cout << "PASS: Wrap long string with width 1" << std::endl;
175178
}
176179

0 commit comments

Comments
 (0)