Henrique Nakashima | 7465522 | 2017-09-06 11:11:49 -0400 | [diff] [blame] | 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_OnedUPCAWriter.h" |
| 6 | #include "testing/gtest/include/gtest/gtest.h" |
| 7 | |
| 8 | namespace { |
| 9 | |
| 10 | TEST(OnedUPCAWriterTest, Encode) { |
| 11 | CBC_OnedUPCAWriter writer; |
| 12 | int32_t width; |
| 13 | int32_t height; |
| 14 | |
| 15 | // TODO(hnakashima): CBC_OnedUPCAWriter is unique in that it needs to be |
| 16 | // Init()'d. Get rid of this call. |
| 17 | writer.Init(); |
| 18 | |
| 19 | // UPCA barcodes encode 12-digit numbers into 95 modules in a unidimensional |
| 20 | // disposition. |
| 21 | uint8_t* encoded = writer.Encode("", BCFORMAT_UPC_A, width, height); |
| 22 | EXPECT_EQ(nullptr, encoded); |
| 23 | FX_Free(encoded); |
| 24 | |
| 25 | encoded = writer.Encode("123", BCFORMAT_UPC_A, width, height); |
| 26 | EXPECT_EQ(nullptr, encoded); |
| 27 | FX_Free(encoded); |
| 28 | |
| 29 | encoded = writer.Encode("12345678901", BCFORMAT_UPC_A, width, height); |
| 30 | EXPECT_EQ(nullptr, encoded); |
| 31 | FX_Free(encoded); |
| 32 | |
| 33 | encoded = writer.Encode("1234567890123", BCFORMAT_UPC_A, width, height); |
| 34 | EXPECT_EQ(nullptr, encoded); |
| 35 | FX_Free(encoded); |
| 36 | |
| 37 | encoded = writer.Encode("123456789012", BCFORMAT_UPC_A, width, height); |
| 38 | const char* expected = |
| 39 | "# #" // Start |
| 40 | " ## #" // 1 L |
| 41 | " # ##" // 2 L |
| 42 | " #### #" // 3 L |
| 43 | " # ##" // 4 L |
| 44 | " ## #" // 5 L |
| 45 | " # ####" // 6 L |
| 46 | " # # " // Middle |
| 47 | "# # " // 7 R |
| 48 | "# # " // 8 R |
| 49 | "### # " // 9 R |
| 50 | "### # " // 0 R |
| 51 | "## ## " // 1 R |
| 52 | "## ## " // 2 R |
| 53 | "# #"; // End |
| 54 | EXPECT_NE(nullptr, encoded); |
| 55 | EXPECT_EQ(1, height); |
| 56 | EXPECT_EQ(static_cast<int32_t>(strlen(expected)), width); |
| 57 | for (size_t i = 0; i < strlen(expected); i++) { |
| 58 | EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i; |
| 59 | } |
| 60 | FX_Free(encoded); |
| 61 | |
| 62 | encoded = writer.Encode("777666555440", BCFORMAT_UPC_A, width, height); |
| 63 | expected = |
| 64 | "# #" // Start |
| 65 | " ### ##" // 7 L |
| 66 | " ### ##" // 7 L |
| 67 | " ### ##" // 7 L |
| 68 | " # ####" // 6 L |
| 69 | " # ####" // 6 L |
| 70 | " # ####" // 6 L |
| 71 | " # # " // Middle |
| 72 | "# ### " // 5 R |
| 73 | "# ### " // 5 R |
| 74 | "# ### " // 5 R |
| 75 | "# ### " // 4 R |
| 76 | "# ### " // 4 R |
| 77 | "### # " // 0 R |
| 78 | "# #"; // End |
| 79 | EXPECT_NE(nullptr, encoded); |
| 80 | EXPECT_EQ(1, height); |
| 81 | EXPECT_EQ(static_cast<int32_t>(strlen(expected)), width); |
| 82 | for (size_t i = 0; i < strlen(expected); i++) { |
| 83 | EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i; |
| 84 | } |
| 85 | FX_Free(encoded); |
| 86 | } |
| 87 | |
| 88 | TEST(OnedUPCAWriterTest, Checksum) { |
| 89 | CBC_OnedUPCAWriter writer; |
| 90 | EXPECT_EQ(0, writer.CalcChecksum("")); |
| 91 | EXPECT_EQ(6, writer.CalcChecksum("123")); |
| 92 | EXPECT_EQ(2, writer.CalcChecksum("12345678901")); |
| 93 | EXPECT_EQ(0, writer.CalcChecksum("77766655544")); |
| 94 | } |
| 95 | |
| 96 | } // namespace |