summaryrefslogtreecommitdiff
path: root/test/tifmt.myr
blob: cd02fd5b9200289212b92028e411d81af4e0bb63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use std
use testr
use termdraw

const main = {
	testr.run([
		/* basic */
		[.name="cpstr", .fn={ctx
			checkfmt(ctx, "abc", [][:], "abc")
		}],
		[.name="multiarg", .fn={ctx
			var params = [`termdraw.Int 123, `termdraw.Str "abc"][:]
			checkfmt(ctx, "%p1%d%p2%s", params, "123abc")
		}],
		[.name="escape-pct", .fn={ctx
			checkfmt(ctx, "%%abc", [][:], "%abc")
		}],
		[.name="increment", .fn={ctx
			var params = [`termdraw.Int 123, `termdraw.Int 243, `termdraw.Str "abc"][:]
			checkfmt(ctx, "%i%p1%d-%p2%d-%p3%s", params, "124-244-abc")
		}],

		/* literals */
		[.name="litint-1",	.fn={ctx
			checkfmt(ctx, "%{5}%d", [`termdraw.Int 9][:], "5")
		}],
		[.name="litint-2",	.fn={ctx
			checkfmt(ctx, "%{5}%{6}%d", [`termdraw.Int 9][:], "6")
		}],

		/* numeric */
		[.name="arith-add",	.fn={ctx; binop(ctx, 'd', '+', 10, 21, "31")}],
		[.name="arith-sub",	.fn={ctx; binop(ctx, 'd', '-', 21, 10, "-11")}],
		[.name="arith-mul",	.fn={ctx; binop(ctx, 'd', '*', 10, 21, "210")}],
		[.name="arith-div",	.fn={ctx; binop(ctx, 'd', '/', 9, 100, "11")}],
		[.name="arith-mod",	.fn={ctx; binop(ctx, 'd', 'm', 9, 100, "1")}],

		/* bitwise */
		[.name="bit-and",	.fn={ctx; binop(ctx, 'x','&', 0x60, 0xc0, "40")}],
		[.name="bit-or",	.fn={ctx; binop(ctx, 'x','|', 0x60, 0x80, "e0")}],
		[.name="bit-xor",	.fn={ctx; binop(ctx, 'x','^', 0x60, 0xc0, "a0")}],
		/* not really a binop, but I can cheat. */
		[.name="bit-not",	.fn={ctx; binop(ctx, 'x','~', 0x00, 0xc0, "ffffff3f")}],

		/* logical */
		[.name="logic-and-tt",	.fn={ctx; binop(ctx, 'x','A', 10, 20, "1")}],
		[.name="logic-and-tf",	.fn={ctx; binop(ctx, 'x','A', 10, 0, "0")}],
		[.name="logic-and-ft",	.fn={ctx; binop(ctx, 'x','A', 0, 10, "0")}],
		[.name="logic-and-ff",	.fn={ctx; binop(ctx, 'x','A', 0, 0, "0")}],

		[.name="logic-or-tt",	.fn={ctx; binop(ctx, 'x','O', 10, 20, "1")}],
		[.name="logic-or-tf",	.fn={ctx; binop(ctx, 'x','O', 10, 0, "1")}],
		[.name="logic-or-ft",	.fn={ctx; binop(ctx, 'x','O', 0, 10, "1")}],
		[.name="logic-or-ff",	.fn={ctx; binop(ctx, 'x','O', 0, 0, "0")}],

		[.name="logic-not-t",	.fn={ctx; binop(ctx, 'x','!', 10, 20, "0")}],
		[.name="logic-not-f",	.fn={ctx; binop(ctx, 'x','!', 10, 0, "1")}],

		/* conditional */
		[.name="cond-basic-t", .fn={ctx
			var params = [`termdraw.Int 1][:]
			checkfmt(ctx, "%?%p1%ttrue%efalse%;", params, "true")
		}],
		[.name="cond-basic-f", .fn={ctx
			var params = [`termdraw.Int 0][:]
			checkfmt(ctx, "%?%p1%ttrue%efalse%;", params, "false")
		}],
		[.name="cond-nested-tt", .fn={ctx
			var params = [`termdraw.Int 1, `termdraw.Int 1][:]
			checkfmt(ctx, \
				"%?%p1%t%?%p2%ttruetrue%efalse%;%efalse%;", \
				params, "truetrue")
		}],
		[.name="cond-nested-tf", .fn={ctx
			var params = [`termdraw.Int 1, `termdraw.Int 0][:]
			checkfmt(ctx, \
				"%?%p1%t%?%p2%ttruetrue%etruefalse%;%efalsefalse%;", \
				params, "truefalse") 
		}],
		/* oddballs */
	][:])
}

const binop = {ctx, fmt, op, a0, a1, expected
	var f, r, s, sb

	s = std.fmt("%p1%p2%{}%{}", op, fmt)
	var params = [`termdraw.Int a0, `termdraw.Int a1][:]
	sb = std.mksb()
	termdraw.tifmt(&f, sb, s, params) 
	r = std.sbfin(sb)
	testr.check(ctx, std.sleq(r, expected), "expected {}, got {}", expected, r)
	std.slfree(s)
	std.slfree(r)
}

const checkfmt = {ctx, fmt, params, expected
	var f, sb, s

	sb = std.mksb()
	termdraw.tifmt(&f, sb, fmt, params) 
	s = std.sbfin(sb)
	testr.check(ctx, std.sleq(s, expected), "expected {}, got {}\n", expected, s)
	std.slfree(s)
}