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