blob: 85d0c3cdba4242983e15c92c858f896031f6556b [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/stringutils.h"
12#include "rtc_base/gunit.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
14namespace rtc {
15
16// Tests for string_match().
17
18TEST(string_matchTest, Matches) {
19 EXPECT_TRUE( string_match("A.B.C.D", "a.b.c.d"));
20 EXPECT_TRUE( string_match("www.TEST.GOOGLE.COM", "www.*.com"));
21 EXPECT_TRUE( string_match("127.0.0.1", "12*.0.*1"));
22 EXPECT_TRUE( string_match("127.1.0.21", "12*.0.*1"));
23 EXPECT_FALSE(string_match("127.0.0.0", "12*.0.*1"));
24 EXPECT_FALSE(string_match("127.0.0.0", "12*.0.*1"));
25 EXPECT_FALSE(string_match("127.1.1.21", "12*.0.*1"));
26}
27
28// It's not clear if we will ever use wchar_t strings on unix. In theory,
29// all strings should be Utf8 all the time, except when interfacing with Win32
30// APIs that require Utf16.
31
32#if defined(WEBRTC_WIN)
33
34// Tests for ascii_string_compare().
35
deadbeef37f5ecf2017-02-27 14:06:41 -080036// Tests null input.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037TEST(ascii_string_compareTest, NullInput) {
38 // The following results in an access violation in
39 // ascii_string_compare. Is this a bug or by design? stringutils.h
40 // should document the expected behavior in this case.
41
deadbeef37f5ecf2017-02-27 14:06:41 -080042 // EXPECT_EQ(0, ascii_string_compare(nullptr, nullptr, 1, identity));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043}
44
45// Tests comparing two strings of different lengths.
46TEST(ascii_string_compareTest, DifferentLengths) {
47 EXPECT_EQ(-1, ascii_string_compare(L"Test", "Test1", 5, identity));
48}
49
50// Tests the case where the buffer size is smaller than the string
51// lengths.
52TEST(ascii_string_compareTest, SmallBuffer) {
53 EXPECT_EQ(0, ascii_string_compare(L"Test", "Test1", 3, identity));
54}
55
56// Tests the case where the buffer is not full.
57TEST(ascii_string_compareTest, LargeBuffer) {
58 EXPECT_EQ(0, ascii_string_compare(L"Test", "Test", 10, identity));
59}
60
61// Tests comparing two eqaul strings.
62TEST(ascii_string_compareTest, Equal) {
63 EXPECT_EQ(0, ascii_string_compare(L"Test", "Test", 5, identity));
64 EXPECT_EQ(0, ascii_string_compare(L"TeSt", "tEsT", 5, tolowercase));
65}
66
67// Tests comparing a smller string to a larger one.
68TEST(ascii_string_compareTest, LessThan) {
69 EXPECT_EQ(-1, ascii_string_compare(L"abc", "abd", 4, identity));
70 EXPECT_EQ(-1, ascii_string_compare(L"ABC", "abD", 5, tolowercase));
71}
72
73// Tests comparing a larger string to a smaller one.
74TEST(ascii_string_compareTest, GreaterThan) {
75 EXPECT_EQ(1, ascii_string_compare(L"xyz", "xy", 5, identity));
76 EXPECT_EQ(1, ascii_string_compare(L"abc", "ABB", 5, tolowercase));
77}
78#endif // WEBRTC_WIN
79
80TEST(string_trim_Test, Trimming) {
81 EXPECT_EQ("temp", string_trim("\n\r\t temp \n\r\t"));
82 EXPECT_EQ("temp\n\r\t temp", string_trim(" temp\n\r\t temp "));
83 EXPECT_EQ("temp temp", string_trim("temp temp"));
84 EXPECT_EQ("", string_trim(" \r\n\t"));
85 EXPECT_EQ("", string_trim(""));
86}
87
88TEST(string_startsTest, StartsWith) {
89 EXPECT_TRUE(starts_with("foobar", "foo"));
90 EXPECT_TRUE(starts_with("foobar", "foobar"));
91 EXPECT_TRUE(starts_with("foobar", ""));
92 EXPECT_TRUE(starts_with("", ""));
93 EXPECT_FALSE(starts_with("foobar", "bar"));
94 EXPECT_FALSE(starts_with("foobar", "foobarbaz"));
95 EXPECT_FALSE(starts_with("", "f"));
96}
97
98TEST(string_endsTest, EndsWith) {
99 EXPECT_TRUE(ends_with("foobar", "bar"));
100 EXPECT_TRUE(ends_with("foobar", "foobar"));
101 EXPECT_TRUE(ends_with("foobar", ""));
102 EXPECT_TRUE(ends_with("", ""));
103 EXPECT_FALSE(ends_with("foobar", "foo"));
104 EXPECT_FALSE(ends_with("foobar", "foobarbaz"));
105 EXPECT_FALSE(ends_with("", "f"));
106}
107
108} // namespace rtc