• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The PDFium Authors
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 <iterator>
8 
9 #include "core/fxcrt/compiler_specific.h"
10 #include "core/fxcrt/span.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 
14 using ::testing::ElementsAreArray;
15 
16 namespace {
17 
18 struct TestCase {
pattern_span__anon1b2af0fa0111::TestCase19   pdfium::span<const int32_t> pattern_span() const {
20     return UNSAFE_TODO(pdfium::make_span(patterns, num_patterns));
21   }
22 
23   const char* input;
24   int32_t checksum;
25   int32_t patterns[7];
26   size_t num_patterns;
27 };
28 
TEST(OnedCode128WriterTest,Encode128B)29 TEST(OnedCode128WriterTest, Encode128B) {
30   static const TestCase kTestCases[] = {
31       {"", 104, {104}, 1},
32       {"a", 169, {104, 65}, 2},
33       {"1", 121, {104, 17}, 2},
34       {"a1", 203, {104, 65, 17}, 3},
35       {"ab", 301, {104, 65, 66}, 3},
36       {"12", 157, {104, 17, 18}, 3},
37       {"abc", 502, {104, 65, 66, 67}, 4},
38       {"123", 214, {104, 17, 18, 19}, 4},
39       {"abc123", 774, {104, 65, 66, 67, 17, 18, 19}, 7},
40       {"ABC123", 582, {104, 33, 34, 35, 17, 18, 19}, 7},
41       {"321ABC", 722, {104, 19, 18, 17, 33, 34, 35}, 7},
42       {"XYZ", 448, {104, 56, 57, 58}, 4},
43   };
44   for (const auto& test_case : kTestCases) {
45     std::vector<int32_t> patterns;
46     int32_t checksum =
47         CBC_OnedCode128Writer::Encode128B(test_case.input, &patterns);
48     EXPECT_EQ(test_case.checksum, checksum);
49     EXPECT_THAT(patterns, ElementsAreArray(test_case.pattern_span()));
50   }
51 }
52 
TEST(OnedCode128WriterTest,Encode128C)53 TEST(OnedCode128WriterTest, Encode128C) {
54   static const TestCase kTestCases[] = {
55       {"", 105, {105}, 1},
56       {"a", 202, {105, 97}, 2},
57       {"1", 106, {105, 1}, 2},
58       {"a1", 204, {105, 97, 1}, 3},
59       {"ab", 398, {105, 97, 98}, 3},
60       {"12", 117, {105, 12}, 2},
61       {"abc", 695, {105, 97, 98, 99}, 4},
62       {"123", 123, {105, 12, 3}, 3},
63       {"abc123", 758, {105, 97, 98, 99, 12, 3}, 6},
64       {"ABC123", 566, {105, 65, 66, 67, 12, 3}, 6},
65       {"321ABC", 933, {105, 32, 1, 65, 66, 67}, 6},
66       {"XYZ", 641, {105, 88, 89, 90}, 4},
67   };
68   for (const auto& test_case : kTestCases) {
69     std::vector<int32_t> patterns;
70     int32_t checksum =
71         CBC_OnedCode128Writer::Encode128C(test_case.input, &patterns);
72     EXPECT_EQ(test_case.checksum, checksum);
73     EXPECT_THAT(patterns, ElementsAreArray(test_case.pattern_span()));
74   }
75 }
76 
TEST(OnedCode128WriterTest,CheckContentValidity)77 TEST(OnedCode128WriterTest, CheckContentValidity) {
78   {
79     CBC_OnedCode128Writer writer(BC_TYPE::kCode128B);
80     EXPECT_TRUE(writer.CheckContentValidity(L"foo"));
81     EXPECT_TRUE(writer.CheckContentValidity(L"xyz"));
82     EXPECT_FALSE(writer.CheckContentValidity(L""));
83     EXPECT_FALSE(writer.CheckContentValidity(L"\""));
84     EXPECT_FALSE(writer.CheckContentValidity(L"f\x10oo"));
85     EXPECT_FALSE(writer.CheckContentValidity(L"bar\x7F"));
86     EXPECT_FALSE(writer.CheckContentValidity(L"qux\x88"));
87   }
88   {
89     CBC_OnedCode128Writer writer(BC_TYPE::kCode128C);
90     EXPECT_TRUE(writer.CheckContentValidity(L"foo"));
91     EXPECT_TRUE(writer.CheckContentValidity(L"xyz"));
92     EXPECT_FALSE(writer.CheckContentValidity(L""));
93     EXPECT_FALSE(writer.CheckContentValidity(L"\""));
94     EXPECT_FALSE(writer.CheckContentValidity(L"f\x10oo"));
95     EXPECT_FALSE(writer.CheckContentValidity(L"bar\x7F"));
96     EXPECT_FALSE(writer.CheckContentValidity(L"qux\x88"));
97   }
98 }
99 
TEST(OnedCode128WriterTest,FilterContents)100 TEST(OnedCode128WriterTest, FilterContents) {
101   {
102     CBC_OnedCode128Writer writer(BC_TYPE::kCode128B);
103     EXPECT_EQ(L"", writer.FilterContents(L""));
104     EXPECT_EQ(L"foo", writer.FilterContents(L"foo\x10"));
105     EXPECT_EQ(L"fool", writer.FilterContents(L"foo\x10l"));
106     EXPECT_EQ(L"foo", writer.FilterContents(L"foo\x10\x7F"));
107     EXPECT_EQ(L"foo", writer.FilterContents(L"foo\x10\x7F\x88"));
108     EXPECT_EQ(L"bar", writer.FilterContents(L"bar\x10\x7F\x88"));
109   }
110   {
111     CBC_OnedCode128Writer writer(BC_TYPE::kCode128C);
112     EXPECT_EQ(L"", writer.FilterContents(L""));
113     EXPECT_EQ(L"f", writer.FilterContents(L"foo\x10"));
114     EXPECT_EQ(L"f", writer.FilterContents(L"foo\x10l"));
115     EXPECT_EQ(L"f", writer.FilterContents(L"foo\x10\x7F"));
116     EXPECT_EQ(L"f", writer.FilterContents(L"foo\x10\x7F\x88"));
117     EXPECT_EQ(L"ba", writer.FilterContents(L"bar\x10\x7F\x88"));
118   }
119 }
120 
121 }  // namespace
122