|
| 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