GTINTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright 2022 gitlost
  3. */
  4. // SPDX-License-Identifier: Apache-2.0
  5. #include "GTIN.h"
  6. #include "gtest/gtest.h"
  7. using namespace ZXing;
  8. using namespace ZXing::GTIN;
  9. TEST(GTINTest, CountryIdentifierEAN13)
  10. {
  11. // From test/samples/ean13-*/
  12. EXPECT_EQ(LookupCountryIdentifier("8413000065504"), "ES");
  13. EXPECT_EQ(LookupCountryIdentifier("8413000065504 12"), "ES");
  14. EXPECT_EQ(LookupCountryIdentifier("8413000065504 51299"), "ES");
  15. EXPECT_EQ(LookupCountryIdentifier("5449000039231"), "BE");
  16. EXPECT_TRUE(LookupCountryIdentifier("9788430532674").empty()); // Bookland (ISBN)
  17. EXPECT_EQ(LookupCountryIdentifier("8480017507990"), "ES");
  18. EXPECT_EQ(LookupCountryIdentifier("3166298099809"), "FR");
  19. EXPECT_EQ(LookupCountryIdentifier("5201815331227"), "GR");
  20. EXPECT_EQ(LookupCountryIdentifier("3560070169443"), "FR");
  21. EXPECT_EQ(LookupCountryIdentifier("4045787034318"), "DE");
  22. EXPECT_EQ(LookupCountryIdentifier("3086126100326"), "FR");
  23. EXPECT_EQ(LookupCountryIdentifier("4820024790635"), "UA");
  24. EXPECT_EQ(LookupCountryIdentifier("7622200008018"), "CH");
  25. EXPECT_EQ(LookupCountryIdentifier("5603667020517"), "PT");
  26. EXPECT_EQ(LookupCountryIdentifier("5709262942503"), "DK");
  27. EXPECT_EQ(LookupCountryIdentifier("4901780188352"), "JP");
  28. EXPECT_EQ(LookupCountryIdentifier("4007817327098"), "DE");
  29. EXPECT_EQ(LookupCountryIdentifier("5025121072311"), "GB");
  30. EXPECT_EQ(LookupCountryIdentifier("5025121072311 12"), "GB");
  31. EXPECT_EQ(LookupCountryIdentifier("5025121072311 51299"), "GB");
  32. EXPECT_EQ(LookupCountryIdentifier("5030159003930"), "GB");
  33. EXPECT_EQ(LookupCountryIdentifier("5000213002834"), "GB");
  34. EXPECT_TRUE(LookupCountryIdentifier("1920081045006").empty()); // 140-199 unassigned
  35. EXPECT_TRUE(LookupCountryIdentifier("9780735200449 51299").empty()); // Bookland (ISBN)
  36. // Other
  37. EXPECT_TRUE(LookupCountryIdentifier("0000000001465").empty()); // 0000000 Restricted Circulation Numbers
  38. EXPECT_TRUE(LookupCountryIdentifier("0000000111461 12").empty());
  39. EXPECT_TRUE(LookupCountryIdentifier("0000001991469").empty()); // 0000001-0000099 unused to avoid GTIN-8 collision
  40. EXPECT_TRUE(LookupCountryIdentifier("0000099991463").empty());
  41. EXPECT_EQ(LookupCountryIdentifier("0000102301463"), "US"); // 00001-00009 US
  42. EXPECT_EQ(LookupCountryIdentifier("0000102301463 51299"), "US");
  43. EXPECT_EQ(LookupCountryIdentifier("0000902301465"), "US");
  44. EXPECT_EQ(LookupCountryIdentifier("0001602301465"), "US"); // 0001-0009 US
  45. EXPECT_EQ(LookupCountryIdentifier("0009602301461 12"), "US");
  46. EXPECT_EQ(LookupCountryIdentifier("0016602301469"), "US"); // 001-019 US
  47. EXPECT_EQ(LookupCountryIdentifier("0036602301467"), "US");
  48. EXPECT_EQ(LookupCountryIdentifier("0196602301468 51299"), "US");
  49. EXPECT_TRUE(LookupCountryIdentifier("0206602301464").empty()); // 020-029 Restricted Circulation Numbers
  50. EXPECT_TRUE(LookupCountryIdentifier("0296602301465").empty());
  51. EXPECT_EQ(LookupCountryIdentifier("0306602301461"), "US"); // 030-039 US
  52. EXPECT_EQ(LookupCountryIdentifier("0396602301462"), "US");
  53. EXPECT_TRUE(LookupCountryIdentifier("0406602301468").empty()); // 040-049 Restricted Circulation Numbers
  54. EXPECT_TRUE(LookupCountryIdentifier("0496602301469").empty());
  55. EXPECT_TRUE(LookupCountryIdentifier("0506602301465").empty()); // 050-059 reserved for future use
  56. EXPECT_TRUE(LookupCountryIdentifier("0596602301466").empty());
  57. EXPECT_EQ(LookupCountryIdentifier("0606602301462"), "US"); // 060-099 US
  58. EXPECT_EQ(LookupCountryIdentifier("0996602301464"), "US");
  59. EXPECT_EQ(LookupCountryIdentifier("1006602301469"), "US"); // 100-139 US
  60. EXPECT_EQ(LookupCountryIdentifier("1396602301461"), "US");
  61. EXPECT_TRUE(LookupCountryIdentifier("1406602301467").empty()); // 140-199 unassigned
  62. EXPECT_TRUE(LookupCountryIdentifier("1996602301463").empty());
  63. EXPECT_TRUE(LookupCountryIdentifier("2006602301468").empty()); // 200-299 Restricted Circulation Numbers
  64. EXPECT_TRUE(LookupCountryIdentifier("2996602301462").empty());
  65. EXPECT_EQ(LookupCountryIdentifier("9586602301468"), "MO");
  66. EXPECT_EQ(LookupCountryIdentifier("9586602301468 12"), "MO");
  67. EXPECT_EQ(LookupCountryIdentifier("9586602301468 51299"), "MO");
  68. // Additions/updates
  69. EXPECT_EQ(LookupCountryIdentifier("3890102301467"), "ME");
  70. //EXPECT_EQ(LookupCountryIdentifier("3900102301463"), "XK"); // Kosovo according to Wikipedia - awaiting GS1 confirmation
  71. EXPECT_EQ(LookupCountryIdentifier("4700102301468"), "KG");
  72. EXPECT_EQ(LookupCountryIdentifier("4830102301462"), "TM");
  73. EXPECT_EQ(LookupCountryIdentifier("4880102301467"), "TJ");
  74. EXPECT_EQ(LookupCountryIdentifier("5210102301461"), "GR");
  75. EXPECT_EQ(LookupCountryIdentifier("5300102301469"), "AL");
  76. EXPECT_EQ(LookupCountryIdentifier("6040102301463"), "SN");
  77. EXPECT_EQ(LookupCountryIdentifier("6150102301469"), "NG");
  78. EXPECT_EQ(LookupCountryIdentifier("6170102301467"), "CM");
  79. EXPECT_EQ(LookupCountryIdentifier("6200102301461"), "TZ");
  80. EXPECT_EQ(LookupCountryIdentifier("6230102301468"), "BN");
  81. EXPECT_EQ(LookupCountryIdentifier("6300102301468"), "QA");
  82. EXPECT_EQ(LookupCountryIdentifier("6310102301467"), "NA");
  83. EXPECT_EQ(LookupCountryIdentifier("6990102301461"), "CN");
  84. EXPECT_EQ(LookupCountryIdentifier("7710102301464"), "CO");
  85. EXPECT_EQ(LookupCountryIdentifier("7780102301467"), "AR");
  86. EXPECT_TRUE(LookupCountryIdentifier("7850102301467").empty());
  87. EXPECT_EQ(LookupCountryIdentifier("8600102301467"), "RS");
  88. EXPECT_EQ(LookupCountryIdentifier("8830102301468"), "MM");
  89. EXPECT_EQ(LookupCountryIdentifier("8840102301467"), "KH");
  90. EXPECT_EQ(LookupCountryIdentifier("9400102301462"), "NZ");
  91. }
  92. TEST(GTINTest, CountryIdentifierUPCA)
  93. {
  94. // From test/samples/upca-*/
  95. EXPECT_EQ(LookupCountryIdentifier("036602301467"), "US"); // 001-019 US/CA
  96. EXPECT_EQ(LookupCountryIdentifier("036602301467 12"), "US"); // 001-019 US
  97. EXPECT_EQ(LookupCountryIdentifier("036602301467 51299"), "US"); // 001-019 US
  98. EXPECT_EQ(LookupCountryIdentifier("070097025088"), "US");
  99. EXPECT_EQ(LookupCountryIdentifier("781735802045"), "US"); // 060-099 US
  100. EXPECT_TRUE(LookupCountryIdentifier("456314319671").empty()); // 040-049 Restricted Circulation Numbers
  101. EXPECT_TRUE(LookupCountryIdentifier("434704791429").empty());
  102. EXPECT_EQ(LookupCountryIdentifier("752919460009"), "US"); // 060-099 US
  103. EXPECT_EQ(LookupCountryIdentifier("606949762520"), "US");
  104. EXPECT_EQ(LookupCountryIdentifier("890444000335"), "US");
  105. EXPECT_EQ(LookupCountryIdentifier("181497000879"), "US"); // 001-019 US
  106. EXPECT_EQ(LookupCountryIdentifier("012546619592"), "US");
  107. EXPECT_EQ(LookupCountryIdentifier("854818000116"), "US"); // 060-099 US
  108. EXPECT_EQ(LookupCountryIdentifier("312547701310"), "US"); // 030-039 US
  109. EXPECT_EQ(LookupCountryIdentifier("071831007995 19868"), "US"); // 001-019 US
  110. EXPECT_EQ(LookupCountryIdentifier("027011006951 02601"), "US");
  111. EXPECT_EQ(LookupCountryIdentifier("024543136538 00"), "US");
  112. // Other
  113. EXPECT_TRUE(LookupCountryIdentifier("000000001465").empty()); // 0000000 Restricted Circulation Numbers
  114. EXPECT_TRUE(LookupCountryIdentifier("000000111461 12").empty());
  115. EXPECT_TRUE(LookupCountryIdentifier("000001991468").empty()); // 0000001-0000099 unused to avoid GTIN-8 collision
  116. EXPECT_TRUE(LookupCountryIdentifier("000099991463").empty());
  117. EXPECT_EQ(LookupCountryIdentifier("000102301463"), "US"); // 00001-00009 US
  118. EXPECT_EQ(LookupCountryIdentifier("000102301463 51299"), "US");
  119. EXPECT_EQ(LookupCountryIdentifier("000902301465"), "US");
  120. EXPECT_EQ(LookupCountryIdentifier("001602301465"), "US"); // 0001-0009 US
  121. EXPECT_EQ(LookupCountryIdentifier("009602301461 12"), "US");
  122. EXPECT_EQ(LookupCountryIdentifier("016602301469"), "US"); // 001-019 US
  123. EXPECT_EQ(LookupCountryIdentifier("036602301467"), "US");
  124. EXPECT_EQ(LookupCountryIdentifier("196602301468 51299"), "US");
  125. EXPECT_TRUE(LookupCountryIdentifier("206602301464").empty()); // 020-029 Restricted Circulation Numbers
  126. EXPECT_TRUE(LookupCountryIdentifier("296602301465").empty());
  127. EXPECT_EQ(LookupCountryIdentifier("306602301461"), "US"); // 030-039 US
  128. EXPECT_EQ(LookupCountryIdentifier("396602301462"), "US");
  129. EXPECT_TRUE(LookupCountryIdentifier("406602301468").empty()); // 040-049 Restricted Circulation Numbers
  130. EXPECT_TRUE(LookupCountryIdentifier("496602301469").empty());
  131. EXPECT_TRUE(LookupCountryIdentifier("506602301465").empty()); // 050-059 reserved for future use
  132. EXPECT_TRUE(LookupCountryIdentifier("596602301466").empty());
  133. EXPECT_EQ(LookupCountryIdentifier("606602301462"), "US"); // 060-099 US
  134. EXPECT_EQ(LookupCountryIdentifier("996602301464"), "US");
  135. }
  136. TEST(GTINTest, CountryIdentifierUPCE)
  137. {
  138. // From test/samples/upce-*/
  139. EXPECT_EQ(LookupCountryIdentifier("01234565"), "US"); // 001-019 US
  140. EXPECT_EQ(LookupCountryIdentifier("01234565", BarcodeFormat::UPCE), "US"); // 001-019 US
  141. EXPECT_EQ(LookupCountryIdentifier("00123457"), "US"); // 0001-0009 US
  142. EXPECT_EQ(LookupCountryIdentifier("00123457", BarcodeFormat::UPCE), "US"); // 0001-0009 US
  143. EXPECT_EQ(LookupCountryIdentifier("05096893"), "US"); // 001-019 US
  144. EXPECT_EQ(LookupCountryIdentifier("05096893", BarcodeFormat::UPCE), "US"); // 001-019 US
  145. EXPECT_EQ(LookupCountryIdentifier("04963406 01"), "US"); // 001-019 US
  146. EXPECT_EQ(LookupCountryIdentifier("04963406 01", BarcodeFormat::UPCE), "US"); // 001-019 US
  147. // Other
  148. // 0000000, 0000001-0000099 and 00001-00009 not possible for UPC-E
  149. EXPECT_EQ(LookupCountryIdentifier("00021357"), "US"); // 0001-0009 US
  150. EXPECT_EQ(LookupCountryIdentifier("00021357 01"), "US");
  151. EXPECT_EQ(LookupCountryIdentifier("11621355"), "US"); // 001-019 US
  152. EXPECT_TRUE(LookupCountryIdentifier("22221111").empty()); // 020-029 Restricted Circulation Numbers
  153. EXPECT_EQ(LookupCountryIdentifier("31621358"), "US"); // 030-039 US
  154. EXPECT_TRUE(LookupCountryIdentifier("40621359").empty()); // 040-049 Restricted Circulation Numbers
  155. EXPECT_TRUE(LookupCountryIdentifier("50621359").empty()); // 050-059 reserved for future use
  156. EXPECT_EQ(LookupCountryIdentifier("61621358"), "US"); // 060-099 US
  157. EXPECT_EQ(LookupCountryIdentifier("99621350"), "US");
  158. }
  159. TEST(GTINTest, CountryIdentifierEAN8)
  160. {
  161. auto format = BarcodeFormat::EAN8; // Require BarcodeFormat for EAN-8 to be distinguished from UPC-E
  162. // From test/samples/ean8-*/
  163. EXPECT_EQ(LookupCountryIdentifier("48512343", format), "AM");
  164. EXPECT_EQ(LookupCountryIdentifier("12345670", format), "US");
  165. EXPECT_TRUE(LookupCountryIdentifier("67678983", format).empty()); // 650-689 unassigned
  166. EXPECT_EQ(LookupCountryIdentifier("80674313", format), "IT");
  167. EXPECT_EQ(LookupCountryIdentifier("59001270", format), "PL");
  168. EXPECT_EQ(LookupCountryIdentifier("50487066", format), "GB");
  169. EXPECT_TRUE(LookupCountryIdentifier("55123457", format).empty()); // 550-559 unassigned
  170. EXPECT_TRUE(LookupCountryIdentifier("95012346", format).empty()); // 950 GS1 Global Office
  171. // Other (GS1 General Specifications 1.4.3 Figure 1.4.3-1
  172. EXPECT_TRUE(LookupCountryIdentifier("00045674", format).empty()); // 000-099 EAN-8 Restricted Circulation Numbers
  173. EXPECT_TRUE(LookupCountryIdentifier("09945678", format).empty());
  174. EXPECT_EQ(LookupCountryIdentifier("10045671", format), "US"); // 100-139 US
  175. EXPECT_EQ(LookupCountryIdentifier("13945671", format), "US");
  176. EXPECT_TRUE(LookupCountryIdentifier("14045677", format).empty()); // 140-199 unassigned
  177. EXPECT_TRUE(LookupCountryIdentifier("19945675", format).empty());
  178. EXPECT_TRUE(LookupCountryIdentifier("20045678", format).empty()); // 200-299 Restricted Circulation Numbers
  179. EXPECT_TRUE(LookupCountryIdentifier("29945672", format).empty());
  180. EXPECT_EQ(LookupCountryIdentifier("30045675", format), "FR");
  181. EXPECT_EQ(LookupCountryIdentifier("95845678", format), "MO");
  182. EXPECT_TRUE(LookupCountryIdentifier("97645672", format).empty()); // Unassigned
  183. EXPECT_TRUE(LookupCountryIdentifier("97745679", format).empty()); // 977-999 Reserved for future use
  184. EXPECT_TRUE(LookupCountryIdentifier("99945671", format).empty());
  185. }
  186. TEST(GTINTest, CountryIdentifierGTIN14)
  187. {
  188. // From test/samples/itf-*/
  189. EXPECT_EQ(LookupCountryIdentifier("30712345000010"), "US");
  190. EXPECT_EQ(LookupCountryIdentifier("00012345678905"), "US");
  191. // Other
  192. EXPECT_TRUE(LookupCountryIdentifier("12345678901231").empty()); // 200-299 Restricted Circulation Numbers
  193. EXPECT_EQ(LookupCountryIdentifier("13005678901233"), "FR");
  194. }