-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler_4_tests.cpp
More file actions
95 lines (72 loc) · 2.27 KB
/
euler_4_tests.cpp
File metadata and controls
95 lines (72 loc) · 2.27 KB
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
#include "euler_4.h"
#include <iostream>
#include <cassert>
void test_extract_digits_normal();
void test_extract_digits_zero();
void test_extract_digits_zero_place_value();
void test_is_palindrome_normal();
void test_is_palindrome_odd();
void test_is_palindrome_single();
void test_is_palindrome_number_normal();
void test_is_palindrome_number_single();
void test_find_largest_palindrome_product();
int main() {
test_extract_digits_normal();
test_extract_digits_zero();
test_extract_digits_zero_place_value();
test_is_palindrome_normal();
test_is_palindrome_odd();
test_is_palindrome_single();
test_is_palindrome_number_normal();
test_is_palindrome_number_single();
test_find_largest_palindrome_product();
std::cout << "All tests pass!" << std::endl;
return 0;
}
void test_extract_digits_normal() {
// normal case
unsigned val = 433853;
auto digits = extract_digits(val);
std::vector<int> digits_correct = {3, 5, 8, 3, 3, 4};
assert(digits == digits_correct);
}
void test_extract_digits_zero() {
// edge case: 0 alone
unsigned val = 0;
auto digits = extract_digits(val);
std::vector<int> digits_correct = {0};
assert(digits == digits_correct);
}
void test_extract_digits_zero_place_value() {
// edge case: 0s in the middle of the number
unsigned val = 90400;
auto digits = extract_digits(val);
std::vector<int> digits_correct = {0, 0, 4, 0, 9};
assert(digits == digits_correct);
}
void test_is_palindrome_normal() {
// normal case, even vector size
assert(is_palindrome({ 1, 2, 2, 1 }));
assert(!is_palindrome({ 1, 1, 1, 2 }));
}
void test_is_palindrome_odd() {
// edge case: vectors with odd size
assert(is_palindrome({ 1, 2, 3, 2, 1 }));
assert(!is_palindrome({ 1, 2, 3, 3, 1 }));
}
void test_is_palindrome_single() {
// edge case: single element
assert(is_palindrome({1}));
}
void test_is_palindrome_number_normal() {
// normal case
assert(is_palindrome(extract_digits(12321)));
}
void test_is_palindrome_number_single() {
// edge case: single digit
assert(is_palindrome(extract_digits(1)));
}
void test_find_largest_palindrome_product() {
assert(find_largest_palindrome_product(1) == 9);
assert(find_largest_palindrome_product(2) == 9009);
}