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