ambrop7 13 лет назад
Родитель
Сommit
b8eef39125

+ 48 - 0
ncd/tests/alias.ncd

@@ -0,0 +1,48 @@
+process foo {
+    var("hello") x;
+    alias("x") y;
+    val_equal(y, "hello") a;
+    assert(a);
+
+    var("hello") x;
+    alias("x") y;
+    y->set("world");
+    val_equal(y, "world") a;
+    assert(a);
+
+    var("hello") x;
+    alias("x") y;
+    alias("y") z;
+    z->set("world");
+    val_equal(x, "world") a;
+    assert(a);
+
+    call("test", {"hello"}) c;
+    alias("c.x") x;
+    val_equal(x, "hello") a;
+    assert(a);
+
+    call("test", {"hello"}) c;
+    alias("c") x;
+    alias("x") y;
+    alias("y.x") z;
+    c.x->set("world");
+    val_equal(z, "world") a;
+    assert(a);
+
+    var("hello") x;
+    call("test2", {"_caller.x"}) c;
+    c.x->set("world");
+    val_equal(x, "world") a;
+    assert(a);
+
+    exit("0");
+}
+
+template test {
+    var(_arg0) x;
+}
+
+template test2 {
+    alias(_arg0) x;
+}

+ 69 - 0
ncd/tests/arithmetic.ncd

@@ -0,0 +1,69 @@
+process main {
+    num_lesser("6", "7") r;
+    assert(r);
+
+    num_lesser("7", "7") r;
+    not(r) a;
+    assert(a);
+
+    num_greater("7", "6") r;
+    assert(r);
+
+    num_greater("7", "7") r;
+    not(r) a;
+    assert(a);
+
+    num_lesser_equal("7", "7") r;
+    assert(r);
+
+    num_lesser_equal("8", "7") r;
+    not(r) a;
+    assert(a);
+
+    num_greater_equal("7", "7") r;
+    assert(r);
+
+    num_greater_equal("7", "8") r;
+    not(r) a;
+    assert(a);
+
+    num_equal("7", "7") r;
+    assert(r);
+
+    num_equal("6", "7") r;
+    not(r) a;
+    assert(a);
+
+    num_equal("7", "6") r;
+    not(r) a;
+    assert(a);
+
+    num_different("7", "6") a;
+    assert(a);
+
+    num_different("7", "007") a;
+    not(a) a;
+    assert(a);
+
+    num_add("4", "7") r;
+    strcmp(r, "11") a;
+    assert(a);
+
+    num_subtract("4", "3") r;
+    strcmp(r, "1") a;
+    assert(a);
+
+    num_multiply("4", "5") r;
+    strcmp(r, "20") a;
+    assert(a);
+
+    num_divide("7", "3") r;
+    strcmp(r, "2") a;
+    assert(a);
+
+    num_modulo("7", "3") r;
+    strcmp(r, "1") a;
+    assert(a);
+
+    exit("0");
+}

+ 19 - 0
ncd/tests/concat.ncd

@@ -0,0 +1,19 @@
+process main {
+    concat("Hello", "", "World") x;
+    strcmp(x, "HelloWorld") a;
+    assert(a);
+
+    concat("\x00\x00", "\x00") x;
+    strcmp(x, "\x00\x00\x00") a;
+    assert(a);
+
+    concatv({"Hello", "", "World"}) x;
+    strcmp(x, "HelloWorld") a;
+    assert(a);
+
+    concatv({"\x00\x00", "\x00"}) x;
+    strcmp(x, "\x00\x00\x00") a;
+    assert(a);
+
+    exit("0");
+}

+ 38 - 0
ncd/tests/escape_and_nulls.ncd

@@ -0,0 +1,38 @@
+process main {
+    value("ab\0") str1;
+    value("ab") str2;
+
+    strcmp(str1.length, "3") a;
+    assert(a);
+
+    strcmp(str2.length, "2") a;
+    assert(a);
+
+    strcmp(str1, str2) a;
+    not(a) a;
+    assert(a);
+
+    concat(str1, str2) strc;
+    strcmp(strc, "ab\0ab") a;
+    assert(a);
+
+    concat(str2, str1) strc;
+    strcmp(strc, "abab\0") a;
+    assert(a);
+
+    value("") str1;
+    value("\x00\x00") str2;
+    value("\x00\x01") str3;
+    value("\x01") str4;
+
+    val_lesser(str1, str2) a;
+    assert(a);
+
+    val_lesser(str2, str3) a;
+    assert(a);
+
+    val_lesser(str3, str4) a;
+    assert(a);
+
+    exit("0");
+}

+ 38 - 0
ncd/tests/if.ncd

@@ -0,0 +1,38 @@
+process foo {
+    If ("true") {
+        If ("truee") {
+            var("A1") y;
+        } else {
+            If ("true") {
+                var("A11") q;
+            } else {
+                var("A22") q;
+            } t;
+            var(t.q) y;
+        } s;
+        var(s.y) x;
+    } elif ("true") {
+        var("B") x;
+    } else {
+        var("C") x;
+    } ifs;
+
+    val_equal(ifs.x, "A11") a;
+    assert(a);
+
+    var("a") v;
+    If ("false") {
+        v->set("b");
+    };
+    val_equal(v, "a") a;
+    assert(a);
+
+    var("a") v;
+    If ("true") {
+        v->set("b");
+    };
+    val_equal(v, "b") a;
+    assert(a);
+
+    exit("0");
+}

+ 15 - 0
ncd/tests/implode.ncd

@@ -0,0 +1,15 @@
+process main {
+    implode("X", {"a", "bb", "", "c"}) str;
+    strcmp(str, "aXbbXXc") a;
+    assert(a);
+
+    implode("", {"a", "b"}) str;
+    strcmp(str, "ab") a;
+    assert(a);
+
+    implode("X", {}) str;
+    strcmp(str, "") a;
+    assert(a);
+
+    exit("0");
+}

+ 46 - 0
ncd/tests/logical.ncd

@@ -0,0 +1,46 @@
+process main {
+    var("true") t;
+    var("Faalse") f;
+
+    and(t, f) r;
+    strcmp(r, "false") a;
+    assert(a);
+
+    and(f, t) r;
+    strcmp(r, "false") a;
+    assert(a);
+
+    and(f, f) r;
+    strcmp(r, "false") a;
+    assert(a);
+
+    and(t, t) r;
+    strcmp(r, "true") a;
+    assert(a);
+
+    or(t, f) r;
+    strcmp(r, "true") a;
+    assert(a);
+
+    or(f, t) r;
+    strcmp(r, "true") a;
+    assert(a);
+
+    or(t, t) r;
+    strcmp(r, "true") a;
+    assert(a);
+
+    or(f, f) r;
+    strcmp(r, "false") a;
+    assert(a);
+
+    not(f) r;
+    strcmp(r, "true") a;
+    assert(a);
+
+    not(t) r;
+    strcmp(r, "false") a;
+    assert(a);
+
+    exit("0");
+}

+ 15 - 0
ncd/tests/netmask.ncd

@@ -0,0 +1,15 @@
+process main {
+    ipv4_prefix_to_mask("16") mask;
+    strcmp(mask, "255.255.0.0") a;
+    assert(a);
+
+    ipv4_mask_to_prefix("128.0.0.0") prefix;
+    strcmp(prefix, "1") a;
+    assert(a);
+
+    ipv4_net_from_addr_and_prefix("192.168.1.4", "24") net;
+    strcmp(net, "192.168.1.0") a;
+    assert(a);
+
+    exit("0");
+}

+ 26 - 0
ncd/tests/parse.ncd

@@ -0,0 +1,26 @@
+process main {
+    parse_number("awfa") x;
+    not(x.succeeded) a;
+    assert(a);
+
+    parse_number("023182") x;
+    assert(x.succeeded);
+    val_equal(x, "23182") a;
+    assert(a);
+
+    parse_ipv4_addr("192.168.61.007") x;
+    assert(x.succeeded);
+    val_equal(x, "192.168.61.7") a;
+    assert(a);
+
+    parse_value("{\"Hello World\", {}}") x;
+    assert(x.succeeded);
+    val_equal(x, {"Hello World", {}}) a;
+    assert(a);
+
+    parse_value("{syntax error") x;
+    not(x.succeeded) a;
+    assert(a);
+
+    exit("0");
+}

+ 138 - 0
ncd/tests/turing.ncd

@@ -0,0 +1,138 @@
+process main {
+    # Turing machine specification.
+    var("B") blank;
+    var([
+        {"0", "0"}:{"0", "0", "right"},
+        {"0", "1"}:{"1", "x", "right"},
+        {"1", "1"}:{"1", "1", "right"},
+        {"1", "0"}:{"2", "0", "right"},
+        {"2", "0"}:{"2", "0", "right"},
+        {"2", "1"}:{"3", "1", "right"},
+        {"3", "1"}:{"3", "1", "right"},
+        {"3", "0"}:{"4", "1", "left"},
+        {"3", "B"}:{"4", "1", "left"},
+        {"4", "1"}:{"4", "1", "left"},
+        {"4", "0"}:{"5", "0", "left"},
+        {"5", "0"}:{"5", "0", "left"},
+        {"5", "1"}:{"6", "1", "left"},
+        {"5", "x"}:{"h", "x", "stay"},
+        {"6", "1"}:{"6", "1", "left"},
+        {"6", "x"}:{"0", "x", "right"},
+        {"6", "0"}:{"0", "0", "right"}
+    ]) rules;
+    var("0") initial_state;
+    var({}) initial_tape_left;
+    var({
+        "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
+        "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
+        "0", "0",
+        "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
+        "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"
+    }) initial_tape_right;
+
+    # Perform the computation, stopping when no rule matches.
+    call("turing", {blank, rules, initial_state, initial_tape_left, initial_tape_right}) results;
+
+    # Check results.
+
+    val_equal(results.tape_left, {"B"}) a;
+    assert(a);
+
+    val_equal(results.tape_right,
+        {"x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x",
+         "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x",
+         "0", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
+         "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
+         "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
+         "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
+         "1", "1"}
+    ) a;
+    assert(a);
+
+    val_equal({results.side, results.pos}, {"right", "55"}) a;
+    assert(a);
+
+    val_equal(results.state, "h") a;
+    assert(a);
+
+    exit("0");
+}
+
+template turing {
+    alias("_arg0") blank;
+    value(_arg1) rules;
+    alias("_arg2") initial_state;
+    alias("_arg3") initial_tape_left;
+    alias("_arg4") initial_tape_right;
+
+    # Head state.
+    var(initial_state) state;
+
+    # Tape. Positions go like this: ... L2 L1 L0 R0 R1 R2 ... 
+    value(initial_tape_left) tape_left;
+    value(initial_tape_right) tape_right;
+
+    # Make sure each side of the tape has at least one symbol so we can flip easily.
+    tape_left->insert(tape_left.length, blank);
+    tape_right->insert(tape_right.length, blank);
+
+    # Head position.
+    var("right") side;
+    var("0") pos;
+
+    # Enter loop.
+    blocker() loop_blk;
+    loop_blk->up();
+    loop_blk->use();
+
+    # Get symbol under head.
+    concat("tape_", side) tape_name;
+    alias(tape_name) cur_tape;
+    cur_tape->get(pos) symbol;
+
+    # Look for a matching rule.
+    rules->try_get({state, symbol}) rule;
+
+    If (rule.exists) {
+        # Extract directions from rule.
+        rule->get("0") new_state;
+        rule->get("1") new_symbol;
+        rule->get("2") move;
+
+        # Change head state.
+        state->set(new_state);
+
+        # Replace symbol under head.
+        cur_tape->remove(pos);
+        cur_tape->insert(pos, new_symbol);
+
+        # Branch based on how we move.
+        strcmp(move, side) is_outside;
+        strcmp(move, "stay") is_stay;
+        strcmp(pos, "0") is_zero;
+        If (is_outside) {
+            # Increment position.
+            num_add(pos, "1") new_pos;
+            pos->set(new_pos);
+
+            # If the new position is out of range, extend tape.
+            strcmp(pos, cur_tape.length) need_extend;
+            If (need_extend) {
+                cur_tape->insert(pos, blank);
+            };
+        } elif (is_stay) {
+            # Nop.
+            getargs();
+        } elif (is_zero) {
+            # Flip side, leave pos at zero.
+            side->set(move);
+        } else {
+            # Decrement position.
+            num_subtract(pos, "1") new_pos;
+            pos->set(new_pos);
+        };
+
+        # Continue loop.
+        loop_blk->downup();
+    };
+}

+ 0 - 1
ncd/tests/value.ncd

@@ -167,7 +167,6 @@ process main {
     val_equal(vB2, "vB") a;
     assert(a);
 
-    println("succeeded");
     exit("0");
 }
 

+ 25 - 0
ncd/tests/value_substr.ncd

@@ -0,0 +1,25 @@
+process foo {
+    value("0123456789") str;
+
+    str->substr("0") sub;
+    strcmp(sub, str) a;
+    assert(a);
+
+    str->substr("1") sub;
+    strcmp(sub, "123456789") a;
+    assert(a);
+
+    str->substr("1", "0") sub;
+    strcmp(sub, "") a;
+    assert(a);
+
+    str->substr("1", "9") sub;
+    strcmp(sub, "123456789") a;
+    assert(a);
+
+    str->substr("1", "8") sub;
+    strcmp(sub, "12345678") a;
+    assert(a);
+
+    exit("0");
+}