Browse Source

string tests

Kajetan Johannes Hammerle 3 years ago
parent
commit
4d0f0b077e
3 changed files with 70 additions and 1 deletions
  1. 1 1
      tests/strings/string
  2. 61 0
      tests/strings/string_lib
  3. 8 0
      tests/strings/string_lib.out

+ 1 - 1
tests/strings/string

@@ -22,5 +22,5 @@ void main() {
     int** n = nullptr;
     wusi(n);
     wusi(nullptr);
-    wusi(b);
+    wusi(b);    
 }

+ 61 - 0
tests/strings/string_lib

@@ -0,0 +1,61 @@
+#define cstring const int* #end
+#define string int* #end
+
+string concat(cstring a, cstring b) {
+    int la = length(a);
+    int lb = length(b);
+    int* s = new int[la + lb];
+    for(int i = 0; i < la; i++) {
+        s[i] = a[i];
+    }
+    for(int i = 0; i < lb; i++) {
+        s[i + la] = b[i];
+    }
+    return s;
+}
+
+bool compare(cstring a, cstring b) {
+    int l = length(a);
+    if(l != length(b)) {   
+        return false;
+    }
+    for(int i = 0; i < l; i++) {
+        if(a[i] != b[i]) {
+            return false;
+        }
+    }
+    return true;
+}
+
+void toLower(string s) {
+    int l = length(s);
+    for(int i = 0; i < l; i++) {
+        int c = s[i];
+        if(c >= 'A' && c <= 'Z') {
+            c = c - 'A' + 'a';
+            s[i] = c;
+        }
+    }
+}
+
+void main() {
+    cstring msg = "Hallo User: ";
+    cstring name = "Kajetan";
+    string together = concat(msg, name);
+    string together2 = concat(together, name);
+    test(together2);
+    toLower(together);
+    test(together);
+    delete together;
+    delete together2;
+    
+    cstring a = name;
+    test(a);
+    test(a == name);
+    
+    cstring baum = "Kajetan";
+    test(name == baum);
+    test(compare(name, baum));
+    test(compare(name, "Kajetan"));
+    test(compare(name, "Kajetaa"));
+}

+ 8 - 0
tests/strings/string_lib.out

@@ -0,0 +1,8 @@
+Hallo User: KajetanKajetan
+hallo user: kajetan
+Kajetan
+true
+false
+true
+true
+false