void potion_test_tuple(CuTest *T) { PN tup = potion_tuple_with_size(P, 3); PN_TUPLE_AT(tup, 0) = PN_NIL; PN_TUPLE_AT(tup, 1) = PN_string; PN_TUPLE_AT(tup, 2) = tup; CuAssert(T, "tuple isn't a tuple", PN_IS_TUPLE(tup)); CuAssert(T, "tuple isn't a ref", PN_IS_PTR(tup)); CuAssertIntEquals(T, "tuple length is off", 3, PN_INT(potion_send(tup, potion_str(P, "length")))); }
void potion_test_sig(CuTest *T) { // test the simple parser entry point yy_sig, not the compiler transformation potion_sig_compile PN sig = potion_sig(P, "num1=N,num2=N"); CuAssert(T, "signature isn't a tuple", PN_IS_TUPLE(sig)); CuAssertIntEquals(T, "len=2", 2, PN_INT(PN_TUPLE_LEN(sig))); CuAssertIntEquals(T, "arity=2", 2, potion_sig_arity(P, sig)); CuAssertStrEquals(T, "num1=N,num2=N", //roundtrip PN_STR_PTR(potion_sig_string(P,0,sig))); CuAssertStrEquals(T, "(num1, 78, num2, 78)", PN_STR_PTR(potion_send(sig, PN_string))); CuAssertStrEquals(T, "num1", PN_STR_PTR(potion_send(PN_TUPLE_AT(sig,0), PN_string))); CuAssertIntEquals(T, "num1=N", 'N', PN_INT(PN_TUPLE_AT(sig,1))); CuAssertStrEquals(T, "num2", PN_STR_PTR(potion_send(PN_TUPLE_AT(sig,2), PN_string))); CuAssertIntEquals(T, "num2=N", 'N', PN_INT(PN_TUPLE_AT(sig,3))); sig = potion_sig(P, "x=N|y=N"); CuAssertStrEquals(T, "(x, 78, 124, y, 78)", PN_STR_PTR(potion_send(sig, PN_string))); CuAssertIntEquals(T, "arity=2", 2, potion_sig_arity(P, sig)); sig = potion_sig(P, "x=N,y=N|r=N"); CuAssert(T, "signature isn't a tuple", PN_IS_TUPLE(sig)); CuAssertStrEquals(T, "(x, 78, y, 78, 124, r, 78)", PN_STR_PTR(potion_send(sig, PN_string))); CuAssertStrEquals(T, "x=N,y=N|r=N", PN_STR_PTR(potion_sig_string(P,0,sig))); CuAssertIntEquals(T, "arity=3", 3, potion_sig_arity(P, sig)); { // roundtrips char *sigs[] = { "", "x,y", "x", "x=N", "x,y", "x=N,y=o", "x|y", "x|y,z", "x=o|y,z", "x|y=o", "x=N,y=N|r=N", /*optional */ "x:=1", "|x:=1", "x|y:=0", /* defaults */ "x,y.z", /* the dot */ }; int size = sizeof(sigs)/sizeof(char *); int i; for (i=0; i< size; i++) { CuAssertStrEquals(T, sigs[i], PN_STR_PTR(potion_sig_string(P,0,potion_sig(P, sigs[i])))); } } CuAssertIntEquals(T, "arity nil", 0, potion_sig_arity(P, PN_NIL)); // sig "" returns PN_FALSE, which throws an error //CuAssertIntEquals(T, "arity ''", 0, potion_sig_arity(P, potion_sig(P, ""))); CuAssertIntEquals(T, "arity x:=1", 1, potion_sig_arity(P, potion_sig(P, "x:=1"))); CuAssertIntEquals(T, "arity |x:=1", 1, potion_sig_arity(P, potion_sig(P, "|x:=1"))); CuAssertIntEquals(T, "arity x|y:=1", 2, potion_sig_arity(P, potion_sig(P, "x|y:=1"))); }
///\memberof PNProto /// string method of PNProto. ascii dump of a function definition PN potion_proto_string(Potion *P, PN cl, PN self) { vPN(Proto) t = (struct PNProto *)self; int x = 0; PN_SIZE num = 1; PN_SIZE numcols; PN out = potion_byte_str(P, "; function definition"); #ifdef JIT_DEBUG pn_printf(P, out, ": %p; %u bytes\n", t, PN_FLEX_SIZE(t->asmb)); #else pn_printf(P, out, ": %u bytes\n", PN_FLEX_SIZE(t->asmb)); #endif if (t->name) pn_printf(P, out, "; %s(", PN_STR_PTR(t->name)); else pn_printf(P, out, "; ("); potion_bytes_obj_string(P, out, potion_sig_string(P, cl, t->sig)); pn_printf(P, out, ") %ld registers\n", PN_INT(t->stack)); PN_TUPLE_EACH(t->paths, i, v, { pn_printf(P, out, ".path /"); v = PN_TUPLE_AT(t->values, PN_INT(v)); potion_bytes_obj_string(P, out, v); pn_printf(P, out, " ; %u\n", i); });
pn_printf(P, out, "; ("); PN_TUPLE_EACH(t->sig, i, v, { if (PN_IS_NUM(v)) { if (v == '.') pn_printf(P, out, ". "); else if (v == '|') pn_printf(P, out, "| "); else pn_printf(P, out, "=%c, ", (int)PN_INT(v)); } else potion_bytes_obj_string(P, out, v); }); pn_printf(P, out, ") %ld registers\n", PN_INT(t->stack)); PN_TUPLE_EACH(t->paths, i, v, { pn_printf(P, out, ".path /"); v = PN_TUPLE_AT(t->values, PN_INT(v)); potion_bytes_obj_string(P, out, v); pn_printf(P, out, " ; %u\n", i); }); PN_TUPLE_EACH(t->locals, i, v, { pn_printf(P, out, ".local "); potion_bytes_obj_string(P, out, v); pn_printf(P, out, " ; %u\n", i); }); PN_TUPLE_EACH(t->upvals, i, v, { pn_printf(P, out, ".upval "); potion_bytes_obj_string(P, out, v); pn_printf(P, out, " ; %u\n", i); }); PN_TUPLE_EACH(t->values, i, v, { pn_printf(P, out, ".value ");