Home | History | Annotate | Download | only in oned
      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_OnedCode128Writer.h"
      6 #include "testing/gtest/include/gtest/gtest.h"
      7 
      8 namespace {
      9 
     10 struct TestCase {
     11   const char* input;
     12   int32_t checksum;
     13   int32_t patterns[7];
     14   size_t num_patterns;
     15 };
     16 
     17 TEST(OnedCode128WriterTest, Encode128B) {
     18   char buf[100];
     19   TestCase kTestCases[] = {
     20       {"", 104, {104}, 1},
     21       {"a", 169, {104, 65}, 2},
     22       {"1", 121, {104, 17}, 2},
     23       {"a1", 203, {104, 65, 17}, 3},
     24       {"ab", 301, {104, 65, 66}, 3},
     25       {"12", 157, {104, 17, 18}, 3},
     26       {"abc", 502, {104, 65, 66, 67}, 4},
     27       {"123", 214, {104, 17, 18, 19}, 4},
     28       {"abc123", 774, {104, 65, 66, 67, 17, 18, 19}, 7},
     29       {"ABC123", 582, {104, 33, 34, 35, 17, 18, 19}, 7},
     30       {"321ABC", 722, {104, 19, 18, 17, 33, 34, 35}, 7},
     31       {"XYZ", 448, {104, 56, 57, 58}, 4},
     32   };
     33   for (size_t i = 0; i < FX_ArraySize(kTestCases); ++i) {
     34     FXSYS_snprintf(buf, sizeof(buf) - 1, "Test case %zu", i);
     35     SCOPED_TRACE(buf);
     36     const TestCase& test_case = kTestCases[i];
     37     std::vector<int32_t> patterns;
     38     int32_t checksum =
     39         CBC_OnedCode128Writer::Encode128B(test_case.input, &patterns);
     40     EXPECT_EQ(test_case.checksum, checksum);
     41     ASSERT_EQ(test_case.num_patterns, patterns.size());
     42     for (size_t j = 0; j < patterns.size(); ++j) {
     43       FXSYS_snprintf(buf, sizeof(buf) - 1, "Comparison %zu", j);
     44       SCOPED_TRACE(buf);
     45       EXPECT_EQ(test_case.patterns[j], patterns[j]);
     46     }
     47   }
     48 }
     49 
     50 TEST(OnedCode128WriterTest, Encode128C) {
     51   char buf[100];
     52   TestCase kTestCases[] = {
     53       {"", 105, {105}, 1},
     54       {"a", 202, {105, 97}, 2},
     55       {"1", 106, {105, 1}, 2},
     56       {"a1", 204, {105, 97, 1}, 3},
     57       {"ab", 398, {105, 97, 98}, 3},
     58       {"12", 117, {105, 12}, 2},
     59       {"abc", 695, {105, 97, 98, 99}, 4},
     60       {"123", 123, {105, 12, 3}, 3},
     61       {"abc123", 758, {105, 97, 98, 99, 12, 3}, 6},
     62       {"ABC123", 566, {105, 65, 66, 67, 12, 3}, 6},
     63       {"321ABC", 933, {105, 32, 1, 65, 66, 67}, 6},
     64       {"XYZ", 641, {105, 88, 89, 90}, 4},
     65   };
     66   for (size_t i = 0; i < FX_ArraySize(kTestCases); ++i) {
     67     FXSYS_snprintf(buf, sizeof(buf) - 1, "Test case %zu", i);
     68     SCOPED_TRACE(buf);
     69     const TestCase& test_case = kTestCases[i];
     70     std::vector<int32_t> patterns;
     71     int32_t checksum =
     72         CBC_OnedCode128Writer::Encode128C(test_case.input, &patterns);
     73     EXPECT_EQ(test_case.checksum, checksum);
     74     ASSERT_EQ(test_case.num_patterns, patterns.size());
     75     for (size_t j = 0; j < patterns.size(); ++j) {
     76       FXSYS_snprintf(buf, sizeof(buf) - 1, "Comparison %zu", j);
     77       SCOPED_TRACE(buf);
     78       EXPECT_EQ(test_case.patterns[j], patterns[j]);
     79     }
     80   }
     81 }
     82 
     83 TEST(OnedCode128WriterTest, CheckContentValidity) {
     84   {
     85     CBC_OnedCode128Writer writer(BC_CODE128_B);
     86     EXPECT_TRUE(writer.CheckContentValidity(L""));
     87     EXPECT_TRUE(writer.CheckContentValidity(L"foo"));
     88     EXPECT_TRUE(writer.CheckContentValidity(L"xyz"));
     89     EXPECT_FALSE(writer.CheckContentValidity(L"\""));
     90     EXPECT_FALSE(writer.CheckContentValidity(L"f\x10oo"));
     91     EXPECT_FALSE(writer.CheckContentValidity(L"bar\x7F"));
     92     EXPECT_FALSE(writer.CheckContentValidity(L"qux\x88"));
     93   }
     94   {
     95     CBC_OnedCode128Writer writer(BC_CODE128_C);
     96     EXPECT_TRUE(writer.CheckContentValidity(L""));
     97     EXPECT_TRUE(writer.CheckContentValidity(L"foo"));
     98     EXPECT_TRUE(writer.CheckContentValidity(L"xyz"));
     99     EXPECT_FALSE(writer.CheckContentValidity(L"\""));
    100     EXPECT_FALSE(writer.CheckContentValidity(L"f\x10oo"));
    101     EXPECT_FALSE(writer.CheckContentValidity(L"bar\x7F"));
    102     EXPECT_FALSE(writer.CheckContentValidity(L"qux\x88"));
    103   }
    104 }
    105 
    106 TEST(OnedCode128WriterTest, FilterContents) {
    107   {
    108     CBC_OnedCode128Writer writer(BC_CODE128_B);
    109     EXPECT_STREQ(L"", writer.FilterContents(L"").c_str());
    110     EXPECT_STREQ(L"foo", writer.FilterContents(L"foo\x10").c_str());
    111     EXPECT_STREQ(L"fool", writer.FilterContents(L"foo\x10l").c_str());
    112     EXPECT_STREQ(L"foo", writer.FilterContents(L"foo\x10\x7F").c_str());
    113     EXPECT_STREQ(L"foo", writer.FilterContents(L"foo\x10\x7F\x88").c_str());
    114     EXPECT_STREQ(L"bar", writer.FilterContents(L"bar\x10\x7F\x88").c_str());
    115   }
    116   {
    117     CBC_OnedCode128Writer writer(BC_CODE128_C);
    118     EXPECT_STREQ(L"", writer.FilterContents(L"").c_str());
    119     EXPECT_STREQ(L"f", writer.FilterContents(L"foo\x10").c_str());
    120     EXPECT_STREQ(L"f", writer.FilterContents(L"foo\x10l").c_str());
    121     EXPECT_STREQ(L"f", writer.FilterContents(L"foo\x10\x7F").c_str());
    122     EXPECT_STREQ(L"f", writer.FilterContents(L"foo\x10\x7F\x88").c_str());
    123     EXPECT_STREQ(L"ba", writer.FilterContents(L"bar\x10\x7F\x88").c_str());
    124   }
    125 }
    126 
    127 }  // namespace
    128