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_OnedEAN13Writer.h"
6 
7 #include "testing/gtest/include/gtest/gtest.h"
8 
9 namespace {
10 
TEST(OnedEAN13WriterTest,Encode)11 TEST(OnedEAN13WriterTest, Encode) {
12   CBC_OnedEAN13Writer writer;
13   writer.InitEANWriter();
14   int32_t width;
15   int32_t height;
16   uint8_t* encoded;
17   const char* expected;
18 
19   // EAN-13 barcodes encode 13-digit numbers into 95 modules in a unidimensional
20   // disposition.
21   encoded = writer.Encode("", BCFORMAT_EAN_13, width, height);
22   EXPECT_EQ(nullptr, encoded);
23   FX_Free(encoded);
24 
25   encoded = writer.Encode("123", BCFORMAT_EAN_13, width, height);
26   EXPECT_EQ(nullptr, encoded);
27   FX_Free(encoded);
28 
29   encoded = writer.Encode("123456789012", BCFORMAT_EAN_13, width, height);
30   EXPECT_EQ(nullptr, encoded);
31   FX_Free(encoded);
32 
33   encoded = writer.Encode("12345678901234", BCFORMAT_EAN_13, width, height);
34   EXPECT_EQ(nullptr, encoded);
35   FX_Free(encoded);
36 
37   encoded = writer.Encode("1234567890128", BCFORMAT_EAN_13, width, height);
38   EXPECT_NE(nullptr, encoded);
39   EXPECT_EQ(1, height);
40   EXPECT_EQ(95, width);
41 
42   expected =
43       "# #"  // Start
44       // 1 implicit by LLGLGG in next 6 digits
45       "  #  ##"  // 2 L
46       " #### #"  // 3 L
47       "  ### #"  // 4 G
48       " ##   #"  // 5 L
49       "    # #"  // 6 G
50       "  #   #"  // 7 G
51       " # # "    // Middle
52       "#  #   "  // 8 R
53       "### #  "  // 9 R
54       "###  # "  // 0 R
55       "##  ## "  // 1 R
56       "## ##  "  // 2 R
57       "#  #   "  // 8 R
58       "# #";     // End
59   for (int i = 0; i < 95; i++) {
60     EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
61   }
62   FX_Free(encoded);
63 
64   encoded = writer.Encode("7776665554440", BCFORMAT_EAN_13, width, height);
65   EXPECT_NE(nullptr, encoded);
66   EXPECT_EQ(1, height);
67   EXPECT_EQ(95, width);
68 
69   expected =
70       "# #"  // Start
71       // 7 implicit by LGLGLG in next 6 digits
72       " ### ##"  // 7 L
73       "  #   #"  // 7 G
74       " # ####"  // 6 L
75       "    # #"  // 6 G
76       " # ####"  // 6 L
77       " ###  #"  // 5 G
78       " # # "    // Middle
79       "#  ### "  // 5 R
80       "#  ### "  // 5 R
81       "# ###  "  // 4 R
82       "# ###  "  // 4 R
83       "# ###  "  // 4 R
84       "###  # "  // 0 R
85       "# #";     // End
86   for (int i = 0; i < 95; i++) {
87     EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
88   }
89   FX_Free(encoded);
90 }
91 
TEST(OnedEAN13WriterTest,Checksum)92 TEST(OnedEAN13WriterTest, Checksum) {
93   CBC_OnedEAN13Writer writer;
94   writer.InitEANWriter();
95   EXPECT_EQ(0, writer.CalcChecksum(""));
96   EXPECT_EQ(6, writer.CalcChecksum("123"));
97   EXPECT_EQ(8, writer.CalcChecksum("123456789012"));
98   EXPECT_EQ(0, writer.CalcChecksum("777666555444"));
99 }
100 
101 }  // namespace
102