Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "utils/checksum.h"
     18 #include "gtest/gtest.h"
     19 
     20 namespace libtextclassifier3 {
     21 namespace {
     22 
     23 TEST(LuhnTest, CorrectlyHandlesSimpleCases) {
     24   EXPECT_TRUE(VerifyLuhnChecksum("3782 8224 6310 005"));
     25   EXPECT_FALSE(VerifyLuhnChecksum("0"));
     26   EXPECT_FALSE(VerifyLuhnChecksum("1"));
     27   EXPECT_FALSE(VerifyLuhnChecksum("0A"));
     28 }
     29 
     30 TEST(LuhnTest, CorrectlyVerifiesPaymentCardNumbers) {
     31   // Fake test numbers.
     32   EXPECT_TRUE(VerifyLuhnChecksum("3782 8224 6310 005"));
     33   EXPECT_TRUE(VerifyLuhnChecksum("371449635398431"));
     34   EXPECT_TRUE(VerifyLuhnChecksum("5610591081018250"));
     35   EXPECT_TRUE(VerifyLuhnChecksum("38520000023237"));
     36   EXPECT_TRUE(VerifyLuhnChecksum("6011000990139424"));
     37   EXPECT_TRUE(VerifyLuhnChecksum("3566002020360505"));
     38   EXPECT_TRUE(VerifyLuhnChecksum("5105105105105100"));
     39   EXPECT_TRUE(VerifyLuhnChecksum("4012 8888 8888 1881"));
     40 }
     41 
     42 TEST(LuhnTest, HandlesWhitespace) {
     43   EXPECT_TRUE(
     44       VerifyLuhnChecksum("3782 8224 6310 005 ", /*ignore_whitespace=*/true));
     45   EXPECT_FALSE(
     46       VerifyLuhnChecksum("3782 8224 6310 005 ", /*ignore_whitespace=*/false));
     47 }
     48 
     49 TEST(LuhnTest, HandlesEdgeCases) {
     50   EXPECT_FALSE(VerifyLuhnChecksum("    ", /*ignore_whitespace=*/true));
     51   EXPECT_FALSE(VerifyLuhnChecksum("    ", /*ignore_whitespace=*/false));
     52   EXPECT_FALSE(VerifyLuhnChecksum("", /*ignore_whitespace=*/true));
     53   EXPECT_FALSE(VerifyLuhnChecksum("", /*ignore_whitespace=*/false));
     54 }
     55 
     56 }  // namespace
     57 }  // namespace libtextclassifier3
     58