• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "fxbarcode/oned/BC_OnedCode128Writer.h"
6 
7 #include "testing/gtest/include/gtest/gtest.h"
8 
9 namespace {
10 
11 struct TestCase {
12   const char* input;
13   int32_t checksum;
14   int32_t patterns[7];
15   size_t num_patterns;
16 };
17 
TEST(OnedCode128WriterTest,Encode128B)18 TEST(OnedCode128WriterTest, Encode128B) {
19   char buf[100];
20   static const TestCase kTestCases[] = {
21       {"", 104, {104}, 1},
22       {"a", 169, {104, 65}, 2},
23       {"1", 121, {104, 17}, 2},
24       {"a1", 203, {104, 65, 17}, 3},
25       {"ab", 301, {104, 65, 66}, 3},
26       {"12", 157, {104, 17, 18}, 3},
27       {"abc", 502, {104, 65, 66, 67}, 4},
28       {"123", 214, {104, 17, 18, 19}, 4},
29       {"abc123", 774, {104, 65, 66, 67, 17, 18, 19}, 7},
30       {"ABC123", 582, {104, 33, 34, 35, 17, 18, 19}, 7},
31       {"321ABC", 722, {104, 19, 18, 17, 33, 34, 35}, 7},
32       {"XYZ", 448, {104, 56, 57, 58}, 4},
33   };
34   for (size_t i = 0; i < FX_ArraySize(kTestCases); ++i) {
35     FXSYS_snprintf(buf, sizeof(buf) - 1, "Test case %zu", i);
36     SCOPED_TRACE(buf);
37     const TestCase& test_case = kTestCases[i];
38     std::vector<int32_t> patterns;
39     int32_t checksum =
40         CBC_OnedCode128Writer::Encode128B(test_case.input, &patterns);
41     EXPECT_EQ(test_case.checksum, checksum);
42     ASSERT_EQ(test_case.num_patterns, patterns.size());
43     for (size_t j = 0; j < patterns.size(); ++j) {
44       FXSYS_snprintf(buf, sizeof(buf) - 1, "Comparison %zu", j);
45       SCOPED_TRACE(buf);
46       EXPECT_EQ(test_case.patterns[j], patterns[j]);
47     }
48   }
49 }
50 
TEST(OnedCode128WriterTest,Encode128C)51 TEST(OnedCode128WriterTest, Encode128C) {
52   char buf[100];
53   static const TestCase kTestCases[] = {
54       {"", 105, {105}, 1},
55       {"a", 202, {105, 97}, 2},
56       {"1", 106, {105, 1}, 2},
57       {"a1", 204, {105, 97, 1}, 3},
58       {"ab", 398, {105, 97, 98}, 3},
59       {"12", 117, {105, 12}, 2},
60       {"abc", 695, {105, 97, 98, 99}, 4},
61       {"123", 123, {105, 12, 3}, 3},
62       {"abc123", 758, {105, 97, 98, 99, 12, 3}, 6},
63       {"ABC123", 566, {105, 65, 66, 67, 12, 3}, 6},
64       {"321ABC", 933, {105, 32, 1, 65, 66, 67}, 6},
65       {"XYZ", 641, {105, 88, 89, 90}, 4},
66   };
67   for (size_t i = 0; i < FX_ArraySize(kTestCases); ++i) {
68     FXSYS_snprintf(buf, sizeof(buf) - 1, "Test case %zu", i);
69     SCOPED_TRACE(buf);
70     const TestCase& test_case = kTestCases[i];
71     std::vector<int32_t> patterns;
72     int32_t checksum =
73         CBC_OnedCode128Writer::Encode128C(test_case.input, &patterns);
74     EXPECT_EQ(test_case.checksum, checksum);
75     ASSERT_EQ(test_case.num_patterns, patterns.size());
76     for (size_t j = 0; j < patterns.size(); ++j) {
77       FXSYS_snprintf(buf, sizeof(buf) - 1, "Comparison %zu", j);
78       SCOPED_TRACE(buf);
79       EXPECT_EQ(test_case.patterns[j], patterns[j]);
80     }
81   }
82 }
83 
TEST(OnedCode128WriterTest,CheckContentValidity)84 TEST(OnedCode128WriterTest, CheckContentValidity) {
85   {
86     CBC_OnedCode128Writer writer(BC_CODE128_B);
87     EXPECT_TRUE(writer.CheckContentValidity(L""));
88     EXPECT_TRUE(writer.CheckContentValidity(L"foo"));
89     EXPECT_TRUE(writer.CheckContentValidity(L"xyz"));
90     EXPECT_FALSE(writer.CheckContentValidity(L"\""));
91     EXPECT_FALSE(writer.CheckContentValidity(L"f\x10oo"));
92     EXPECT_FALSE(writer.CheckContentValidity(L"bar\x7F"));
93     EXPECT_FALSE(writer.CheckContentValidity(L"qux\x88"));
94   }
95   {
96     CBC_OnedCode128Writer writer(BC_CODE128_C);
97     EXPECT_TRUE(writer.CheckContentValidity(L""));
98     EXPECT_TRUE(writer.CheckContentValidity(L"foo"));
99     EXPECT_TRUE(writer.CheckContentValidity(L"xyz"));
100     EXPECT_FALSE(writer.CheckContentValidity(L"\""));
101     EXPECT_FALSE(writer.CheckContentValidity(L"f\x10oo"));
102     EXPECT_FALSE(writer.CheckContentValidity(L"bar\x7F"));
103     EXPECT_FALSE(writer.CheckContentValidity(L"qux\x88"));
104   }
105 }
106 
TEST(OnedCode128WriterTest,FilterContents)107 TEST(OnedCode128WriterTest, FilterContents) {
108   {
109     CBC_OnedCode128Writer writer(BC_CODE128_B);
110     EXPECT_STREQ(L"", writer.FilterContents(L"").c_str());
111     EXPECT_STREQ(L"foo", writer.FilterContents(L"foo\x10").c_str());
112     EXPECT_STREQ(L"fool", writer.FilterContents(L"foo\x10l").c_str());
113     EXPECT_STREQ(L"foo", writer.FilterContents(L"foo\x10\x7F").c_str());
114     EXPECT_STREQ(L"foo", writer.FilterContents(L"foo\x10\x7F\x88").c_str());
115     EXPECT_STREQ(L"bar", writer.FilterContents(L"bar\x10\x7F\x88").c_str());
116   }
117   {
118     CBC_OnedCode128Writer writer(BC_CODE128_C);
119     EXPECT_STREQ(L"", writer.FilterContents(L"").c_str());
120     EXPECT_STREQ(L"f", writer.FilterContents(L"foo\x10").c_str());
121     EXPECT_STREQ(L"f", writer.FilterContents(L"foo\x10l").c_str());
122     EXPECT_STREQ(L"f", writer.FilterContents(L"foo\x10\x7F").c_str());
123     EXPECT_STREQ(L"f", writer.FilterContents(L"foo\x10\x7F\x88").c_str());
124     EXPECT_STREQ(L"ba", writer.FilterContents(L"bar\x10\x7F\x88").c_str());
125   }
126 }
127 
128 }  // namespace
129