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