susumu.yata
null+****@clear*****
Fri Sep 26 13:39:18 JST 2014
susumu.yata 2014-09-26 13:39:18 +0900 (Fri, 26 Sep 2014) New Revision: 01b7b3b052bcd1d87a8cfbd20df297d9d7fe6e0c https://github.com/groonga/grnxx/commit/01b7b3b052bcd1d87a8cfbd20df297d9d7fe6e0c Message: Add tests for StringCRef. (#75) Added files: test/test_string.cpp Modified files: .gitignore test/Makefile.am Modified: .gitignore (+1 -0) =================================================================== --- .gitignore 2014-09-26 13:30:35 +0900 (4d601d6) +++ .gitignore 2014-09-26 13:39:18 +0900 (4f51e78) @@ -32,6 +32,7 @@ oprofile_data/ src/grnxx stamp-h1 test/*.trs +test/test_string test/test_array test/test_db test/test_table Modified: test/Makefile.am (+4 -0) =================================================================== --- test/Makefile.am 2014-09-26 13:30:35 +0900 (6eeac8a) +++ test/Makefile.am 2014-09-26 13:39:18 +0900 (80a8c0e) @@ -1,4 +1,5 @@ TESTS = \ + test_string \ test_array \ test_db \ test_table \ @@ -12,6 +13,9 @@ TESTS = \ check_PROGRAMS = $(TESTS) +test_string_SOURCES = test_string.cpp +test_string_LDADD = $(top_srcdir)/lib/grnxx/libgrnxx.la + test_array_SOURCES = test_array.cpp test_array_LDADD = $(top_srcdir)/lib/grnxx/libgrnxx.la Added: test/test_string.cpp (+91 -0) 100644 =================================================================== --- /dev/null +++ test/test_string.cpp 2014-09-26 13:39:18 +0900 (dc2655b) @@ -0,0 +1,91 @@ +/* + Copyright (C) 2012-2014 Brazil, Inc. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include <algorithm> +#include <cassert> +#include <iostream> +#include <string> +#include <sstream> +#include <vector> + +#include "grnxx/types.hpp" + +bool string_starts_with(const std::string &lhs, const std::string &rhs) { + if (lhs.size() < rhs.size()) { + return false; + } + return lhs.compare(0, rhs.size(), rhs) == 0; +} + +bool string_ends_with(const std::string &lhs, const std::string &rhs) { + if (lhs.size() < rhs.size()) { + return false; + } + return lhs.compare(lhs.size() - rhs.size(), rhs.size(), rhs) == 0; +} + +void test_string_cref() { + constexpr grnxx::Int NUM_STRINGS = 1000; + + std::vector<std::string> strings(NUM_STRINGS); + std::vector<grnxx::StringCRef> refs(NUM_STRINGS); + for (grnxx::Int i = 0; i < NUM_STRINGS; ++i) { + std::stringstream stream; + stream << i; + strings[i] = stream.str(); + refs[i] = grnxx::StringCRef(strings[i].data(), strings[i].size()); + } + + for (grnxx::Int i = 0; i < NUM_STRINGS; ++i) { + for (grnxx::Int j = 0; j < NUM_STRINGS; ++j) { + assert((refs[i] == refs[j]) == (strings[i] == strings[j])); + assert((refs[i] != refs[j]) == (strings[i] != strings[j])); + assert((refs[i] < refs[j]) == (strings[i] < strings[j])); + assert((refs[i] > refs[j]) == (strings[i] > strings[j])); + assert((refs[i] <= refs[j]) == (strings[i] <= strings[j])); + assert((refs[i] >= refs[j]) == (strings[i] >= strings[j])); + + assert((refs[i] == strings[j].c_str()) == (strings[i] == strings[j])); + assert((refs[i] != strings[j].c_str()) == (strings[i] != strings[j])); + assert((refs[i] < strings[j].c_str()) == (strings[i] < strings[j])); + assert((refs[i] > strings[j].c_str()) == (strings[i] > strings[j])); + assert((refs[i] <= strings[j].c_str()) == (strings[i] <= strings[j])); + assert((refs[i] >= strings[j].c_str()) == (strings[i] >= strings[j])); + + assert((strings[i].c_str() == refs[j]) == (strings[i] == strings[j])); + assert((strings[i].c_str() != refs[j]) == (strings[i] != strings[j])); + assert((strings[i].c_str() < refs[j]) == (strings[i] < strings[j])); + assert((strings[i].c_str() > refs[j]) == (strings[i] > strings[j])); + assert((strings[i].c_str() <= refs[j]) == (strings[i] <= strings[j])); + assert((strings[i].c_str() >= refs[j]) == (strings[i] >= strings[j])); + + assert(refs[i].starts_with(refs[j]) == + string_starts_with(strings[i], strings[j])); + assert(refs[i].starts_with(strings[j].c_str()) == + string_starts_with(strings[i], strings[j])); + assert(refs[i].ends_with(refs[j]) == + string_ends_with(strings[i], strings[j])); + assert(refs[i].ends_with(strings[j].c_str()) == + string_ends_with(strings[i], strings[j])); + } + } +} + +int main() { + test_string_cref(); + return 0; +} -------------- next part -------------- HTML����������������������������... 下载