• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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/datamatrix/BC_DataMatrixWriter.h"
6 
7 #include <stdint.h>
8 
9 #include "core/fxcrt/data_vector.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 
13 using testing::ElementsAreArray;
14 
15 class CBCDataMatrixWriterTest : public testing::Test {
16  public:
17   CBCDataMatrixWriterTest() = default;
18   ~CBCDataMatrixWriterTest() override = default;
19 
20   // testing::Test:
SetUp()21   void SetUp() override { BC_Library_Init(); }
TearDown()22   void TearDown() override { BC_Library_Destroy(); }
23 };
24 
TEST_F(CBCDataMatrixWriterTest,Encode)25 TEST_F(CBCDataMatrixWriterTest, Encode) {
26   CBC_DataMatrixWriter writer;
27   int32_t width = -1;
28   int32_t height = -1;
29 
30   {
31     static constexpr int kExpectedDimension = 10;
32     // clang-format off
33     static constexpr uint8_t kExpectedData[] = {
34         1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
35         1, 1, 0, 1, 1, 0, 1, 0, 0, 1,
36         1, 1, 0, 1, 0, 0, 0, 0, 1, 0,
37         1, 1, 1, 1, 0, 0, 0, 1, 0, 1,
38         1, 0, 1, 0, 0, 0, 1, 0, 0, 0,
39         1, 1, 1, 0, 1, 0, 0, 0, 0, 1,
40         1, 0, 0, 1, 0, 1, 1, 0, 1, 0,
41         1, 0, 1, 0, 1, 1, 1, 1, 0, 1,
42         1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
43         1, 1, 1, 1, 1, 1, 1, 1, 1, 1
44     };
45     // clang-format on
46     DataVector<uint8_t> data = writer.Encode(L"", &width, &height);
47     ASSERT_EQ(std::size(kExpectedData), data.size());
48     ASSERT_EQ(kExpectedDimension, width);
49     ASSERT_EQ(kExpectedDimension, height);
50     EXPECT_THAT(data, ElementsAreArray(kExpectedData));
51   }
52   {
53     static constexpr int kExpectedDimension = 14;
54     // clang-format off
55     static constexpr uint8_t kExpectedData[] = {
56         1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
57         1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1,
58         1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0,
59         1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1,
60         1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0,
61         1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1,
62         1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0,
63         1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1,
64         1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0,
65         1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1,
66         1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0,
67         1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1,
68         1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0,
69         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
70     };
71     // clang-format on
72     DataVector<uint8_t> data = writer.Encode(L"helloworld", &width, &height);
73     ASSERT_EQ(std::size(kExpectedData), data.size());
74     ASSERT_EQ(kExpectedDimension, width);
75     ASSERT_EQ(kExpectedDimension, height);
76     EXPECT_THAT(data, ElementsAreArray(kExpectedData));
77   }
78   {
79     static constexpr int kExpectedDimension = 10;
80     // clang-format off
81     static constexpr uint8_t kExpectedData[] = {
82         1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
83         1, 1, 0, 1, 1, 0, 0, 1, 1, 1,
84         1, 1, 0, 0, 0, 1, 0, 1, 1, 0,
85         1, 1, 0, 0, 1, 1, 0, 1, 0, 1,
86         1, 1, 0, 0, 1, 1, 1, 0, 0, 0,
87         1, 0, 0, 0, 0, 1, 1, 1, 1, 1,
88         1, 1, 0, 1, 0, 1, 1, 1, 1, 0,
89         1, 1, 1, 0, 0, 0, 0, 1, 1, 1,
90         1, 1, 0, 1, 1, 0, 0, 1, 0, 0,
91         1, 1, 1, 1, 1, 1, 1, 1, 1, 1
92     };
93     // clang-format on
94     DataVector<uint8_t> data = writer.Encode(L"12345", &width, &height);
95     ASSERT_EQ(std::size(kExpectedData), data.size());
96     ASSERT_EQ(kExpectedDimension, width);
97     ASSERT_EQ(kExpectedDimension, height);
98     EXPECT_THAT(data, ElementsAreArray(kExpectedData));
99   }
100   {
101     static constexpr int kExpectedDimension = 18;
102     // clang-format off
103     static constexpr uint8_t kExpectedData[] = {
104         1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
105         1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1,
106         1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0,
107         1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1,
108         1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0,
109         1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1,
110         1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0,
111         1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1,
112         1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0,
113         1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1,
114         1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0,
115         1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1,
116         1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0,
117         1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1,
118         1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0,
119         1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1,
120         1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0,
121         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
122     };
123     // clang-format on
124     DataVector<uint8_t> data =
125         writer.Encode(L"abcdefghijklmnopqrst", &width, &height);
126     ASSERT_EQ(std::size(kExpectedData), data.size());
127     ASSERT_EQ(kExpectedDimension, width);
128     ASSERT_EQ(kExpectedDimension, height);
129     EXPECT_THAT(data, ElementsAreArray(kExpectedData));
130   }
131   {
132     DataVector<uint8_t> data = writer.Encode(L"hello world", &width, &height);
133     ASSERT_TRUE(data.empty());
134   }
135 }
136 
TEST_F(CBCDataMatrixWriterTest,EncodeLimitAlphaNumeric)137 TEST_F(CBCDataMatrixWriterTest, EncodeLimitAlphaNumeric) {
138   CBC_DataMatrixWriter writer;
139   int32_t width = -1;
140   int32_t height = -1;
141 
142   static constexpr int kMaxInputLength = 2335;  // Per spec.
143   WideString input;
144   for (size_t i = 0; i < kMaxInputLength; ++i)
145     input.InsertAtBack(L'a');
146 
147   {
148     static constexpr int kExpectedDimension = 144;
149     DataVector<uint8_t> data = writer.Encode(input.c_str(), &width, &height);
150     EXPECT_EQ(20736u, data.size());
151     EXPECT_EQ(kExpectedDimension, width);
152     EXPECT_EQ(kExpectedDimension, height);
153   }
154 
155   // Go over the limit.
156   input.InsertAtBack(L'a');
157   {
158     width = -1;
159     height = -1;
160     DataVector<uint8_t> data = writer.Encode(input.c_str(), &width, &height);
161     EXPECT_EQ(0u, data.size());
162     EXPECT_EQ(-1, width);
163     EXPECT_EQ(-1, height);
164   }
165 }
166 
TEST_F(CBCDataMatrixWriterTest,EncodeLimitNumbers)167 TEST_F(CBCDataMatrixWriterTest, EncodeLimitNumbers) {
168   CBC_DataMatrixWriter writer;
169   int32_t width = -1;
170   int32_t height = -1;
171 
172   static constexpr int kMaxInputLength = 3116;  // Per spec.
173   WideString input;
174   for (size_t i = 0; i < kMaxInputLength; ++i)
175     input.InsertAtBack(L'1');
176 
177   {
178     static constexpr int kExpectedDimension = 144;
179     DataVector<uint8_t> data = writer.Encode(input.c_str(), &width, &height);
180     EXPECT_EQ(20736u, data.size());
181     EXPECT_EQ(kExpectedDimension, width);
182     EXPECT_EQ(kExpectedDimension, height);
183   }
184 
185   // Go over the limit.
186   input.InsertAtBack(L'1');
187   {
188     width = -1;
189     height = -1;
190     DataVector<uint8_t> data = writer.Encode(input.c_str(), &width, &height);
191     EXPECT_EQ(0u, data.size());
192     EXPECT_EQ(-1, width);
193     EXPECT_EQ(-1, height);
194   }
195 }
196