diff --git a/source/tests/CarlaString.cpp b/source/tests/CarlaString.cpp index b4b637fc6..90e8ed2a3 100644 --- a/source/tests/CarlaString.cpp +++ b/source/tests/CarlaString.cpp @@ -1,6 +1,6 @@ /* - * Carla Tests - * Copyright (C) 2013 Filipe Coelho + * CarlaString Tests + * Copyright (C) 2013-2014 Filipe Coelho * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -27,6 +27,7 @@ int main() assert(str.isEmpty()); assert(str.contains("")); assert(str.contains("\0")); + assert(str.isEmpty() == !str.isNotEmpty()); assert(! str.isNotEmpty()); assert(! str.contains(" ")); assert(! str.isDigit(0)); @@ -35,6 +36,7 @@ int main() str = "5"; assert(str.length() == 1); assert(str.length() == std::strlen("5")); + assert(str.isEmpty() == !str.isNotEmpty()); assert(str.isNotEmpty()); assert(str.contains("")); assert(str.contains("5")); @@ -147,10 +149,27 @@ int main() str2 = "test2"; str2 += ".0"; + // startsWith, contains and endsWith CarlaString str3("1.23_ ?test2.0 final"); + assert(str3.startsWith('1')); + assert(str3.startsWith("1")); + assert(str3.startsWith("1.23_ ")); + assert(str3.contains("1")); + assert(str3.contains("?test")); + assert(str3.contains("final")); + assert(! str3.contains("\n")); + assert(! str3.contains("\t")); + assert(str3.endsWith('l')); + assert(str3.endsWith("l")); + assert(str3.endsWith(" final")); CarlaString str4 = "" + str1 + str2 + " final"; + assert(str4.contains(str1)); + assert(str4.contains(str2)); + assert(str4.startsWith(str1)); + assert(! str4.startsWith(str2)); + // length and content assert(str3 == "1.23_ ?test2.0 final"); assert(str3 == str4); assert(str3.length() == str4.length()); @@ -160,6 +179,64 @@ int main() assert(str5 == "ola _51ah0x910_ 6"); assert(str5.length() == std::strlen("ola _51ah0x910_ 6")); + // find, rfind + bool found; + assert(str5.find('o', &found) == 0); + assert(found); + assert(str5.find('l', &found) == 1); + assert(found); + assert(str5.find('5', &found) == 5); + assert(found); + assert(str5.find('6', &found) == str5.length()-1); + assert(found); + + assert(str5.rfind('6', &found) == str5.length()-1); + assert(found); + assert(str5.rfind(' ', &found) == str5.length()-2); + assert(found); + assert(str5.rfind('x', &found) == 10); + assert(found); + + assert(str5.find('\0', &found) == str5.length()); + assert(! found); + assert(str5.find('Z', &found) == str5.length()); + assert(! found); + assert(str5.rfind('A', &found) == str5.length()); + assert(! found); + + assert(str5.find("o", &found) == 0); + assert(found); + assert(str5.find("ola", &found) == 0); + assert(found); + assert(str5.find("la", &found) == 1); + assert(found); + assert(str5.find(" ", &found) == 3); + assert(found); + assert(str5.find("6", &found) == str5.length()-1); + assert(found); + assert(str5.find(" 6", &found) == str5.length()-2); + assert(found); + + assert(str5.rfind("6", &found) == str5.length()-1); + assert(found); + assert(str5.rfind(" ", &found) == str5.length()-2); + assert(found); + assert(str5.rfind(" 6", &found) == str5.length()-2); + assert(found); + assert(str5.rfind("ola", &found) == 0); + assert(found); + assert(str5.rfind("la ", &found) == 1); + assert(found); + + assert(str5.find("", &found) == str5.length()); + assert(! found); + assert(str5.find("Zoom", &found) == str5.length()); + assert(! found); + assert(str5.rfind("", &found) == str5.length()); + assert(! found); + assert(str5.rfind("haha!", &found) == str5.length()); + assert(! found); + printf("FINAL: \"%s\"\n", (const char*)str5); // clear diff --git a/source/tests/Makefile b/source/tests/Makefile index 2060359b0..7895cebfe 100644 --- a/source/tests/Makefile +++ b/source/tests/Makefile @@ -23,8 +23,9 @@ ANSI_FLAGS += -L../backend -lcarla_standalone2 # -------------------------------------------------------------- -TARGETS = ansi-pedantic-test_c ansi-pedantic-test_c99 ansi-pedantic-test_cxx ansi-pedantic-test_cxx11 -# TARGETS += CarlaString Print RtList Utils +# TARGETS = ansi-pedantic-test_c ansi-pedantic-test_c99 ansi-pedantic-test_cxx ansi-pedantic-test_cxx11 +TARGETS += CarlaString +# Print RtList Utils all: $(TARGETS) @@ -46,7 +47,7 @@ ansi-pedantic-test_cxx11: ansi-pedantic-test.c ../backend/Carla*.h CarlaString: CarlaString.cpp ../utils/CarlaString.hpp $(CXX) $< $(BUILD_CXX_FLAGS) $(LINK_FLAGS) -o $@ -# valgrind ./CarlaString + valgrind ./CarlaString DISTRHO: DISTRHO.cpp ../modules/distrho/*.hpp ../modules/distrho/src/*.cpp $(CXX) $< ../modules/dgl.a $(BUILD_CXX_FLAGS) -I../modules/distrho -I../modules/carla_native/nekobi $(LINK_FLAGS) $(DGL_LIBS) -lpthread -o $@ diff --git a/source/utils/CarlaString.hpp b/source/utils/CarlaString.hpp index e6a8bca51..331c6ea0d 100644 --- a/source/utils/CarlaString.hpp +++ b/source/utils/CarlaString.hpp @@ -262,7 +262,7 @@ public: if (fBufferLen < prefixLen) return false; - return (std::strncmp(fBuffer + (fBufferLen-prefixLen), prefix, prefixLen) == 0); + return (std::strncmp(fBuffer, prefix, prefixLen) == 0); } /* @@ -272,7 +272,7 @@ public: { CARLA_SAFE_ASSERT_RETURN(c != '\0', false); - return (fBufferLen > 0 && fBuffer[fBufferLen] == c); + return (fBufferLen > 0 && fBuffer[fBufferLen-1] == c); } /* @@ -324,7 +324,7 @@ public: */ size_t find(const char* const strBuf, bool* const found = nullptr) const { - if (fBufferLen == 0 || strBuf != nullptr || strBuf[0] != '\0') + if (fBufferLen == 0 || strBuf == nullptr || strBuf[0] == '\0') { if (found != nullptr) *found = false; @@ -338,7 +338,7 @@ public: if (ret < 0) { // should never happen! - carla_stderr2("Carla assertion failure: \"ret >= 0\" in file %s, line %i, value %i", __FILE__, __LINE__, ret); + carla_safe_assert_int("ret >= 0", __FILE__, __LINE__, int(ret)); if (found != nullptr) *found = false; @@ -395,12 +395,14 @@ public: if (fBufferLen == 0 || strBuf == nullptr || strBuf[0] == '\0') return fBufferLen; - size_t ret = fBufferLen+1; + const size_t strBufLen(std::strlen(strBuf)); + + size_t ret = fBufferLen; const char* tmpBuf = fBuffer; for (size_t i=0; i < fBufferLen; ++i) { - if (std::strstr(tmpBuf, strBuf) == nullptr) + if (std::strstr(tmpBuf+1, strBuf) == nullptr && std::strncmp(tmpBuf, strBuf, strBufLen) == 0) { if (found != nullptr) *found = true; @@ -411,7 +413,7 @@ public: ++tmpBuf; } - return (ret > fBufferLen) ? fBufferLen : fBufferLen-ret; + return fBufferLen-ret; } /* @@ -526,7 +528,13 @@ public: char& operator[](const size_t pos) const noexcept { - return fBuffer[pos]; + if (pos < fBufferLen) + return fBuffer[pos]; + + static char rfallback; + rfallback = '\0'; + carla_safe_assert("pos < fBufferLen", __FILE__, __LINE__); + return rfallback; } bool operator==(const char* const strBuf) const