aboutsummaryrefslogtreecommitdiffstats
path: root/util/string/ascii_ut.cpp
blob: 41083026221774c3e766a9eed9c9599c08f49baa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "ascii.h" 
#include <ctype.h> 
 
#include <library/cpp/testing/unittest/registar.h>
 
Y_UNIT_TEST_SUITE(TAsciiTest) {
    Y_UNIT_TEST(TestAscii) {
        UNIT_ASSERT(IsAsciiDigit('3')); 
        UNIT_ASSERT(!IsAsciiDigit('x')); 
 
        UNIT_ASSERT(IsAsciiAlpha('r')); 
        UNIT_ASSERT(IsAsciiAlpha('R')); 
        UNIT_ASSERT(!IsAsciiAlpha('3')); 
 
        UNIT_ASSERT_EQUAL(AsciiToLower('3'), '3'); 
        UNIT_ASSERT_EQUAL(AsciiToLower('A'), 'a'); 
        UNIT_ASSERT_EQUAL(AsciiToLower('a'), 'a'); 
 
        UNIT_ASSERT_EQUAL(AsciiToUpper('3'), '3'); 
        UNIT_ASSERT_EQUAL(AsciiToUpper('A'), 'A'); 
        UNIT_ASSERT_EQUAL(AsciiToUpper('a'), 'A'); 
 
        UNIT_ASSERT(IsAscii('a')); 
        UNIT_ASSERT(!IsAscii(-100)); 
        UNIT_ASSERT(!IsAscii(+200)); 
        UNIT_ASSERT(!IsAscii(int('a') + 256)); 
 
        for (int i = 0; i < 128; ++i) { 
            UNIT_ASSERT_VALUES_EQUAL((bool)isxdigit(i), IsAsciiHex(i)); 
            UNIT_ASSERT_VALUES_EQUAL((bool)isspace(i), IsAsciiSpace((char)i)); 
            UNIT_ASSERT_VALUES_EQUAL((bool)isspace(i), IsAsciiSpace((char)i)); 
            UNIT_ASSERT_VALUES_EQUAL((bool)isalnum(i), IsAsciiAlnum((char)i)); 
            UNIT_ASSERT_VALUES_EQUAL((bool)isalpha(i), IsAsciiAlpha((char)i)); 
            UNIT_ASSERT_VALUES_EQUAL((bool)isupper(i), IsAsciiUpper((char)i)); 
            UNIT_ASSERT_VALUES_EQUAL((bool)islower(i), IsAsciiLower((char)i)); 
            UNIT_ASSERT_VALUES_EQUAL((bool)isdigit(i), IsAsciiDigit((char)i)); 
            UNIT_ASSERT_VALUES_EQUAL((bool)ispunct(i), IsAsciiPunct((char)i));
        } 
    } 

    Y_UNIT_TEST(Test1) {
        for (int i = 128; i < 1000; ++i) {
            UNIT_ASSERT(!IsAsciiHex(i));
            UNIT_ASSERT(!IsAsciiSpace(i));
            UNIT_ASSERT(!IsAsciiAlnum(i));
            UNIT_ASSERT(!IsAsciiAlpha(i));
            UNIT_ASSERT(!IsAsciiUpper(i));
            UNIT_ASSERT(!IsAsciiLower(i));
            UNIT_ASSERT(!IsAsciiDigit(i));
            UNIT_ASSERT(!IsAsciiPunct(i));
        }

        for (int i = -1000; i < 0; ++i) {
            UNIT_ASSERT(!IsAsciiHex(i));
            UNIT_ASSERT(!IsAsciiSpace(i));
            UNIT_ASSERT(!IsAsciiAlnum(i));
            UNIT_ASSERT(!IsAsciiAlpha(i));
            UNIT_ASSERT(!IsAsciiUpper(i));
            UNIT_ASSERT(!IsAsciiLower(i));
            UNIT_ASSERT(!IsAsciiDigit(i));
            UNIT_ASSERT(!IsAsciiPunct(i));
        }
    }

    Y_UNIT_TEST(CompareTest) {
        UNIT_ASSERT(AsciiEqualsIgnoreCase("qqq", "qQq"));
        UNIT_ASSERT(AsciiEqualsIgnoreCase("qqq", TStringBuf("qQq")));
        TString qq = "qq";
        TString qQ = "qQ";
        UNIT_ASSERT(AsciiEqualsIgnoreCase(qq, qQ));

        TString x = "qqqA";
        TString y = "qQqB";
        TString z = "qQnB";
        TString zz = "qQqq";
        TString zzz = "qQqqq";
        TStringBuf xs = TStringBuf(x.data(), 3);
        TStringBuf ys = TStringBuf(y.data(), 3);
        TStringBuf zs = TStringBuf(z.data(), 3);
        UNIT_ASSERT(AsciiCompareIgnoreCase(xs, ys) == 0);
        UNIT_ASSERT(AsciiCompareIgnoreCase(xs, zs) > 0);
        UNIT_ASSERT(AsciiCompareIgnoreCase(xs, zz) < 0);
        UNIT_ASSERT(AsciiCompareIgnoreCase(zzz, zz) > 0);

        UNIT_ASSERT(AsciiCompareIgnoreCase("qqQ", "qq") > 0);
        UNIT_ASSERT(AsciiCompareIgnoreCase("qq", "qq") == 0);

        UNIT_ASSERT_EQUAL(AsciiHasPrefix("qweasd", "qwe"), true);
        UNIT_ASSERT_EQUAL(AsciiHasPrefix("qweasd", "qWe"), false);
        UNIT_ASSERT_EQUAL(AsciiHasPrefix("qweasd", "eWq"), false);

        UNIT_ASSERT_EQUAL(AsciiHasPrefixIgnoreCase("qweasd", "qWe"), true);
        UNIT_ASSERT_EQUAL(AsciiHasPrefixIgnoreCase("qweasd", "eWq"), false);

        UNIT_ASSERT_EQUAL(AsciiHasSuffixIgnoreCase("qweasd", "asD"), true);
        UNIT_ASSERT_EQUAL(AsciiHasSuffixIgnoreCase("qweasd", "ast"), false);
    }
}