aboutsummaryrefslogtreecommitdiffstats
path: root/util/string/util_ut.cpp
blob: 2d9f5f357943c2b3beacf79f6de250a7588c5cab (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
#include "util.h"

#include <library/cpp/testing/unittest/registar.h>

class TStrUtilTest: public TTestBase {
    UNIT_TEST_SUITE(TStrUtilTest);
    UNIT_TEST(TestSpn);
    UNIT_TEST(TestRemoveAll);
    UNIT_TEST_SUITE_END();

public:
    void TestSpn() {
        str_spn rul("a-z", true);
        char s[] = "!@#$ab%^&c+-";
        UNIT_ASSERT_EQUAL(rul.brk(s), s + 4);
        UNIT_ASSERT_EQUAL(rul.brk(s + 4), s + 4);
        UNIT_ASSERT_EQUAL(rul.brk(s + 10), s + 12);
        char* s1 = s;
        UNIT_ASSERT_EQUAL(strcmp(rul.sep(s1), "!@#$"), 0);
        UNIT_ASSERT_EQUAL(strcmp(rul.sep(s1), ""), 0);
        UNIT_ASSERT_EQUAL(strcmp(rul.sep(s1), "%^&"), 0);
        UNIT_ASSERT_EQUAL(strcmp(rul.sep(s1), "+-"), 0);
        UNIT_ASSERT_EQUAL(rul.sep(s1), nullptr);
    }

    void TestRemoveAll() {
        static const struct T {
            const char* Str;
            char Ch;
            const char* Result;
        } tests[] = {
            {"", 'x', ""},
            {"hello world", 'h', "ello world"},
            {"hello world", 'l', "heo word"},
            {"hello world", 'x', "hello world"},
        };

        for (const T* t = tests; t != std::end(tests); ++t) {
            TString str(t->Str); 
            RemoveAll(str, t->Ch);
            UNIT_ASSERT_EQUAL(t->Result, str);
        }
    }
};

UNIT_TEST_SUITE_REGISTRATION(TStrUtilTest);