Skip to content
This repository was archived by the owner on Oct 2, 2023. It is now read-only.

Commit 03ee106

Browse files
Add a much-needed test suite for C++.
1 parent ce9560f commit 03ee106

File tree

3 files changed

+354
-8
lines changed

3 files changed

+354
-8
lines changed

.clang-format

+216
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
---
2+
Language: Cpp
3+
# BasedOnStyle: LLVM
4+
AccessModifierOffset: -2
5+
AlignAfterOpenBracket: Align
6+
AlignArrayOfStructures: None
7+
AlignConsecutiveAssignments:
8+
Enabled: false
9+
AcrossEmptyLines: false
10+
AcrossComments: false
11+
AlignCompound: false
12+
PadOperators: true
13+
AlignConsecutiveBitFields:
14+
Enabled: false
15+
AcrossEmptyLines: false
16+
AcrossComments: false
17+
AlignCompound: false
18+
PadOperators: false
19+
AlignConsecutiveDeclarations:
20+
Enabled: false
21+
AcrossEmptyLines: false
22+
AcrossComments: false
23+
AlignCompound: false
24+
PadOperators: false
25+
AlignConsecutiveMacros:
26+
Enabled: false
27+
AcrossEmptyLines: false
28+
AcrossComments: false
29+
AlignCompound: false
30+
PadOperators: false
31+
AlignEscapedNewlines: Right
32+
AlignOperands: Align
33+
AlignTrailingComments: true
34+
AllowAllArgumentsOnNextLine: true
35+
AllowAllParametersOfDeclarationOnNextLine: true
36+
AllowShortEnumsOnASingleLine: true
37+
AllowShortBlocksOnASingleLine: Never
38+
AllowShortCaseLabelsOnASingleLine: false
39+
AllowShortFunctionsOnASingleLine: All
40+
AllowShortLambdasOnASingleLine: All
41+
AllowShortIfStatementsOnASingleLine: Never
42+
AllowShortLoopsOnASingleLine: false
43+
AlwaysBreakAfterDefinitionReturnType: None
44+
AlwaysBreakAfterReturnType: None
45+
AlwaysBreakBeforeMultilineStrings: false
46+
AlwaysBreakTemplateDeclarations: MultiLine
47+
AttributeMacros:
48+
- __capability
49+
BinPackArguments: true
50+
BinPackParameters: true
51+
BraceWrapping:
52+
AfterCaseLabel: false
53+
AfterClass: false
54+
AfterControlStatement: Never
55+
AfterEnum: false
56+
AfterFunction: false
57+
AfterNamespace: false
58+
AfterObjCDeclaration: false
59+
AfterStruct: false
60+
AfterUnion: false
61+
AfterExternBlock: false
62+
BeforeCatch: false
63+
BeforeElse: false
64+
BeforeLambdaBody: false
65+
BeforeWhile: false
66+
IndentBraces: false
67+
SplitEmptyFunction: true
68+
SplitEmptyRecord: true
69+
SplitEmptyNamespace: true
70+
BreakBeforeBinaryOperators: None
71+
BreakBeforeConceptDeclarations: Always
72+
BreakBeforeBraces: Attach
73+
BreakBeforeInheritanceComma: false
74+
BreakInheritanceList: BeforeColon
75+
BreakBeforeTernaryOperators: true
76+
BreakConstructorInitializersBeforeComma: false
77+
BreakConstructorInitializers: BeforeColon
78+
BreakAfterJavaFieldAnnotations: false
79+
BreakStringLiterals: true
80+
ColumnLimit: 80
81+
CommentPragmas: '^ IWYU pragma:'
82+
QualifierAlignment: Leave
83+
CompactNamespaces: false
84+
ConstructorInitializerIndentWidth: 4
85+
ContinuationIndentWidth: 4
86+
Cpp11BracedListStyle: true
87+
DeriveLineEnding: true
88+
DerivePointerAlignment: false
89+
DisableFormat: false
90+
EmptyLineAfterAccessModifier: Never
91+
EmptyLineBeforeAccessModifier: LogicalBlock
92+
ExperimentalAutoDetectBinPacking: false
93+
PackConstructorInitializers: BinPack
94+
BasedOnStyle: ''
95+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
96+
AllowAllConstructorInitializersOnNextLine: true
97+
FixNamespaceComments: true
98+
ForEachMacros:
99+
- foreach
100+
- Q_FOREACH
101+
- BOOST_FOREACH
102+
IfMacros:
103+
- KJ_IF_MAYBE
104+
IncludeBlocks: Preserve
105+
IncludeCategories:
106+
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
107+
Priority: 2
108+
SortPriority: 0
109+
CaseSensitive: false
110+
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
111+
Priority: 3
112+
SortPriority: 0
113+
CaseSensitive: false
114+
- Regex: '.*'
115+
Priority: 1
116+
SortPriority: 0
117+
CaseSensitive: false
118+
IncludeIsMainRegex: '(Test)?$'
119+
IncludeIsMainSourceRegex: ''
120+
IndentAccessModifiers: false
121+
IndentCaseLabels: false
122+
IndentCaseBlocks: false
123+
IndentGotoLabels: true
124+
IndentPPDirectives: None
125+
IndentExternBlock: AfterExternBlock
126+
IndentRequiresClause: true
127+
IndentWidth: 2
128+
IndentWrappedFunctionNames: false
129+
InsertBraces: false
130+
InsertTrailingCommas: None
131+
JavaScriptQuotes: Leave
132+
JavaScriptWrapImports: true
133+
KeepEmptyLinesAtTheStartOfBlocks: true
134+
LambdaBodyIndentation: Signature
135+
MacroBlockBegin: ''
136+
MacroBlockEnd: ''
137+
MaxEmptyLinesToKeep: 1
138+
NamespaceIndentation: None
139+
ObjCBinPackProtocolList: Auto
140+
ObjCBlockIndentWidth: 2
141+
ObjCBreakBeforeNestedBlockParam: true
142+
ObjCSpaceAfterProperty: false
143+
ObjCSpaceBeforeProtocolList: true
144+
PenaltyBreakAssignment: 2
145+
PenaltyBreakBeforeFirstCallParameter: 19
146+
PenaltyBreakComment: 300
147+
PenaltyBreakFirstLessLess: 120
148+
PenaltyBreakOpenParenthesis: 0
149+
PenaltyBreakString: 1000
150+
PenaltyBreakTemplateDeclaration: 10
151+
PenaltyExcessCharacter: 1000000
152+
PenaltyReturnTypeOnItsOwnLine: 60
153+
PenaltyIndentedWhitespace: 0
154+
PointerAlignment: Right
155+
PPIndentWidth: -1
156+
ReferenceAlignment: Pointer
157+
ReflowComments: true
158+
RemoveBracesLLVM: false
159+
RequiresClausePosition: OwnLine
160+
SeparateDefinitionBlocks: Leave
161+
ShortNamespaceLines: 1
162+
SortIncludes: CaseSensitive
163+
SortJavaStaticImport: Before
164+
SortUsingDeclarations: true
165+
SpaceAfterCStyleCast: false
166+
SpaceAfterLogicalNot: false
167+
SpaceAfterTemplateKeyword: true
168+
SpaceBeforeAssignmentOperators: true
169+
SpaceBeforeCaseColon: false
170+
SpaceBeforeCpp11BracedList: false
171+
SpaceBeforeCtorInitializerColon: true
172+
SpaceBeforeInheritanceColon: true
173+
SpaceBeforeParens: ControlStatements
174+
SpaceBeforeParensOptions:
175+
AfterControlStatements: true
176+
AfterForeachMacros: true
177+
AfterFunctionDefinitionName: false
178+
AfterFunctionDeclarationName: false
179+
AfterIfMacros: true
180+
AfterOverloadedOperator: false
181+
AfterRequiresInClause: false
182+
AfterRequiresInExpression: false
183+
BeforeNonEmptyParentheses: false
184+
SpaceAroundPointerQualifiers: Default
185+
SpaceBeforeRangeBasedForLoopColon: true
186+
SpaceInEmptyBlock: false
187+
SpaceInEmptyParentheses: false
188+
SpacesBeforeTrailingComments: 1
189+
SpacesInAngles: Never
190+
SpacesInConditionalStatement: false
191+
SpacesInContainerLiterals: true
192+
SpacesInCStyleCastParentheses: false
193+
SpacesInLineCommentPrefix:
194+
Minimum: 1
195+
Maximum: -1
196+
SpacesInParentheses: false
197+
SpacesInSquareBrackets: false
198+
SpaceBeforeSquareBrackets: false
199+
BitFieldColonSpacing: Both
200+
Standard: Latest
201+
StatementAttributeLikeMacros:
202+
- Q_EMIT
203+
StatementMacros:
204+
- Q_UNUSED
205+
- QT_REQUIRE_VERSION
206+
TabWidth: 8
207+
UseCRLF: false
208+
UseTab: Never
209+
WhitespaceSensitiveMacros:
210+
- STRINGIZE
211+
- PP_STRINGIZE
212+
- BOOST_PP_STRINGIZE
213+
- NS_SWIFT_NAME
214+
- CF_SWIFT_NAME
215+
...
216+

CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ target_link_libraries(os2dsrules PUBLIC os2dsrules_compiler_flags)
2222

2323
# Compile test suite.
2424
enable_testing()
25+
find_package(GTest REQUIRED)
26+
include_directories(${GTEST_INCLUDE_DIRS})
2527
add_executable(testsuite tests/test.cpp)
2628
target_include_directories(testsuite PUBLIC "${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR/include}")
27-
target_link_libraries(testsuite os2dsrules os2dsrules_compiler_flags)
29+
target_link_libraries(testsuite ${GTEST_LIBRARIES} os2dsrules os2dsrules_compiler_flags)
2830
add_test(unittests testsuite)
2931

3032
# Install library on system.

tests/test.cpp

+135-7
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,145 @@
22
#include <exception>
33
#include <string>
44
#include <iostream>
5+
#include <gtest/gtest.h>
56

67
using namespace OS2DSRules::CPRDetector;
78

8-
int main(void) {
9-
std::string content = "1111111118 2304516782";
9+
class CPRDetectorTest : public testing::Test {};
10+
11+
TEST_F(CPRDetectorTest, Test_Find_Basic_CPR_Number) {
12+
std::string content = "1111111118";
13+
CPRDetector detector(false);
14+
15+
auto results = detector.find_matches(content);
16+
17+
ASSERT_EQ(1, results.size());
18+
ASSERT_STREQ("1111111118", results[0].match().c_str());
19+
}
20+
21+
TEST_F(CPRDetectorTest, Test_Find_Basic_CPR_Number_With_Valid_Separators) {
22+
std::string content = "11 11 11 1118";
23+
CPRDetector detector(false);
24+
25+
auto results = detector.find_matches(content);
26+
27+
ASSERT_EQ(1, results.size());
28+
ASSERT_STREQ("1111111118", results[0].match().c_str());
29+
}
30+
31+
TEST_F(CPRDetectorTest, Test_Find_CPR_Number_With_Valid_Leap_Year) {
32+
std::string content = "2902081111";
33+
CPRDetector detector(false);
34+
35+
auto results = detector.find_matches(content);
36+
37+
ASSERT_EQ(1, results.size());
38+
ASSERT_STREQ("2902081111", results[0].match().c_str());
39+
}
40+
41+
TEST_F(CPRDetectorTest, Test_Find_CPR_Number_With_Modulus11_Check) {
42+
std::string content = "1111111118";
43+
CPRDetector detector(true);
44+
45+
auto results = detector.find_matches(content);
46+
47+
ASSERT_EQ(1, results.size());
48+
ASSERT_STREQ("1111111118", results[0].match().c_str());
49+
}
50+
51+
TEST_F(CPRDetectorTest, Test_Reject_CPR_Number_With_Too_Many_Separators) {
52+
std::string content = "111111 1118";
53+
CPRDetector detector(false);
54+
55+
auto results = detector.find_matches(content);
56+
57+
ASSERT_EQ(0, results.size());
58+
}
59+
60+
TEST_F(CPRDetectorTest, Test_Reject_CPR_Number_With_Invalid_Separator) {
61+
std::string content = "111111b1118";
62+
CPRDetector detector(false);
63+
64+
auto results = detector.find_matches(content);
65+
66+
ASSERT_EQ(0, results.size());
67+
}
68+
69+
TEST_F(CPRDetectorTest, Test_Reject_CPR_Number_With_Invalid_Prefix) {
70+
std::string content = "#1111111118";
71+
CPRDetector detector(false);
72+
73+
auto results = detector.find_matches(content);
74+
75+
ASSERT_EQ(0, results.size());
76+
}
77+
78+
TEST_F(CPRDetectorTest, Test_Reject_CPR_Number_With_Invalid_Suffix) {
79+
std::string content = "1111111118#";
80+
CPRDetector detector(false);
81+
82+
auto results = detector.find_matches(content);
83+
84+
ASSERT_EQ(0, results.size());
85+
}
86+
87+
TEST_F(CPRDetectorTest, Test_Reject_CPR_Number_With_Invalid_Date_Format) {
88+
std::string content = "9999999999";
89+
CPRDetector detector(false);
90+
91+
auto results = detector.find_matches(content);
92+
93+
ASSERT_EQ(0, results.size());
94+
}
95+
96+
TEST_F(CPRDetectorTest, Test_Reject_CPR_Number_With_Invalid_Date_February) {
97+
std::string content = "3002111111";
98+
CPRDetector detector(false);
99+
100+
auto results = detector.find_matches(content);
101+
102+
ASSERT_EQ(0, results.size());
103+
}
104+
105+
TEST_F(CPRDetectorTest, Test_Reject_CPR_Number_With_Invalid_Leap_Year) {
106+
std::string content = "2902111111";
107+
CPRDetector detector(false);
108+
109+
auto results = detector.find_matches(content);
110+
111+
ASSERT_EQ(0, results.size());
112+
}
113+
114+
TEST_F(CPRDetectorTest, Test_Reject_CPR_Number_With_Control_All_Zeros) {
115+
std::string content = "2902110000";
116+
CPRDetector detector(false);
117+
118+
auto results = detector.find_matches(content);
119+
120+
ASSERT_EQ(0, results.size());
121+
}
122+
123+
TEST_F(CPRDetectorTest, Test_Reject_CPR_Number_With_Modulus11_Check_Fail) {
124+
std::string content = "1111111111";
10125
CPRDetector detector(true);
11126

12-
auto result = detector.find_matches(content);
127+
auto results = detector.find_matches(content);
128+
129+
ASSERT_EQ(0, results.size());
130+
}
131+
132+
TEST_F(CPRDetectorTest, Test_Find_Two_CPR_Numbers_Separated_By_Whitespace) {
133+
std::string content = "1111111118 2304516782";
134+
CPRDetector detector(false);
135+
136+
auto results = detector.find_matches(content);
137+
138+
ASSERT_EQ(2, results.size());
139+
ASSERT_STREQ("1111111118", results[0].match().c_str());
140+
ASSERT_STREQ("2304516782", results[1].match().c_str());
141+
}
13142

14-
std::cout << "Found results: " << result.size() << '\n';
15-
for (auto res : result) {
16-
std::cout << res.match() << '\n';
17-
}
143+
int main(int argc, char **argv) {
144+
testing::InitGoogleTest(&argc, argv);
145+
return RUN_ALL_TESTS();
18146
}

0 commit comments

Comments
 (0)