Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-string.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2011 Brazil
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License version 2.1 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include <gcutter.h>
20 #include <cppcutter.h>
21 
22 #include <grn-assertions.h>
23 #include <dat/string.hpp>
24 
25 #include "test-string.hpp"
26 
27 namespace test_dat_string
28 {
29  void test_empty_string(void)
30  {
31  const grn::dat::String str;
32 
33  cut_assert_null(str.ptr());
34  cppcut_assert_equal(grn::dat::UInt32(0), str.length());
35  }
36 
38  {
39  const char str_buf[] = "日本語";
40  const grn::dat::String str(str_buf, sizeof(str_buf) - 1);
41 
42  cppcut_assert_equal(static_cast<const void *>(str_buf), str.ptr());
43  cppcut_assert_equal(static_cast<grn::dat::UInt32>(sizeof(str_buf) - 1),
44  str.length());
45  }
46 
48  {
49  const char str_buf[] = "日本語";
50  const grn::dat::String str(str_buf);
51 
52  cppcut_assert_equal(static_cast<const void *>(str_buf), str.ptr());
53  cppcut_assert_equal(static_cast<grn::dat::UInt32>(sizeof(str_buf) - 1),
54  str.length());
55  }
56 
58  {
59  const char str_buf[] = "日本語";
60  const grn::dat::String str_origin(str_buf);
61  const grn::dat::String str_copy(str_origin);
62 
63  cppcut_assert_equal(str_copy.ptr(), str_origin.ptr());
64  cppcut_assert_equal(str_copy.length(), str_origin.length());
65  }
66 
67  void test_index_access(void)
68  {
69  const char str_buf[] = "日本語";
70  const grn::dat::String str(str_buf);
71 
72  for (grn::dat::UInt32 i = 0; i < str.length(); ++i) {
73  cppcut_assert_equal(static_cast<grn::dat::UInt8>(str_buf[i]), str[i]);
74  }
75  }
76 
77  void test_assign(void)
78  {
79  const char str_buf[] = "日本語";
80 
81  grn::dat::String str;
82  str.assign(str_buf, sizeof(str_buf) - 1);
83 
84  cppcut_assert_equal(static_cast<const void *>(str_buf), str.ptr());
85  cppcut_assert_equal(static_cast<grn::dat::UInt32>(sizeof(str_buf) - 1),
86  str.length());
87  }
88 
89  void test_substr(void)
90  {
91  const grn::dat::String str("apple");
92 
93  cppcut_assert_equal(grn::dat::String("le"), str.substr(3));
94  cppcut_assert_equal(grn::dat::String("app"), str.substr(0, 3));
95  cppcut_assert_equal(grn::dat::String("ppl"), str.substr(1, 3));
96  }
97 
98  void test_compare(void)
99  {
100  const grn::dat::String str("apple");
101 
102  cppcut_assert_equal(0, str.compare(grn::dat::String("apple")));
103  cppcut_assert_operator(str.compare(grn::dat::String("appl")), >, 0);
104  cppcut_assert_operator(str.compare(grn::dat::String("appleX")), <, 0);
105  cppcut_assert_operator(str.compare(grn::dat::String("banana")), <, 0);
106  cppcut_assert_operator(str.compare(grn::dat::String("and")), >, 0);
107  }
108 
109  void test_starts_with(void)
110  {
111  const grn::dat::String str("apple");
112 
113  cppcut_assert_equal(true, str.starts_with(grn::dat::String("")));
114  cppcut_assert_equal(true, str.starts_with(grn::dat::String("app")));
115  cppcut_assert_equal(true, str.starts_with(grn::dat::String("apple")));
116  cppcut_assert_equal(false, str.starts_with(grn::dat::String("X")));
117  }
118 
119  void test_ends_with(void)
120  {
121  const grn::dat::String str("apple");
122 
123  cppcut_assert_equal(true, str.ends_with(grn::dat::String("")));
124  cppcut_assert_equal(true, str.ends_with(grn::dat::String("ple")));
125  cppcut_assert_equal(true, str.ends_with(grn::dat::String("apple")));
126  cppcut_assert_equal(false, str.ends_with(grn::dat::String("X")));
127  }
128 }