summaryrefslogtreecommitdiff
path: root/sys/src/cmd/ktrans/test.c
blob: 77d416b0c5ce7d0cda63f9a54205dba9c391af96 (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
106
107
108
109
110
111
#include <u.h>
#include <libc.h>

struct {
	char *input;
	Rune *expect;
} set[] = {
	"n", L"ん",
	"no", L"の",
	"nno", L"んの",
	"neko", L"猫",
	"neko", L"ねこ",
	"watashi", L"私",
	"tanoShi", L"楽し",
	"oreNO", L"俺の",

	"watashiHAmainichi35funijouaruIte,saraNI10fundenshaNInoTtegakkouNIkayoImasu.\nkenkouNOijiNImoyakuDAtteimasuga,nakanakatanoshiImonodesu.\n",
	L"私は毎日35分以上歩いて、更に10分電車に乗って学校に通います。\n健康の維持にも役だっていますが、なかなかたのしいものです。\n",
};

char*
makemsg(char *s)
{
	char *out, *d;
	int i, n;

	n = strlen(s) + 1;
	out = mallocz(n * 3, 1);
	for(d = out, i = 0; i < n; i++){
		*d++ = 'c';
		if(i == n - 1)
			*d++ = 1;
		else
			*d++ = s[i];
		*d++ = '\0';
	}
	return out;
}

void
main(int argc, char **argv)
{
	int io1[2], io2[2];
	int i;
	int n;
	char *p, *e;
	static char buf[256];
	Rune r;
	char *to;
	char *bin;
	static Rune stack[256];
	static int nstack;

	if(argc < 2)
		sysfatal("usage: %s binary", argv[0]);

	bin = argv[1];
	pipe(io1);
	pipe(io2);
	if(fork() == 0){
		dup(io1[0], 0);
		dup(io2[0], 1);
		close(io1[1]); close(io2[1]);
		execl(bin, "ktrans", "-l", "jp", "-G", nil);
		sysfatal("exec: %r");
	}
	close(io1[0]); close(io2[0]);
	for(i = 0; i < nelem(set); i++){
		nstack = 0;
		stack[nstack] = 0;
		to = makemsg(set[i].input);
		for(;;){
			write(io1[1], to, strlen(to) + 1);
			if(to[1] == 1)
					break;
			to += strlen(to)+1;
		}
		for(;;) {
			n = read(io2[1], buf, sizeof buf);
			if(n <= 0)
				break;
			e = buf + n;
			for(p = buf; p < e; p += (strlen(p)+1)){
				assert(*p == 'c');
				chartorune(&r, p+1);
				switch(r){
				case 1:
					goto Verify;
				case 8:
					if(nstack == 0)
						sysfatal("buffer underrun");
					nstack--;
					stack[nstack] = 0;
					break;
				default:
					stack[nstack++] = r;
					stack[nstack] = 0;
					break;
				}
			}
		}
	Verify:
		if(runestrcmp(set[i].expect, stack) != 0){
			fprint(2, "%S != %S\n", stack, set[i].expect);
			exits("fail");
		}
	}
	close(io1[1]); close(io2[1]);
	waitpid();
	exits(nil);
}