Skip to content

Commit 5a825be

Browse files
tersalandy31415
andauthored
Basic Unit Test for Verhoeff (#37195)
* Create basic Unit Test for Verhoff * Change the build script for Accessor class * Change structure and remove private accessors * Change dependencies in build file * Reduce dependencies list * Remove empty sources Editorial commit: remove extra `sources = []`, I expect our templates to handle this one missing. --------- Co-authored-by: Andrei Litvin <andy314@gmail.com>
1 parent 214e03e commit 5a825be

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

src/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ if (chip_build_tests) {
6666
"${chip_root}/src/protocols/interaction_model/tests",
6767
"${chip_root}/src/protocols/secure_channel/tests",
6868
"${chip_root}/src/protocols/user_directed_commissioning/tests",
69+
"${chip_root}/src/lib/support/verhoeff/tests",
6970
"${chip_root}/src/system/tests",
7071
"${chip_root}/src/transport/retransmit/tests",
7172
"${chip_root}/src/transport/tests",
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright (c) 2024 Project CHIP Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import("//build_overrides/build.gni")
16+
import("//build_overrides/chip.gni")
17+
import("//build_overrides/pigweed.gni")
18+
import("${chip_root}/build/chip/chip_test_suite.gni")
19+
20+
chip_test_suite("tests") {
21+
output_name = "libVerhoeffTest"
22+
23+
test_sources = [ "TestVerhoeff.cpp" ]
24+
25+
public_deps = [ "${chip_root}/src/lib/support:support" ]
26+
27+
cflags = [ "-Wconversion" ]
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
*
3+
* Copyright (c) 2025 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include <pw_unit_test/framework.h>
19+
20+
#include <lib/support/verhoeff/Verhoeff.h>
21+
22+
#include <string>
23+
24+
namespace {
25+
26+
TEST(TestVerhoeff, TestVerhoeff)
27+
{
28+
29+
// Valid cases
30+
EXPECT_EQ(Verhoeff10::ComputeCheckChar("236"), '3');
31+
EXPECT_EQ(Verhoeff10::ComputeCheckChar("0"), '4');
32+
EXPECT_EQ(Verhoeff10::ComputeCheckChar("11111"), '5');
33+
34+
EXPECT_TRUE(Verhoeff10::ValidateCheckChar("123451"));
35+
EXPECT_TRUE(Verhoeff10::ValidateCheckChar("0987652"));
36+
EXPECT_TRUE(Verhoeff10::ValidateCheckChar("150"));
37+
38+
// Invalid cases
39+
EXPECT_NE(Verhoeff10::ComputeCheckChar("236"), '8');
40+
EXPECT_NE(Verhoeff10::ComputeCheckChar("0"), '1');
41+
EXPECT_NE(Verhoeff10::ComputeCheckChar("11111"), '7');
42+
43+
EXPECT_FALSE(Verhoeff10::ValidateCheckChar("123456"));
44+
EXPECT_FALSE(Verhoeff10::ValidateCheckChar("0987651"));
45+
EXPECT_FALSE(Verhoeff10::ValidateCheckChar("157"));
46+
47+
// Transposition
48+
EXPECT_NE(Verhoeff10::ComputeCheckChar("12345"), Verhoeff10::ComputeCheckChar("13245"));
49+
EXPECT_NE(Verhoeff10::ComputeCheckChar("1122334455"), Verhoeff10::ComputeCheckChar("1122334545"));
50+
EXPECT_NE(Verhoeff10::ComputeCheckChar("1234567890"), Verhoeff10::ComputeCheckChar("1234567809"));
51+
52+
// Non adjacent transpose
53+
EXPECT_NE(Verhoeff10::ComputeCheckChar("12345"), Verhoeff10::ComputeCheckChar("14325"));
54+
EXPECT_NE(Verhoeff10::ComputeCheckChar("876543"), Verhoeff10::ComputeCheckChar("678543"));
55+
56+
// Long numbers
57+
EXPECT_EQ(Verhoeff10::ComputeCheckChar("4356678912349008"), '7');
58+
EXPECT_EQ(Verhoeff10::ComputeCheckChar("78324562830019274123748"), '4');
59+
60+
// Invalid character
61+
EXPECT_EQ(Verhoeff10::ComputeCheckChar("123F4567"), 0);
62+
EXPECT_EQ(Verhoeff10::ComputeCheckChar("0A"), 0);
63+
EXPECT_EQ(Verhoeff10::ComputeCheckChar("I"), 0);
64+
EXPECT_EQ(Verhoeff10::ComputeCheckChar("23.4"), 0);
65+
66+
// Empty string
67+
EXPECT_EQ(Verhoeff10::ComputeCheckChar(""), '0');
68+
}
69+
70+
} // namespace

0 commit comments

Comments
 (0)