diff options
| author | Rémi Verschelde | 2017-03-05 16:44:50 +0100 |
|---|---|---|
| committer | Rémi Verschelde | 2017-03-05 16:44:50 +0100 |
| commit | 5dbf1809c6e3e905b94b8764e99491e608122261 (patch) | |
| tree | 5e5a5360db15d86d59ec8c6e4f7eb511388c5a9a /main/tests/test_string.cpp | |
| parent | 45438e9918d421b244bfd7776a30e67dc7f2d3e3 (diff) | |
| download | godot-5dbf180.tar.gz godot-5dbf180.tar.zst godot-5dbf180.zip | |
A Whole New World (clang-format edition)
I can show you the code
Pretty, with proper whitespace
Tell me, coder, now when did
You last write readable code?
I can open your eyes
Make you see your bad indent
Force you to respect the style
The core devs agreed upon
A whole new world
A new fantastic code format
A de facto standard
With some sugar
Enforced with clang-format
A whole new world
A dazzling style we all dreamed of
And when we read it through
It's crystal clear
That now we're in a whole new world of code
Diffstat (limited to 'main/tests/test_string.cpp')
| -rw-r--r-- | main/tests/test_string.cpp | 260 |
1 files changed, 112 insertions, 148 deletions
diff --git a/main/tests/test_string.cpp b/main/tests/test_string.cpp index ca7fdedcb..41ec11384 100644 --- a/main/tests/test_string.cpp +++ b/main/tests/test_string.cpp @@ -29,9 +29,9 @@ #include "ustring.h" #include <wchar.h> //#include "math_funcs.h" -#include <stdio.h> -#include "os/os.h" #include "core/io/ip_address.h" +#include "os/os.h" +#include <stdio.h> #include "test_string.h" @@ -44,10 +44,9 @@ bool test_1() { String s = "Hello"; OS::get_singleton()->print("\tExpected: Hello\n"); - OS::get_singleton()->print("\tResulted: %ls\n",s.c_str()); - - return (wcscmp(s.c_str(),L"Hello")==0); + OS::get_singleton()->print("\tResulted: %ls\n", s.c_str()); + return (wcscmp(s.c_str(), L"Hello") == 0); } bool test_2() { @@ -58,10 +57,9 @@ bool test_2() { String t = s; OS::get_singleton()->print("\tExpected: Dolly\n"); - OS::get_singleton()->print("\tResulted: %ls\n",t.c_str()); - - return (wcscmp(t.c_str(),L"Dolly")==0); + OS::get_singleton()->print("\tResulted: %ls\n", t.c_str()); + return (wcscmp(t.c_str(), L"Dolly") == 0); } bool test_3() { @@ -72,10 +70,9 @@ bool test_3() { String t(s); OS::get_singleton()->print("\tExpected: Sheep\n"); - OS::get_singleton()->print("\tResulted: %ls\n",t.c_str()); - - return (wcscmp(t.c_str(),L"Sheep")==0); + OS::get_singleton()->print("\tResulted: %ls\n", t.c_str()); + return (wcscmp(t.c_str(), L"Sheep") == 0); } bool test_4() { @@ -85,10 +82,9 @@ bool test_4() { String s(L"Give me"); OS::get_singleton()->print("\tExpected: Give me\n"); - OS::get_singleton()->print("\tResulted: %ls\n",s.c_str()); - - return (wcscmp(s.c_str(),L"Give me")==0); + OS::get_singleton()->print("\tResulted: %ls\n", s.c_str()); + return (wcscmp(s.c_str(), L"Give me") == 0); } bool test_5() { @@ -98,89 +94,81 @@ bool test_5() { String s(L"Wool"); OS::get_singleton()->print("\tExpected: Wool\n"); - OS::get_singleton()->print("\tResulted: %ls\n",s.c_str()); - - return (wcscmp(s.c_str(),L"Wool")==0); + OS::get_singleton()->print("\tResulted: %ls\n", s.c_str()); + return (wcscmp(s.c_str(), L"Wool") == 0); } bool test_6() { OS::get_singleton()->print("\n\nTest 6: comparisons (equal)\n"); - - String s="Test Compare"; + String s = "Test Compare"; OS::get_singleton()->print("\tComparing to \"Test Compare\"\n"); - if (! ( s=="Test Compare" ) ) + if (!(s == "Test Compare")) return false; - if (! ( s==L"Test Compare" ) ) + if (!(s == L"Test Compare")) return false; - if (! ( s==String("Test Compare") ) ) + if (!(s == String("Test Compare"))) return false; return true; - } bool test_7() { OS::get_singleton()->print("\n\nTest 7: comparisons (unequal)\n"); - - String s="Test Compare"; + String s = "Test Compare"; OS::get_singleton()->print("\tComparing to \"Test Compare\"\n"); - if (! ( s!="Peanut" ) ) + if (!(s != "Peanut")) return false; - if (! ( s!=L"Coconut" ) ) + if (!(s != L"Coconut")) return false; - if (! ( s!=String("Butter") ) ) + if (!(s != String("Butter"))) return false; return true; - } bool test_8() { OS::get_singleton()->print("\n\nTest 8: comparisons (operator<)\n"); - - String s="Bees"; + String s = "Bees"; OS::get_singleton()->print("\tComparing to \"Bees\"\n"); - if ( ! (s < "Elephant") ) + if (!(s < "Elephant")) return false; - if ( s < L"Amber" ) + if (s < L"Amber") return false; - if ( s < String("Beatrix") ) + if (s < String("Beatrix")) return false; return true; - } bool test_9() { OS::get_singleton()->print("\n\nTest 9: Concatenation\n"); - String s; - s+="Have"; - s+=' '; - s+='a'; - s+=String(" "); + s += "Have"; + s += ' '; + s += 'a'; + s += String(" "); s = s + L"Nice"; s = s + " "; s = s + String("Day"); @@ -188,14 +176,13 @@ bool test_9() { OS::get_singleton()->print("\tComparing to \"Have a Nice Day\"\n"); return (s == "Have a Nice Day"); - } bool test_10() { OS::get_singleton()->print("\n\nTest 10: Misc funcs (size/length/empty/etc)\n"); - if (! String("").empty()) + if (!String("").empty()) return false; if (String("Mellon").size() != 7) @@ -205,44 +192,39 @@ bool test_10() { return false; return true; - } - bool test_11() { OS::get_singleton()->print("\n\nTest 11: Operator[]\n"); - String a="Kugar Sane"; + String a = "Kugar Sane"; - a[0]='S'; - a[6]='C'; + a[0] = 'S'; + a[6] = 'C'; if (a != "Sugar Cane") return false; - if (a[1]!='u') + if (a[1] != 'u') return false; return true; - } bool test_12() { OS::get_singleton()->print("\n\nTest 12: case functions\n"); - - String a="MoMoNgA"; + String a = "MoMoNgA"; if (a.to_upper() != "MOMONGA") return false; - if (a.nocasecmp_to("momonga")!=0) + if (a.nocasecmp_to("momonga") != 0) return false; return true; - } bool test_13() { @@ -251,16 +233,15 @@ bool test_13() { /* how can i embed UTF in here? */ - static const CharType ustr[] = { 0x304A , 0x360F, 0x3088, 0x3046, 0 }; + static const CharType ustr[] = { 0x304A, 0x360F, 0x3088, 0x3046, 0 }; //static const wchar_t ustr[] = { 'P', 0xCE, 'p',0xD3, 0 }; - String s=ustr; - - OS::get_singleton()->print("\tUnicode: %ls\n",ustr); - s.parse_utf8( s.utf8().get_data() ); - OS::get_singleton()->print("\tConvert/Parse UTF8: %ls\n",s.c_str()); + String s = ustr; - return (s==ustr); + OS::get_singleton()->print("\tUnicode: %ls\n", ustr); + s.parse_utf8(s.utf8().get_data()); + OS::get_singleton()->print("\tConvert/Parse UTF8: %ls\n", s.c_str()); + return (s == ustr); } bool test_14() { @@ -268,195 +249,180 @@ bool test_14() { OS::get_singleton()->print("\n\nTest 14: ASCII\n"); String s = L"Primero Leche"; - OS::get_singleton()->print("\tAscii: %s\n",s.ascii().get_data()); - - String t=s.ascii().get_data(); - return (s==t); + OS::get_singleton()->print("\tAscii: %s\n", s.ascii().get_data()); + String t = s.ascii().get_data(); + return (s == t); } bool test_15() { OS::get_singleton()->print("\n\nTest 15: substr\n"); - String s="Killer Baby"; - OS::get_singleton()->print("\tsubstr(3,4) of \"%ls\" is \"%ls\"\n",s.c_str(),s.substr(3,4).c_str()); - - return (s.substr(3,4)=="ler "); + String s = "Killer Baby"; + OS::get_singleton()->print("\tsubstr(3,4) of \"%ls\" is \"%ls\"\n", s.c_str(), s.substr(3, 4).c_str()); + return (s.substr(3, 4) == "ler "); } bool test_16() { OS::get_singleton()->print("\n\nTest 16: find\n"); - String s="Pretty Woman"; - OS::get_singleton()->print("\tString: %ls\n",s.c_str()); - OS::get_singleton()->print("\t\"tty\" is at %i pos.\n",s.find("tty")); - OS::get_singleton()->print("\t\"Revenge of the Monster Truck\" is at %i pos.\n",s.find("Revenge of the Monster Truck")); + String s = "Pretty Woman"; + OS::get_singleton()->print("\tString: %ls\n", s.c_str()); + OS::get_singleton()->print("\t\"tty\" is at %i pos.\n", s.find("tty")); + OS::get_singleton()->print("\t\"Revenge of the Monster Truck\" is at %i pos.\n", s.find("Revenge of the Monster Truck")); - if (s.find("tty")!=3) + if (s.find("tty") != 3) return false; - if (s.find("Revenge of the Monster Truck")!=-1) + if (s.find("Revenge of the Monster Truck") != -1) return false; return true; - } bool test_17() { OS::get_singleton()->print("\n\nTest 17: find no case\n"); - String s="Pretty Whale"; - OS::get_singleton()->print("\tString: %ls\n",s.c_str()); - OS::get_singleton()->print("\t\"WHA\" is at %i pos.\n",s.findn("WHA")); - OS::get_singleton()->print("\t\"Revenge of the Monster SawFish\" is at %i pos.\n",s.findn("Revenge of the Monster Truck")); + String s = "Pretty Whale"; + OS::get_singleton()->print("\tString: %ls\n", s.c_str()); + OS::get_singleton()->print("\t\"WHA\" is at %i pos.\n", s.findn("WHA")); + OS::get_singleton()->print("\t\"Revenge of the Monster SawFish\" is at %i pos.\n", s.findn("Revenge of the Monster Truck")); - if (s.findn("WHA")!=7) + if (s.findn("WHA") != 7) return false; - if (s.findn("Revenge of the Monster SawFish")!=-1) + if (s.findn("Revenge of the Monster SawFish") != -1) return false; return true; - } bool test_18() { OS::get_singleton()->print("\n\nTest 18: find no case\n"); - String s="Pretty Whale"; - OS::get_singleton()->print("\tString: %ls\n",s.c_str()); - OS::get_singleton()->print("\t\"WHA\" is at %i pos.\n",s.findn("WHA")); - OS::get_singleton()->print("\t\"Revenge of the Monster SawFish\" is at %i pos.\n",s.findn("Revenge of the Monster Truck")); + String s = "Pretty Whale"; + OS::get_singleton()->print("\tString: %ls\n", s.c_str()); + OS::get_singleton()->print("\t\"WHA\" is at %i pos.\n", s.findn("WHA")); + OS::get_singleton()->print("\t\"Revenge of the Monster SawFish\" is at %i pos.\n", s.findn("Revenge of the Monster Truck")); - if (s.findn("WHA")!=7) + if (s.findn("WHA") != 7) return false; - if (s.findn("Revenge of the Monster SawFish")!=-1) + if (s.findn("Revenge of the Monster SawFish") != -1) return false; return true; - } bool test_19() { OS::get_singleton()->print("\n\nTest 19: Search & replace\n"); - String s="Happy Birthday, Anna!"; - OS::get_singleton()->print("\tString: %ls\n",s.c_str()); + String s = "Happy Birthday, Anna!"; + OS::get_singleton()->print("\tString: %ls\n", s.c_str()); - s=s.replace("Birthday","Halloween"); - OS::get_singleton()->print("\tReplaced Birthday/Halloween: %ls.\n",s.c_str()); - - return (s=="Happy Halloween, Anna!"); + s = s.replace("Birthday", "Halloween"); + OS::get_singleton()->print("\tReplaced Birthday/Halloween: %ls.\n", s.c_str()); + return (s == "Happy Halloween, Anna!"); } bool test_20() { OS::get_singleton()->print("\n\nTest 20: Insertion\n"); - String s="Who is Frederic?"; - - OS::get_singleton()->print("\tString: %ls\n",s.c_str()); - s=s.insert( s.find("?")," Chopin" ); - OS::get_singleton()->print("\tInserted Chopin: %ls.\n",s.c_str()); + String s = "Who is Frederic?"; - return (s=="Who is Frederic Chopin?"); + OS::get_singleton()->print("\tString: %ls\n", s.c_str()); + s = s.insert(s.find("?"), " Chopin"); + OS::get_singleton()->print("\tInserted Chopin: %ls.\n", s.c_str()); + return (s == "Who is Frederic Chopin?"); } bool test_21() { OS::get_singleton()->print("\n\nTest 21: Number -> String\n"); - OS::get_singleton()->print("\tPi is %f\n",33.141593); - OS::get_singleton()->print("\tPi String is %ls\n",String::num(3.141593).c_str()); - - return String::num(3.141593)=="3.141593"; + OS::get_singleton()->print("\tPi is %f\n", 33.141593); + OS::get_singleton()->print("\tPi String is %ls\n", String::num(3.141593).c_str()); + return String::num(3.141593) == "3.141593"; } bool test_22() { OS::get_singleton()->print("\n\nTest 22: String -> Int\n"); - static const char* nums[4]={ "1237461283", "- 22", "0", " - 1123412" }; - static const int num[4]={ 1237461283, -22, 0, -1123412 }; + static const char *nums[4] = { "1237461283", "- 22", "0", " - 1123412" }; + static const int num[4] = { 1237461283, -22, 0, -1123412 }; - for (int i=0;i<4;i++) { - OS::get_singleton()->print("\tString: \"%s\" as Int is %i\n",nums[i],String(nums[i]).to_int()); + for (int i = 0; i < 4; i++) { + OS::get_singleton()->print("\tString: \"%s\" as Int is %i\n", nums[i], String(nums[i]).to_int()); - if (String(nums[i]).to_int()!=num[i]) + if (String(nums[i]).to_int() != num[i]) return false; } return true; - } bool test_23() { OS::get_singleton()->print("\n\nTest 23: String -> Float\n"); - static const char* nums[4]={ "-12348298412.2", "0.05", "2.0002", " -0.0001" }; - static const double num[4]={ -12348298412.2, 0.05, 2.0002, -0.0001 }; + static const char *nums[4] = { "-12348298412.2", "0.05", "2.0002", " -0.0001" }; + static const double num[4] = { -12348298412.2, 0.05, 2.0002, -0.0001 }; - for (int i=0;i<4;i++) { - OS::get_singleton()->print("\tString: \"%s\" as Float is %f\n",nums[i],String(nums[i]).to_double()); + for (int i = 0; i < 4; i++) { + OS::get_singleton()->print("\tString: \"%s\" as Float is %f\n", nums[i], String(nums[i]).to_double()); - if ( ABS(String(nums[i]).to_double()-num[i])>0.00001) + if (ABS(String(nums[i]).to_double() - num[i]) > 0.00001) return false; } return true; - } - bool test_24() { OS::get_singleton()->print("\n\nTest 24: Slicing\n"); - String s="Mars,Jupiter,Saturn,Uranus"; - - const char*slices[4]={"Mars","Jupiter","Saturn","Uranus"}; + String s = "Mars,Jupiter,Saturn,Uranus"; - OS::get_singleton()->print("\tSlicing \"%ls\" by \"%s\"..\n",s.c_str(),","); + const char *slices[4] = { "Mars", "Jupiter", "Saturn", "Uranus" }; - for (int i=0;i<s.get_slice_count(",");i++) { + OS::get_singleton()->print("\tSlicing \"%ls\" by \"%s\"..\n", s.c_str(), ","); - OS::get_singleton()->print("\t\t%i- %ls\n",i+1,s.get_slice(",",i).c_str()); + for (int i = 0; i < s.get_slice_count(","); i++) { + OS::get_singleton()->print("\t\t%i- %ls\n", i + 1, s.get_slice(",", i).c_str()); - if (s.get_slice(",",i)!=slices[i]) + if (s.get_slice(",", i) != slices[i]) return false; } return true; - } bool test_25() { OS::get_singleton()->print("\n\nTest 25: Erasing\n"); - String s="Josephine is such a cute girl!"; + String s = "Josephine is such a cute girl!"; - OS::get_singleton()->print("\tString: %ls\n",s.c_str()); + OS::get_singleton()->print("\tString: %ls\n", s.c_str()); OS::get_singleton()->print("\tRemoving \"cute\"\n"); - s.erase(s.find("cute "),String("cute ").length()); - OS::get_singleton()->print("\tResult: %ls\n",s.c_str()); - - - return (s=="Josephine is such a girl!"); + s.erase(s.find("cute "), String("cute ").length()); + OS::get_singleton()->print("\tResult: %ls\n", s.c_str()); + return (s == "Josephine is such a girl!"); } bool test_26() { @@ -466,8 +432,8 @@ bool test_26() { }; struct test_27_data { - char const * data; - char const * begin; + char const *data; + char const *begin; bool expected; }; @@ -475,13 +441,13 @@ bool test_27() { OS::get_singleton()->print("\n\nTest 27: begins_with\n"); test_27_data tc[] = { - {"res://foobar", "res://", true}, - {"res", "res://", false}, - {"abc", "abc", true} + { "res://foobar", "res://", true }, + { "res", "res://", false }, + { "abc", "abc", true } }; size_t count = sizeof(tc) / sizeof(tc[0]); bool state = true; - for (size_t i = 0;state && i < count; ++i) { + for (size_t i = 0; state && i < count; ++i) { String s = tc[i].data; state = s.begins_with(tc[i].begin) == tc[i].expected; if (state) { @@ -496,7 +462,6 @@ bool test_27() { return state; }; - bool test_28() { OS::get_singleton()->print("\n\nTest 28: sprintf\n"); @@ -925,22 +890,22 @@ TestFunc test_funcs[] = { }; -MainLoop* test() { +MainLoop *test() { /** A character length != wchar_t may be forced, so the tests wont work */ - ERR_FAIL_COND_V( sizeof(CharType) != sizeof(wchar_t), NULL ); + ERR_FAIL_COND_V(sizeof(CharType) != sizeof(wchar_t), NULL); - int count=0; - int passed=0; + int count = 0; + int passed = 0; - while(true) { + while (true) { if (!test_funcs[count]) break; - bool pass=test_funcs[count](); + bool pass = test_funcs[count](); if (pass) passed++; - OS::get_singleton()->print("\t%s\n",pass?"PASS":"FAILED"); + OS::get_singleton()->print("\t%s\n", pass ? "PASS" : "FAILED"); count++; } @@ -954,5 +919,4 @@ MainLoop* test() { return NULL; } - } |
