added test cases; bug fixes
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 16 Sep 2020 13:45:55 +0000 (08:45 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 16 Sep 2020 13:45:55 +0000 (08:45 -0500)
core/i32map.onyx
onyxcmd.js
runtests.sh
tests/general1 [new file with mode: 0644]
tests/general1.onyx [new file with mode: 0644]
tests/hello_world
tests/i32map [new file with mode: 0644]
tests/i32map.onyx [new file with mode: 0644]

index d8ba4675e267936d7930fa0f2d7f4a8ef32ae919..05f6e5662aa5ac553e667e6aa444fd93aac49c99 100644 (file)
@@ -57,8 +57,8 @@ i32map_delete :: proc (use imap: ^I32Map($T), key: i32) {
     lr := i32map_lookup(imap, key);
     if lr.entry_index < 0 do return;
 
-    if lr.entry_prev < 0   do hashes[lr.hash_index] = entries[lr.entry_index].next;
-    else                   do hashes[lr.entry_prev] = entries[lr.entry_index].next;
+    if lr.entry_prev < 0   do hashes[lr.hash_index]       = entries[lr.entry_index].next;
+    else                   do entries[lr.entry_prev].next = entries[lr.entry_index].next;
 
     if lr.entry_index == entries.count - 1 {
         array_pop(^entries);
index 3b99f343795bba08995a858e5a049461d21bdda2..cd0075247c9ba6d38143c99366928df410f8d2d1 100644 (file)
@@ -8,7 +8,7 @@ const ENV = {
         print_str(ptr, len) {
             const data = new Uint8Array(wasm_memory, ptr, len);
             const str  = new TextDecoder().decode(data);
-            console.log(str);
+            process.stdout.write(str);
         }
     }   
 }
index dad60902f1892c6bdbdc414a0a04c678116033bf..4c01bce5be27df9c0ab5f1f9acc3448530ce9c97 100755 (executable)
@@ -21,9 +21,12 @@ for test_file in ./tests/*.onyx ; do
 
        if ! diff ./tmpoutput "./tests/$name" >/dev/null; then
                echo "❌ Test output did not match."
+               diff ./tmpoutput "./tests/$name"
                success=0
                continue
        fi
+
+       rm "./tests/$name.wasm"
 done
 rm ./tmpoutput
 
diff --git a/tests/general1 b/tests/general1
new file mode 100644 (file)
index 0000000..23a35a4
--- /dev/null
@@ -0,0 +1,68 @@
+a was 4, 5, or 6
+a was something else.
+This is a test.
+Here are some numbers:
+1 2 3 4 5 
+
+Got this many args: 7
+1203
+Got this many args: 7
+1203
+5
+Some information about those numbers:
+Min: 1
+Max: 5
+Sum: 15
+
+20
+Evens from 6 to 34:
+6 8 10 12 14 16 18 20 22 24 26 28 30 32 
+
+Array details:
+       Size: 0
+       Capacity: 4
+       Data ptr: 0x10A38
+       Size of elements: 4
+       Alignment of elements: 4
+
+
+Array details:
+       Size: 0
+       Capacity: 4
+       Data ptr: 0x10A58
+       Size of elements: 8
+       Alignment of elements: 8
+
+0 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99
+0 5 10 15 20 4 9 14 19 3 8 13 18 2 7 12 17 1 6 11 16 0 5 10 15 20 4 9 14 19 3 8 13 18 2 7 12 17 1 6 11 16 0 5 10 15 20 4 9 14 19 3 8 13 18 2 7 12 17 1 6 11 16 0 5 10 15 20 4 9 14 19 3 8 13 18 2 7 12 17 1 6 11 16 0 5 10 15 20 4 9 14 19 3 8 13 18 2 7 12
+A has 22? false
+Vec3(0, 0, 0) Vec3(1, 1, 1) Vec3(2, 4, 8) Vec3(3, 9, 27) Vec3(4, 16, 64) Vec3(5, 25, 125) Vec3(6, 36, 216) Vec3(7, 49, 343) Vec3(8, 64, 512) Vec3(9, 81, 729) Vec3(10, 100, 1000) Vec3(11, 121, 1331) Vec3(12, 144, 1728) Vec3(13, 169, 2197) Vec3(14, 196, 2744) Vec3(15, 225, 3375) Vec3(16, 256, 4096) Vec3(17, 289, 4913) Vec3(18, 324, 5832) Vec3(19, 361, 6859) Vec3(20, 400, 8000) Vec3(21, 441, 9261) Vec3(22, 484, 10648) Vec3(23, 529, 12167) Vec3(24, 576, 13824) Vec3(25, 625, 15625) Vec3(26, 676, 17576) Vec3(27, 729, 19683) Vec3(28, 784, 21952) Vec3(29, 841, 24389) Vec3(30, 900, 27000) Vec3(31, 961, 29791) Vec3(32, 1024, 32768) Vec3(33, 1089, 35937) Vec3(34, 1156, 39304) Vec3(35, 1225, 42875) Vec3(36, 1296, 46656) Vec3(37, 1369, 50653) Vec3(38, 1444, 54872) Vec3(39, 1521, 59319) Vec3(40, 1600, 64000) Vec3(41, 1681, 68921) Vec3(42, 1764, 74088) Vec3(43, 1849, 79507) Vec3(44, 1936, 85184) Vec3(45, 2025, 91125) Vec3(46, 2116, 97336) Vec3(47, 2209, 103823) Vec3(48, 2304, 110592) Vec3(49, 2401, 117649) Vec3(50, 2500, 125000) Vec3(51, 2601, 132651) Vec3(52, 2704, 140608) Vec3(53, 2809, 148877) Vec3(54, 2916, 157464) Vec3(55, 3025, 166375) Vec3(56, 3136, 175616) Vec3(57, 3249, 185193) Vec3(58, 3364, 195112) Vec3(59, 3481, 205379) Vec3(60, 3600, 216000) Vec3(61, 3721, 226981) Vec3(62, 3844, 238328) Vec3(63, 3969, 250047) Vec3(64, 4096, 262144) Vec3(65, 4225, 274625) Vec3(66, 4356, 287496) Vec3(67, 4489, 300763) Vec3(68, 4624, 314432) Vec3(69, 4761, 328509) Vec3(70, 4900, 343000) Vec3(71, 5041, 357911) Vec3(72, 5184, 373248) Vec3(73, 5329, 389017) Vec3(74, 5476, 405224) Vec3(75, 5625, 421875) Vec3(76, 5776, 438976) Vec3(77, 5929, 456533) Vec3(78, 6084, 474552) Vec3(79, 6241, 493039) Vec3(80, 6400, 512000) Vec3(81, 6561, 531441) Vec3(82, 6724, 551368) Vec3(83, 6889, 571787) Vec3(84, 7056, 592704) Vec3(85, 7225, 614125) Vec3(86, 7396, 636056) Vec3(87, 7569, 658503) Vec3(88, 7744, 681472) Vec3(89, 7921, 704969) Vec3(90, 8100, 729000) Vec3(91, 8281, 753571) Vec3(92, 8464, 778688) Vec3(93, 8649, 804357) Vec3(94, 8836, 830584) Vec3(95, 9025, 857375) Vec3(96, 9216, 884736) Vec3(97, 9409, 912673) Vec3(98, 9604, 941192) Vec3(99, 9801, 970299) 
+0x11CE8 0x11CF4 0x11D00 0x11D0C 0x11D18 0x11D24 0x11D30 0x11D3C 0x11D48 0x11D54 0x11D60 0x11D6C 0x11D78 0x11D84 0x11D90 0x11D9C 0x11DA8 0x11DB4 0x11DC0 0x11DCC 0x11DD8 0x11DE4 0x11DF0 0x11DFC 0x11E08 0x11E14 0x11E20 0x11E2C 0x11E38 0x11E44 0x11E50 0x11E5C 0x11E68 0x11E74 0x11E80 0x11E8C 0x11E98 0x11EA4 0x11EB0 0x11EBC 0x11EC8 0x11ED4 0x11EE0 0x11EEC 0x11EF8 0x11F04 0x11F10 0x11F1C 0x11F28 0x11F34 0x11F40 0x11F4C 0x11F58 0x11F64 0x11F70 0x11F7C 0x11F88 0x11F94 0x11FA0 0x11FAC 0x11FB8 0x11FC4 0x11FD0 0x11FDC 0x11FE8 0x11FF4 0x12000 0x1200C 0x12018 0x12024 0x12030 0x1203C 0x12048 0x12054 0x12060 0x1206C 0x12078 0x12084 0x12090 0x1209C 0x120A8 0x120B4 0x120C0 0x120CC 0x120D8 0x120E4 0x120F0 0x120FC 0x12108 0x12114 0x12120 0x1212C 0x12138 0x12144 0x12150 0x1215C 0x12168 0x12174 0x12180 0x1218C 
+1854 1858 1862 1866 1870 1874 1878 1882 1886 1890 1894 1898 
+20 20 20 20 20 19 19 19 19 19 18 18 18 18 18 17 17 17 17 16 16 16 16 15 15 15 15 15 14 14 14 14 14 13 13 13 13 13 12 12 12 12 12 11 11 11 11 10 10 10 10 10 9 9 9 9 9 8 8 8 8 8 7 7 7 7 7 6 6 6 6 5 5 5 5 5 4 4 4 4 4 3 3 3 3 3 2 2 2 2 2 1 1 1 1 0 0 0 0 0
+297 294 291 288 285 282 279 276 273 270 267 264 261 258 255 252 249 246 243 240 237 234 231 228 225 222 219 216 213 210 207 204 201 198 195 192 189 186 183 180 177 174 171 168 165 162 159 156 153 150 147 144 141 138 135 132 129 126 123 120 117 114 111 108 105 102 99 96 93 90 87 84 81 78 75 72 69 66 63 60 57 54 51 48 45 42 39 36 33 30 27 24 21 18 15 12 9 6 3 0
+After adding...
+
+Array details:
+       Size: 100
+       Capacity: 128
+       Data ptr: 0x116C8
+       Size of elements: 4
+       Alignment of elements: 4
+
+
+Array details:
+       Size: 100
+       Capacity: 128
+       Data ptr: 0x118D8
+       Size of elements: 8
+       Alignment of elements: 8
+
+Array A sum: 999
+
+Has ^a[20]? true
+Has null? false
+Value at ^a[50]: 0x11A68 == 0x11A68
+Deleteing ^a[20]
+Has ^a[20]? false
+Clearing SOA...
diff --git a/tests/general1.onyx b/tests/general1.onyx
new file mode 100644 (file)
index 0000000..3df1138
--- /dev/null
@@ -0,0 +1,282 @@
+package main
+
+#include_file "core/std/js"
+
+use package core
+
+print_arr_details :: proc (arr: ^[..] $T) {
+    print("\nArray details:\n\tSize: ");
+    print(arr.count);
+    print("\n\tCapacity: ");
+    print(arr.capacity);
+    print("\n\tData ptr: ");
+    print(cast(^void) arr.data);
+    print("\n\tSize of elements: ");
+    print(sizeof T);
+    print("\n\tAlignment of elements: ");
+    print(alignof T);
+    print("\n\n");
+}
+
+print_vec :: proc (v: Vec3) #add_overload print {
+    print("Vec3(");
+    print(v.x);
+    print(", ");
+    print(v.y);
+    print(", ");
+    print(v.z);
+    print(")");
+}
+
+// This demonstrates that we have something similar to static 'duck' typing.
+get_count :: proc (x: $T) -> u32 do return x.count;
+
+
+// Because of the static duck typing, this will pass as an
+// array/slice in most places.
+Dummy :: struct {
+    count : u32 = 5;
+    data  : [5] u32;
+}
+
+
+/* This demos some of the power you have with the polymorphic types */
+compose :: proc (a: A, f: proc ($A) -> $B, g: proc (B) -> $C) -> C {
+    return a |> f() |> g();
+}
+
+
+SOA :: struct {
+    a  : [..] i32;
+    b  : [..] i64;
+    c  : [..] Vec3;
+}
+
+soa_init :: proc (s: ^SOA) {
+    array_init(^s.a);
+    array_init(^s.b);
+    array_init(^s.c);
+}
+
+soa_deinit :: proc (s: ^SOA) {
+    array_free(^s.a);
+    array_free(^s.b);
+    array_free(^s.c);
+}
+
+get_range :: proc (arr: ^[..] $T) -> range {
+    return 0 .. arr.count;
+}
+
+// NOTE: This function will be very useful for for loops. i.e.
+//        for i: 0 .. 100 |> by(2) {
+//            ...
+//        }
+by :: proc (r: range, s: u32) -> range {
+    return range.{ low = r.low, high = r.high, step = s };
+}
+
+switch_demo :: proc {
+    switch a := 4; a {
+        case 4, 5, 6 {
+            print("a was 4, 5, or 6\n");
+            fallthrough fallthrough;
+        }
+
+        case 10 do print("a was 10\n");
+
+        case #default {
+            print("a was something else.\n");
+        }
+    }
+}
+
+vararg_test :: proc (prefix: string, nums: ..i32) {
+    print(prefix);
+    for num: nums {
+        print(num);
+        print(" ");
+    }
+}
+
+NumInfo :: struct {
+    min : i32;
+    max : i32;
+    sum : i32;
+}
+
+get_num_info :: proc (nums: ..i32) -> NumInfo {
+    ni : NumInfo;
+
+    ni.min = nums[0];
+    for num: nums do if num < ni.min do ni.min = num;
+
+    ni.max = nums[0];
+    for num: nums do if num > ni.max do ni.max = num;
+
+    ni.sum = 0;
+    for num: nums do ni.sum += num;
+
+    return ni;
+}
+
+print_strings :: proc (ss: ..string) {
+    for s: ss do print_string(s);
+}
+
+multi_max :: proc (nums: ..$T) -> T {
+    print("Got this many args: ");
+    print(nums.count);
+    print("\n");
+
+    max := nums[0];
+    for num: nums do if num > max do max = num;
+    return max;
+}
+
+weird_sum :: proc (n1: $T, others: ..T) -> T {
+    s := n1;
+    for n: others do s += n;
+    return s;
+}
+
+main :: proc (args: [] cstring) {
+    switch_demo();
+
+    print_strings("This ", "is ", "a ", "test.\n");
+
+    vararg_test("Here are some numbers:\n", 1, 2, 3, 4, 5);
+    print("\n\n");
+
+    multi_max(4, 2, 76, 3, 1203, 2, 4) |> print();
+    print("\n");
+
+    multi_max(4l, 2l, 76l, 3l, 1203l, 2l, 4l) |> print();
+    print("\n");
+
+    weird_sum(4, 1) |> print();
+    print("\n");
+
+    ni := get_num_info(1, 2, 3, 4, 5);
+    print("Some information about those numbers:\n");
+    print("Min: ");
+    print(ni.min);
+    print("\nMax: ");
+    print(ni.max);
+    print("\nSum: ");
+    print(ni.sum);
+    print("\n\n");
+
+    res := compose(5, proc (x: i32) -> i32 do return x * 3;,
+                      proc (x: i32) -> i32 do return x + 5;);
+    print(res);
+    print("\n");
+
+    s : SOA;
+    soa_init(^s);
+    defer {
+        print("Clearing SOA...\n");
+        soa_deinit(^s);
+    }
+
+    print("Evens from 6 to 34:\n");
+    for i: 6 .. 34 |> by(2) {
+        print(i);
+        print(" ");
+    }
+    print("\n");
+
+    print_arr_details(^s.a);
+    print_arr_details(^s.b);
+
+    for i: 0 .. 100 {
+        array_push(^s.a, (5 * i) % 21);
+        array_push(^s.b, 3l * cast(i64) i);
+        array_push(^s.c, Vec3.{ i, i * i, i * i * i });
+    }
+
+    r := ^s.a |> get_range() |> by(3);
+    print(r);
+    print_array(^s.a);
+    print("A has 22? ");
+    print(array_contains(^s.a, 22));
+    print("\n");
+
+    // NOTE: Iterating by value
+    for vec: s.c {
+        print(vec);
+        print(" ");
+    }
+    print("\n");
+
+    // NOTE: Iterating by pointer
+    for ^vec: s.c {
+        print(cast(^void) vec);
+        print(" ");
+    }
+    print("\n");
+
+    small : [12] i32;
+    for ^it: small do *it = 1234 + cast(u32) it;
+
+    for it: small {
+        print(it);
+        print(" ");
+    }
+    print("\n");
+
+
+    array_sort(^s.a, cmp_dec);
+    array_sort(^s.b, cmp_dec);
+
+    print_array(^s.a);
+    print_array(^s.b);
+
+    print("After adding...\n");
+    print_arr_details(^s.a);
+    print_arr_details(^s.b);
+
+    print("Array A sum: ");
+    print(array_fold(^s.a, 0, proc (x: i32, acc: i32) -> i32 do return x + acc;));
+    print("\n\n");
+
+    map : PtrMap;
+    ptrmap_init(^map, 50);
+    defer ptrmap_free(^map);
+
+    for i: 0 .. 100 do ptrmap_put(^map, ^s.a[i], ^s.b[i]);
+
+    print("Has ^a[20]? ");
+    print(ptrmap_has(^map, ^s.a[20]));
+    print("\n");
+
+    print("Has null? ");
+    print(ptrmap_has(^map, null));
+    print("\n");
+
+    print("Value at ^a[50]: ");
+    print(cast(^void) ptrmap_get(^map, ^s.a[50]));
+    print(" == ");
+    print(cast(^void) (^s.b[50]));
+    print("\n");
+
+    print("Deleteing ^a[20]\n");
+    ptrmap_delete(^map, ^s.a[20]);
+
+    print("Has ^a[20]? ");
+    print(ptrmap_has(^map, ^s.a[20]));
+    print("\n");
+}
+
+
+Vec3 :: struct {
+    x: i32;
+    y: i32;
+    z: i32;
+}
+
+cmp_vec3 :: proc (v1: Vec3, v2: Vec3) -> i32 {
+    if v1.x != v2.x do return v1.x - v2.x;
+    if v1.y != v2.y do return v1.y - v2.y;
+    return v1.z - v2.z;
+}
index 8ab686eafeb1f44702738c8b0f24f2567c36da6d..b45ef6fec89518d314f546fd6c3025367b721684 100644 (file)
@@ -1 +1 @@
-Hello, World!
+Hello, World!
\ No newline at end of file
diff --git a/tests/i32map b/tests/i32map
new file mode 100644 (file)
index 0000000..092cdba
--- /dev/null
@@ -0,0 +1,6 @@
+true
+false
+Hello World!
+false
+false
+Freeing map
diff --git a/tests/i32map.onyx b/tests/i32map.onyx
new file mode 100644 (file)
index 0000000..297f86d
--- /dev/null
@@ -0,0 +1,37 @@
+package main
+
+#include_file "core/std/js"
+
+use package core
+
+main :: proc (args: [] cstring) {
+    imap : I32Map(^string);
+    i32map_init(^imap);
+    defer {
+        print("Freeing map\n");
+        i32map_free(^imap);
+    }
+
+    hello := "Hello ";
+    world := "World!";
+
+    i32map_put(^imap, 50, ^hello);
+    i32map_put(^imap, 1234, ^world);
+
+    print(i32map_has(^imap, 50));
+    print("\n");
+    print(i32map_has(^imap, 51));
+    print("\n");
+
+    print(*i32map_get(^imap, 50));
+    print(*i32map_get(^imap, 1234));
+    print("\n");
+
+    i32map_delete(^imap, 50);
+    print(i32map_has(^imap, 50));
+    print("\n");
+
+    i32map_clear(^imap);
+    print(i32map_has(^imap, 1234));
+    print("\n");
+}