diff options
author | Niko Catania <niko@google.com> | 2009-04-16 22:15:27 -0700 |
---|---|---|
committer | Nicolas Catania <niko@google.com> | 2009-05-02 20:09:39 -0700 |
commit | fe47ae56a8808c836923466e44704db3a8371593 (patch) | |
tree | 1d9e5eca07ca40c8fb814e4ffcaafe72f52d7a25 /tests | |
parent | 62c1471ed539420b4d48118e2d0b6bf153814629 (diff) | |
download | astl-fe47ae56a8808c836923466e44704db3a8371593.tar.gz |
Added 2 new files: algorithm and string.
Added tests but will submit change to runtest_py separately.
Added more description to the README file.
Check for unsigned overflows.
Added Android.mk in the top dir.
Removed the reserve() method and made the size default to 0 in reserve(size_t)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Android.mk | 74 | ||||
-rw-r--r-- | tests/macros.h | 53 | ||||
-rw-r--r-- | tests/test_algorithm.cpp | 81 | ||||
-rw-r--r-- | tests/test_string.cpp | 709 |
4 files changed, 917 insertions, 0 deletions
diff --git a/tests/Android.mk b/tests/Android.mk new file mode 100644 index 0000000..8f43a25 --- /dev/null +++ b/tests/Android.mk @@ -0,0 +1,74 @@ +# Copyright (C) 2009 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Build control file for Bionic's test programs +# define the BIONIC_TESTS environment variable to build the test programs +# + +# define the ASTL_TESTS environment variable to build the test programs +ifdef ASTL_TESTS + +LOCAL_PATH := $(call my-dir) + +# used to define a simple test program and build it as a standalone +# device executable. +# +# you can use EXTRA_CFLAGS to indicate additional CFLAGS to use +# in the build. the variable will be cleaned on exit +# +define device-test + $(foreach file,$(1), \ + $(eval include $(CLEAR_VARS)) \ + $(eval LOCAL_SRC_FILES := $(file)) \ + $(eval LOCAL_MODULE := $(notdir $(file:%.cpp=%))) \ + $(eval $(info LOCAL_MODULE=$(LOCAL_MODULE))) \ + $(eval LOCAL_CFLAGS += $(EXTRA_CFLAGS)) \ + $(eval LOCAL_MODULE_TAGS := tests) \ + $(eval LOCAL_STATIC_LIBRARIES := libastl) \ + $(eval include $(BUILD_EXECUTABLE)) \ + ) \ + $(eval EXTRA_CFLAGS :=) +endef + +# same as 'device-test' but builds a host executable instead +# you can use EXTRA_LDLIBS to indicate additional linker flags +# +define host-test + $(foreach file,$(1), \ + $(eval include $(CLEAR_VARS)) \ + $(eval LOCAL_SRC_FILES := $(file)) \ + $(eval LOCAL_MODULE := $(notdir $(file:%.cpp=%))) \ + $(eval $(info LOCAL_MODULE=$(LOCAL_MODULE) file=$(file))) \ + $(eval LOCAL_CFLAGS += $(EXTRA_CFLAGS)) \ + $(eval LOCAL_LDLIBS += $(EXTRA_LDLIBS)) \ + $(eval LOCAL_MODULE_TAGS := eng tests) \ + $(eval LOCAL_STATIC_LIBRARIES := libastl) \ + $(eval include $(BUILD_HOST_EXECUTABLE)) \ + ) \ + $(eval EXTRA_CFLAGS :=) \ + $(eval EXTRA_LDLIBS :=) +endef + +sources := \ + test_algorithm.cpp \ + test_string.cpp + +# Disable all optimization for the host target to help test tools (valgrind...) +EXTRA_CFLAGS := -I bionic/libstdc++/include -I external/astl/include -g -O0 +$(call host-test, $(sources)) + +EXTRA_CFLAGS := -I bionic/libstdc++/include -I external/astl/include +$(call device-test, $(sources)) + +endif #ASTL_TESTS diff --git a/tests/macros.h b/tests/macros.h new file mode 100644 index 0000000..1ef577f --- /dev/null +++ b/tests/macros.h @@ -0,0 +1,53 @@ +/* -*- c++ -*- */ +/* + * Copyright (C) 2009 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef ANDROID_ASTL_TESTS_MACROS__ +#define ANDROID_ASTL_TESTS_MACROS__ + +// Common macros for tests. +#include <cstdio> + +namespace { +const int kPassed = 0; +const int kFailed = 1; +#define FAIL_UNLESS(v) if (!android::v()) return kFailed; + +#define EXPECT_TRUE(expr) \ + if (!(expr)) { \ + std::fprintf(stderr, "%d: %s\n", __LINE__, #expr); \ + return false; \ + } + +#ifndef ARRAYSIZE +#define ARRAYSIZE(array) (sizeof(array)/sizeof(array[0])) +#endif + +} // anonymous namespace + +#endif // ANDROID_ASTL_TEST_MACROS__ diff --git a/tests/test_algorithm.cpp b/tests/test_algorithm.cpp new file mode 100644 index 0000000..a1c44a3 --- /dev/null +++ b/tests/test_algorithm.cpp @@ -0,0 +1,81 @@ +/* -*- c++ -*- */ +/* + * Copyright (C) 2009 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "../include/algorithm" +#ifndef ANDROID_ASTL_ALGORITHM__ +#error "Wrong header included!!" +#endif +#include "macros.h" + +namespace android { + +bool testSwapInt() +{ + int a = 5; + int b = 10; + + std::swap(a, b); + EXPECT_TRUE(a == 10); + EXPECT_TRUE(b == 5); + return true; +} + +bool testMin() +{ + int a = 5; + int b = 10; + + int c = std::min(a, b); + EXPECT_TRUE(c == 5); + c = std::min(b, a); + EXPECT_TRUE(c == 5); + return true; +} + +bool testMax() +{ + int a = 5; + int b = 10; + + int c = std::max(a, b); + EXPECT_TRUE(c == 10); + c = std::max(b, a); + EXPECT_TRUE(c == 10); + return true; +} + +} // namespace android + +int main(int argc, char **argv) +{ + FAIL_UNLESS(testSwapInt); + FAIL_UNLESS(testMin); + FAIL_UNLESS(testMax); + return kPassed; +} diff --git a/tests/test_string.cpp b/tests/test_string.cpp new file mode 100644 index 0000000..ea3cbb2 --- /dev/null +++ b/tests/test_string.cpp @@ -0,0 +1,709 @@ +/* -*- c++ -*- */ +/* + * Copyright (C) 2009 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "../include/string" +#ifndef ANDROID_ASTL_STRING__ +#error "Wrong header included!!" +#endif +#include <climits> +#include <cstring> +#include "macros.h" + +#ifndef MAX_SIZE_T +#define MAX_SIZE_T (~(size_t)0) +#endif + +namespace android { +using std::string; + +bool testConstructorCString() +{ + string empty_str1; + EXPECT_TRUE(empty_str1.size() == 0); + EXPECT_TRUE(empty_str1.capacity() == 0); + + string empty_str2(""); + EXPECT_TRUE(empty_str2.size() == 0); + + const char empty_as_array[] = ""; + string empty_str3(empty_as_array); + EXPECT_TRUE(empty_str3.size() == 0); + + const char literal[] = "scott mills cracks me up"; + string str1(literal); + EXPECT_TRUE(strcmp(literal, str1.c_str()) == 0); + + string str2(literal, 11); + EXPECT_TRUE(strcmp("scott mills", str2.c_str()) == 0); + + string str3(literal, sizeof(literal)); + EXPECT_TRUE(strcmp(literal, str3.c_str()) == 0); + + // Pass the end of the string => still ok, there is \0 + string str4(literal, sizeof(literal) + 1); + EXPECT_TRUE(str4.size() == sizeof(literal) + 1); + + string str5(literal, literal + 11); + EXPECT_TRUE(strcmp("scott mills", str5.c_str()) == 0); + + const char text[] = {'l','a','d','y',' ','g','a','g','a'}; + + string str6(text, ARRAYSIZE(text)); + EXPECT_TRUE(str6 == "lady gaga"); + + string str7(NULL); + EXPECT_TRUE(empty_str1.size() == 0); + EXPECT_TRUE(empty_str1.empty()); + return true; +} + +bool testConstructorString() +{ + string empty_str1; + string empty_str2; + EXPECT_TRUE(empty_str1.c_str() == empty_str2.c_str()); + + string empty_str3(empty_str2); + EXPECT_TRUE(empty_str3.size() == 0); + + const char string_with_nulls[] = "contains 2 \0 bytes \0."; + string str1 (string_with_nulls, 21); + EXPECT_TRUE(str1.size() == 21); + + string str2 (str1); + EXPECT_TRUE(str1.size() == 21); + + const string str3("scott mills cracks me up"); + string str4(str3, 12); + EXPECT_TRUE(strcmp("cracks me up", str4.c_str()) == 0); + + string str5(str3, 12, 6); + EXPECT_TRUE(strcmp("cracks", str5.c_str()) == 0); + + string str6(str3, 23); + EXPECT_TRUE(strcmp("p", str6.c_str()) == 0); + + string str7(str3, 24); + EXPECT_TRUE(strcmp("", str7.c_str()) == 0); + + string str8(str3, 23, 1); + EXPECT_TRUE(strcmp("p", str8.c_str()) == 0); + + string str9(str3, 24, 1); + EXPECT_TRUE(strcmp("", str9.c_str()) == 0); + + return true; +} + +bool testConstructorPointers() +{ + const string empty; + char data[] = "a 16 char string"; + + string str01(data, data + 0); + EXPECT_TRUE(str01.c_str() == empty.c_str()); + + string str02(data, data + 1); + EXPECT_TRUE(str02 == "a"); + + string str03(data + 2, data + 16); + EXPECT_TRUE(str03 == "16 char string"); + + string str04(data + 15, data + 16); + EXPECT_TRUE(str04 == "g"); + + string str05(data + 16, data + 16); + EXPECT_TRUE(str05 == ""); + + return true; +} + +bool testConstructorRepeatChar() +{ + string str01(0, 'c'); + + EXPECT_TRUE(str01.empty()); + EXPECT_TRUE(str01.size() == 0); + EXPECT_TRUE(str01.capacity() == 0); + + string str02(10, 'c'); + + EXPECT_TRUE(!str02.empty()); + EXPECT_TRUE(str02.size() == 10); + EXPECT_TRUE(str02.capacity() == 10); + + for (int i = 0; i < 100; ++i) + { + string str03(i, 'x'); + + str03.reserve(i + 20); + EXPECT_TRUE(str03[i] == '\0'); + } + + return true; +} + +bool testConstructorInvalidValues() +{ + const string empty; + const string str01("a 16 char string"); + + EXPECT_TRUE(str01.size() == 16); + + string str02(str01, 17, 1); // invalid index + EXPECT_TRUE(str02.c_str() == empty.c_str()); + + string str03(str01, 17, 0); // invalid index + EXPECT_TRUE(str03.c_str() == empty.c_str()); + + string str04(str01, -1, 0); // invalid index + EXPECT_TRUE(str04.c_str() == empty.c_str()); + + string str05(str01, 0, 17); // invalid length + EXPECT_TRUE(str05.c_str() == empty.c_str()); + + string str06(str01, 17); // invalid index + EXPECT_TRUE(str06.c_str() == empty.c_str()); + + char end[] = "a string"; + char *begin = end + 1; // begin after end. + + string str07(begin, end); + EXPECT_TRUE(str07.c_str() == empty.c_str()); + + return true; +} + +bool testSize() +{ + string str01; + EXPECT_TRUE(str01.size() == 0); + EXPECT_TRUE(str01.length() == 0); + + str01 += "a string."; + + EXPECT_TRUE(str01.size() == 9); + EXPECT_TRUE(str01.length() == 9); + + return true; +} + +bool testCString() +{ + string str01; + string str02; + + // Should point to the same empty string. + EXPECT_TRUE(str01.c_str() == str02.c_str()); + // c_str() == data() + EXPECT_TRUE(str01.c_str() == str01.data()); + EXPECT_TRUE(str01.empty()); + + const char text[] = "a string"; + str01 += text; + EXPECT_TRUE(strcmp(str01.c_str(), text) == 0); + EXPECT_TRUE(strcmp(str01.data(), text) == 0); + EXPECT_TRUE(!str01.empty()); + + // after a clear, points back to the original empty string. + str01.clear(); + EXPECT_TRUE(str01.c_str() == str02.c_str()); + EXPECT_TRUE(str01.empty()); + + return true; +} + +bool testReserve() +{ + string str01; + size_t capacity = str01.capacity(); + + EXPECT_TRUE(0 == capacity); + + str01.reserve(5); + EXPECT_TRUE(5 == str01.capacity()); + str01.reserve(0); + EXPECT_TRUE(0 == str01.capacity()); + + string str02("7 chars"); + EXPECT_TRUE(7 == str02.capacity()); + EXPECT_TRUE(7 == str02.size()); + + str02.reserve(10); + EXPECT_TRUE(str02 == "7 chars"); + EXPECT_TRUE(10 == str02.capacity()); + EXPECT_TRUE(7 == str02.size()); + + str02.reserve(6); // no effect + EXPECT_TRUE(str02 == "7 chars"); + EXPECT_TRUE(10 == str02.capacity()); + EXPECT_TRUE(7 == str02.size()); + + string str03; + const string str04; + + // Both point to kEmptyString. + EXPECT_TRUE(str03.c_str() == str04.c_str()); + + str03.reserve(); + EXPECT_TRUE(0 == str03.capacity()); + EXPECT_TRUE(str03.c_str() == str04.c_str()); + + str03.reserve(10); + EXPECT_TRUE(10 == str03.capacity()); + // Not pointing at the empty string anymore. + EXPECT_TRUE(str03.c_str() != str04.c_str()); + + str03.reserve(); + EXPECT_TRUE(0 == str03.capacity()); + // c_str() points back to the empty string. + EXPECT_TRUE(str03.c_str() == str04.c_str()); + + str03.reserve(10); + str03.append("7 chars"); + EXPECT_TRUE(str03 == "7 chars"); + str03.reserve(); // shrink to fit. + EXPECT_TRUE(7 == str03.capacity()); + + string str05 = "twelve chars"; + string str06 = str05; + str05.reserve(1); + EXPECT_TRUE(str05.capacity() == 12); + + + for (size_t i = 1; i <= 100; i *= 2) + { + string str(i, 'x'); + str.reserve(3 * i); + EXPECT_TRUE(str.capacity() == 3 * i); + + str.reserve(2 * i); + EXPECT_TRUE(str.capacity() == 2 * i); + + str.reserve(); + EXPECT_TRUE(str.capacity() == i); + } + + // Check overflow. + string str07; + + str07.reserve(10); + EXPECT_TRUE(str07.capacity() == 10); + + str07.reserve(MAX_SIZE_T); + + EXPECT_TRUE(str07.capacity() == 10); + + return true; +} + +bool testAppend() +{ + string str1; + const char *text = "You spin my head right round."; + + str1.append(text); + EXPECT_TRUE(str1 == text); + + str1.append(" Flo Rida."); + EXPECT_TRUE(str1 == "You spin my head right round. Flo Rida."); + + string str2; + str2.append(str1); + EXPECT_TRUE(str2 == "You spin my head right round. Flo Rida."); + + string str3("You spin "); + str3.append("my head right round."); + EXPECT_TRUE(str3 == "You spin my head right round."); + + string str4("You spin "); + string str5("my head right round."); + str4.append(str5); + EXPECT_TRUE(str4 == "You spin my head right round."); + + string str6(""); + string str7(""); + str6.append(str7); + EXPECT_TRUE(str6 == ""); + EXPECT_TRUE(str6.empty()); + + string str8; + str8.append("a"); + EXPECT_TRUE(str8 == "a"); + + const char more_text[] = {'l','a','d','y',' ','g','a','g','a'}; + + string str9; + str9.append(more_text, ARRAYSIZE(more_text)); + EXPECT_TRUE(str9 == string(more_text, ARRAYSIZE(more_text))); + + string str10; + str10.append("", 0); + EXPECT_TRUE(str10.size() == 0 ); + str10.append(text, strlen(text)); + EXPECT_TRUE(str10 == "You spin my head right round."); + + string str11; + str11.append("You spin my head right round.", 5, 11); + + EXPECT_TRUE(str11 == "pin my head"); + + // Append overflow + string str12("original"); + char dummy[] = "unused"; + // We lie about the size but that is ok. Since the lenght of the new string + // is going to be MAX_SIZE_T, the call will have not effect (there is no + // space for the trailing '\0'). + str12.append(dummy, MAX_SIZE_T); + EXPECT_TRUE(str12 == "original"); + + return true; +} + +bool testAppendOperator() +{ + string str1; + const char *text = "You spin my head right round."; + + str1 += text; + EXPECT_TRUE(str1 == text); + + str1 += " Flo Rida."; + EXPECT_TRUE(str1 == "You spin my head right round. Flo Rida."); + + string str2; + str2 += str1; + EXPECT_TRUE(str2 == "You spin my head right round. Flo Rida."); + + string str3("You spin "); + str3 += "my head right round."; + EXPECT_TRUE(str3 == "You spin my head right round."); + + string str4("You spin "); + string str5("my head right round."); + str4 += str5; + EXPECT_TRUE(str4 == "You spin my head right round."); + + string str6(""); + string str7(""); + str6 += str7; + EXPECT_TRUE(str6 == ""); + EXPECT_TRUE(str6.empty()); + + string str8; + str8 += "a"; + EXPECT_TRUE(str8 == "a"); + + const char more_text[] = {'l','a','d','y',' ','g','a','g','a'}; + + string str9; + for (size_t i = 0; i < ARRAYSIZE(more_text); ++i) + { + str9 += more_text[i]; + } + EXPECT_TRUE(str9 == "lady gaga"); + + str9 += (const char *)NULL; + EXPECT_TRUE(str9 == "lady gaga"); + + string str10(more_text, ARRAYSIZE(more_text)); + EXPECT_TRUE(str10 == "lady gaga"); + str10 += '\0'; + EXPECT_TRUE(str10 == "lady gaga"); + EXPECT_TRUE(str10 == string("lady gaga\0", 10)); + str10 += 'x'; + EXPECT_TRUE(str10 == string("lady gaga\0x", 11)); + EXPECT_TRUE(str10[11] == '\0'); + + return true; +} + + +bool testCompare() +{ + string str01("bell helmet"); + string str02("bell moto"); + string str03("bell"); + string str04("bell pants"); + string str05; + + str05 = str01; + // Compare with self. + EXPECT_TRUE(str01 == str01); + EXPECT_TRUE(!(str01 != str01)); + + EXPECT_TRUE(str01 == str05); + EXPECT_TRUE(str05 == str01); + EXPECT_TRUE(!(str01 != str05)); + EXPECT_TRUE(!(str05 != str01)); + + EXPECT_TRUE(str01 != str02); + EXPECT_TRUE(str01 != str03); + EXPECT_TRUE(str01 != str04); + + // Compare with literals. + EXPECT_TRUE(str01 == "bell helmet"); + EXPECT_TRUE(!(str01 != "bell helmet")); + EXPECT_TRUE("bell helmet" == str01); + EXPECT_TRUE(!("bell helmet" != str01)); + + // Compare with char array. + char array[] = { 'a', ' ', 'b', 'u', 'g', '\0'}; + str01 = "a bug"; + EXPECT_TRUE(array == str01); + + EXPECT_TRUE(strcmp("a bug", "a bugg") < 0); + + char array2[] = { 'a', 'b', 'u', 'g', 'g' }; + EXPECT_TRUE(str01.compare(array2) < 0); + + string str06; + EXPECT_TRUE(str06 != NULL); + return true; +} + +bool testSwap() +{ + string str01; + string str02("test"); + + str01.swap(str02); + EXPECT_TRUE(str02.empty()); + EXPECT_TRUE(str01 == "test"); + + string str03("altima"); + string str04("versa"); + str03.swap(str04); + EXPECT_TRUE(str03 == "versa"); + EXPECT_TRUE(str04 == "altima"); + + { + string empty; + // swap can be used to clean strings + str04.swap(empty); + } + EXPECT_TRUE(str04.empty()); + + return true; +} + +bool testAccessor() +{ + string str01 = "earmarks"; + + EXPECT_TRUE(str01[0] == 'e'); + EXPECT_TRUE(str01[7] == 's'); + + str01[0] = 'E'; + str01[7] = 'S'; + EXPECT_TRUE(str01 == "EarmarkS"); + + for (int i = 0; i < 100; ++i) + { + string str02(i, 'x'); + + str02.reserve(20); + + EXPECT_TRUE(str02[i] == '\0'); + + const string str03(str02); + EXPECT_TRUE(str03[i] == '\0'); + } + + string str05; + str05.reserve(100); + str05[99] = 'a'; + + return true; +} + + +bool testAssignment() +{ + const char *literal = "Need to buy a full face helmet for Lilie."; + const string str01 = literal; + + EXPECT_TRUE(str01.length() == strlen(literal)); + EXPECT_TRUE(str01.size() == strlen(literal)); + EXPECT_TRUE(str01.capacity() == strlen(literal)); + EXPECT_TRUE(str01 == literal); + + string str02; + + str02.assign(str01, 8, 33); + EXPECT_TRUE(str02 == "buy a full face helmet for Lilie."); + + str02.assign(str01, 8, 0); + EXPECT_TRUE(str02 == ""); + + str02.assign(str01, 0, 7); + EXPECT_TRUE(str02 == "Need to"); + + str02.assign("unchanged"); + str02.assign(str01, 35, 1000); + EXPECT_TRUE(str02 == "unchanged"); + + str02.assign(str01, 35, 6); + EXPECT_TRUE(str02 == "Lilie."); + + + str02.assign(str01, 35, 5); + EXPECT_TRUE(str02 == "Lilie"); + + string str03; + + str03.assign(literal); + EXPECT_TRUE(str03 == "Need to buy a full face helmet for Lilie."); + + string str04; + + str04.assign(str03.c_str()); + EXPECT_TRUE(str04 == "Need to buy a full face helmet for Lilie."); + + str04.assign(str03.c_str() + 5, 10); + EXPECT_TRUE(str04 == "to buy a f"); + + str04.assign("noop"); + str04.assign(NULL); + EXPECT_TRUE(str04 == "noop"); + + str04.assign(str01, str01.size() - 1, 1); + EXPECT_TRUE(str04 == "."); + + str04.assign("unchanged"); + str04.assign(str01, str01.size(), 1); + str04.assign(NULL, 4, 1); + str04.assign(NULL, 4); + EXPECT_TRUE(str04 == "unchanged"); + + return true; +} + + +bool testConcat() +{ + string str01("The full"); + string str02(" sentence."); + string str03; + + str03 = str01 + str02; + EXPECT_TRUE(str03 == "The full sentence."); + + str03 = str02 + str01; + EXPECT_TRUE(str03 == " sentence.The full"); + + + str03 = str01 + " sentence."; + EXPECT_TRUE(str03 == "The full sentence."); + + str03 = "The full" + str02; + EXPECT_TRUE(str03 == "The full sentence."); + + str03 = 'l' + str02; + str03 = 'l' + str03; + str03 = 'u' + str03; + str03 = 'f' + str03; + str03 = ' ' + str03; + str03 = 'e' + str03; + str03 = 'h' + str03; + str03 = 'T' + str03; + EXPECT_TRUE(str03 == "The full sentence."); + + str03 = "The full "; + str03 = str03 + 's'; + str03 = str03 + 'e'; + str03 = str03 + 'n'; + str03 = str03 + 't'; + str03 = str03 + 'e'; + str03 = str03 + 'n'; + str03 = str03 + 'c'; + str03 = str03 + 'e'; + str03 = str03 + '.'; + EXPECT_TRUE(str03 == "The full sentence."); + + // Check the new string buffer is not the same as the original one. + string str04("left and"); + string str05(" right"); + string str06(str04 + str05); + + EXPECT_TRUE(str06 == "left and right"); + EXPECT_TRUE(str06.c_str() != str04.c_str()); + EXPECT_TRUE(str06.c_str() != str05.c_str()); + + str06 = str04 + str05; + EXPECT_TRUE(str06 == "left and right"); + EXPECT_TRUE(str06.c_str() != str04.c_str()); + EXPECT_TRUE(str06.c_str() != str05.c_str()); + return true; +} + +bool testPushBack() +{ + string str01; + + str01.push_back('a'); + EXPECT_TRUE(str01 == "a"); + EXPECT_TRUE(str01.capacity() == 1); + + str01.reserve(10); + str01.push_back('b'); + EXPECT_TRUE(str01 == "ab"); + EXPECT_TRUE(str01.capacity() == 10); + EXPECT_TRUE(str01[2] == '\0'); + + str01.reserve(); + EXPECT_TRUE(str01 == "ab"); + EXPECT_TRUE(str01.capacity() == 2); + EXPECT_TRUE(str01[2] == '\0'); + + return true; +} + +} // namespace android + +int main(int argc, char **argv) +{ + FAIL_UNLESS(testConstructorCString); + FAIL_UNLESS(testConstructorString); + FAIL_UNLESS(testConstructorRepeatChar); + FAIL_UNLESS(testConstructorPointers); + FAIL_UNLESS(testConstructorInvalidValues); + FAIL_UNLESS(testSize); + FAIL_UNLESS(testCString); + FAIL_UNLESS(testAppend); + FAIL_UNLESS(testAppendOperator); + FAIL_UNLESS(testConcat); + FAIL_UNLESS(testAssignment); + FAIL_UNLESS(testReserve); + FAIL_UNLESS(testCompare); + FAIL_UNLESS(testAccessor); + FAIL_UNLESS(testSwap); + FAIL_UNLESS(testPushBack); + return kPassed; +} |