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