#!/bin/sh
# Copyright 2018 by Denys Vlasenko
# Licensed under GPLv2 or later, see file LICENSE in this source tree.

. ./testing.sh

# testing "test name" "command" "expected result" "file input" "stdin"

testing "bc comment 1" \
	"bc" \
	"3\n" \
	"" "1 /* comment */ + 2"

testing "bc comment 2: /*/ is not a closed comment" \
	"bc" \
	"4\n" \
	"" "1 /*/ + 2 */ + 3"

testing "bc comment 3: unterminated #comment" \
	"bc" \
	"" \
	"" "#foo"  # no trailing newline

testing "bc backslash 1" \
	"bc" \
	"3\n" \
	"" "1 \\\\\n + 2"

testing "bc string 1" \
	"bc" \
	"STR\n" \
	"" "\"STR\n\""

testing "bc read() 4<EOF>" \
	"bc input" \
	"4\n" \
	"read();halt" "4"

testing "bc read()^2" \
	"bc input" \
	"16\n" \
	"read()^2;halt" "4\n"

testing "bc read()*read()" \
	"bc input" \
	"20\n" \
	"read()*read();halt" "4\n5"

testing "bc if 0 else" \
	"bc" \
	"2\n9\n" \
	"" "if (0) 1 else 2; 9"

testing "bc if 1 else" \
	"bc" \
	"1\n9\n" \
	"" "if (1) 1 else 2; 9"

testing "bc if 1 if 1 else else" \
	"bc" \
	"1\n9\n" \
	"" "if (1) if (1) 1 else 2 else 3; 9"

testing "bc if 0 else if 1" \
	"bc" \
	"2\n9\n" \
	"" "if (0) 1 else if (1) 2; 9"

testing "bc for(;;)" \
	"bc" \
	"2\n3\n2\n9\n" \
	"" "i=2; for (;;) { 2; if(--i==0) break; 3; }; 9"

testing "bc for (;cond;)" \
	"bc" \
	"1\n2\n3\n9\n" \
	"" "i=0; for(;i<3;)++i; 9"

testing "bc for (;cond;upd)" \
	"bc" \
	"1\n2\n3\n9\n" \
	"" "i=1; for(;i<4;i++)i; 9"

testing "bc for (init;cond;upd)" \
	"bc" \
	"1\n2\n3\n9\n" \
	"" "for(i=1;i<4;i++)i; 9"

testing "bc define auto" \
	"bc" \
	"8\n9\n" \
	"" "define w() { auto z; return 8; }; w(); 9"

testing "bc define with body on next line" \
	"bc" \
	"8\n9\n" \
	"" "define w()\n{ auto z; return 8; }\nw()\n9"

testing "bc if(cond)<NL>" \
	"bc" \
	"9\n" \
	"" "if(0)\n3\n9"

testing "bc if(cond) stmt else<NL>" \
	"bc" \
	"4\n9\n" \
	"" "if(0)3 else\n4\n9"

testing "bc while(cond)<NL>" \
	"bc" \
	"8\n7\n6\n5\n4\n3\n2\n1\n9\n" \
	"" "i=9;while(--i)\ni\n9"

testing "bc ifz does not match if keyword" \
	"bc" \
	"1\n2\n2\n3\n" \
	"" "ifz=1;ifz\n++ifz;ifz++\nifz"

testing "bc print 1,2,3" \
	"bc" \
	"123" \
	"" "print 1,2,3"

testing "bc { print 1 }" \
	"bc" \
	"1" \
	"" "{ print 1 }"

testing "bc nested loops and breaks" \
	"bc" \
	"\
11
21
31
22
12
99
" \
	"" "\
if(1) {
	11
	while(1) {
		21
		while(1) {
			31
			break
			32
		}
		22
		break
		23
	}
	12
} else {
	88
}
99
"

testing "bc continue in if" \
	"bc" \
	"\
11
21
11
31
99
" \
	"" "\
i=2
while(i--) {
        11
        if(i) {
                21
                continue
                22
        } else {
                31
                continue
                32
	}
        12
}
99
"

testing "bc continue in for" \
	"bc" \
	"\
1
77
2
99
" \
	"" "\
for(i=1; i<3; i++) {
    i
    if(i==2) continue
    77
}
99
"

tar xJf bc_large.tar.xz

for f in bc*.bc; do
	r="`basename "$f" .bc`_results.txt"
	test -f "$r" || continue
	# testing "test name" "command" "expected result" "file input" "stdin"
	testing "bc -lq $f" \
		"{ { bc -lq $f 2>&1; echo E:\$? >&2; } | diff -u - $r; echo E:\$?; } 2>&1" \
		"E:0\nE:0\n" \
		"" ""
done

exit $FAILCOUNT