1#!/bin/sh
2
3echo T.utf: tests of utf functions
4
5awk=${awk-../a.out}
6
7$awk '
8BEGIN {
9	FS = "\t"
10	awk = "../a.out"
11}
12NF == 0 || $1 ~ /^#/ {
13	next
14}
15$1 ~ /try/ {	# new test
16	nt++
17	sub(/try [a-zA-Z_0-9]+ /, "")
18	prog = $0
19	printf("try %3d %s\n", nt, prog)
20	prog = sprintf("%s -F\"\\t\" '"'"'%s'"'"'", awk, prog)
21	# print "prog is", prog
22	nt2 = 0
23	while (getline > 0) {
24		if (NF == 0)	# blank line terminates a sequence
25			break
26		input = $1
27		for (i = 2; i < NF; i++)	# input data
28			input = input "\t" $i
29		test = sprintf("./echo '"'"'%s'"'"' | %s >foo1; ",
30			input, prog)
31		if ($NF == "\"\"")
32			output = ">foo2;"
33		else
34			output = sprintf("./echo '"'"'%s'"'"' >foo2; ", $NF)
35		gsub(/\\t/, "\t", output)
36		gsub(/\\n/, "\n", output)
37		run = sprintf("diff foo1 foo2 || echo test %d.%d failed",
38			nt, ++nt2)
39		# print  "input is", input
40		# print  "test is", test
41		# print  "output is", output
42		# print  "run is", run
43		system(test output run)
44	}
45	tt += nt2
46}
47END { print tt, "tests" }
48' <<\!!!!
49# General format:
50# try program as rest of line
51# $1	$2	$3	output1  (\t for tab, \n for newline,
52# $1	$2	$3	output2  ("" for null)
53# ... terminated by blank line
54
55# try another program...
56
57try length { print length($1) }
58	0
59a	1
60���������������������	7
61������������	6
62������������������	6
63������������������	6
64������������������	6
65����	1
66���� finger	8
67��������	4
68������	3
69����	2
70����������	5
71������	3
72��������	4
73���������������������	7
74���������	3
75������	2
76���������	3
77������������	6
78������	3
79������������	6
80
81try index { print index($1, $2) }
82abc	a	1
83abc	b	2
84abc	x	0
85������������������	""	0
86������������������	���	6
87������������������	������	2
88������������������	x	0
89���x���������������	x	2
90���� finger������������������������������������������������������������	f	3
91���� finger����	r����	8
92
93try substr { print substr($0, 2, 3) }
94abcdef	bcd
95�������� ���������� ��	������
96����	��
97������ ������������	��� ���
98x��y����������	��y��
99
100try rematch { print $1 ~ $2 }
101abc	a	1
102abc	x	0
103������������������������������	���	1
104������������������������������	���.*���	1
105������������������������������	���.*���	0
106��������	��+	1
107
108# replace first occurrence of $2 by $3 in $1
109try sub { n = sub($2, $3, $1); print n, $1 }
110abcdef	bc	XYZ	1 aXYZdef
111abcdef	xy	XYZ	0 abcdef
112���������������������	���	NO	1 NO������������������
113���� finger	����.*g	FING	1 FINGer
114������������	.	x	1 x����������
115
116# replace all occurrences of $2 by $3 in $1
117try gsub { n = gsub($2, $3, $1); print n, $1 }
118abcdef	bc	XYZ	1 aXYZdef
119abcdef	xy	XYZ	0 abcdef
120���������������������	���	NO	2 NO���������NO������
121���� finger	����.*g	FING	1 FINGer
122������������	.	x	6 xxxxxx
123
124try match { print match($1, $2), RSTART, RLENGTH }
125abc	[^a]	2 2 1
126abc	[^ab]	3 3 1
127������������������������������	[^���]	2 2 1
128������������������������������	[^���-���]	5 5 1
129abc	a	1 1 1
130abc	x	0 0 -1
131������������������������������	���	4 4 1
132������������������������������	���.*���	4 4 4
133������������������������������	���.*���	0 0 -1
134��������	��+	2 2 1
135��������	x+	0 0 -1
136��������	��.	2 2 2
137������������������������������	[���]	4 4 1
138������������������������������	[���-���]	0 0 -1
139������������������������������	[^���-���]	1 1 1
140�������� ���������� ��	[��-��]	2 2 1
141�������� ���������� ��	[��-��]+	2 2 3
142xxx�������� ���������� ��	[��-��]	4 4 1
143������ ���������� �������� ������������ ��	��.*��	3 3 15
144���� ���������� ������ ����	[^��]	2 2 1
145
146# FS="" should split into unicode chars
147try emptyFS BEGIN {FS=""} {print NF}
148������������������������������	10
149���������������������	7
150������������	6
151������������������	6
152������������������	6
153������������������	6
154����	1
155���� finger	8
156
157# printf(%N.Ns) for utf8 strings
158try printfs1 {printf("[%5.2s][%-5.2s]\n"), $1, $1}
159abcd	[   ab][ab   ]
160������abc	[   ������][������   ]
161�����abc	[   �����][�����   ]
162��abc	[   ��a][��a   ]
163������������	[   ����][����   ]
164������xyz	[   ����][����   ]
165����	[    ����][����    ]
166
167# printf(%N.Ns) for utf8 strings
168try printfs2 {printf("[%5s][%-5s]\n"), $1, $1}
169abcd	[ abcd][abcd ]
170������ab	[ ������ab][������ab ]
171a������ab	[a������ab][a������ab]
172a������abc	[a������abc][a������abc]
173�����ab	[ �����ab][�����ab ]
174��abc	[ ��abc][��abc ]
175������������	[������������][������������]
176����	[    ����][����    ]
177
178# printf(%N.Ns) for utf8 strings
179try printfs3 {printf("[%.2s][%-.2s]\n"), $1, $1}
180abcd	[ab][ab]
181������abc	[������][������]
182�����abc	[�����][�����]
183��	[��][��]
184����	[����][����]
185
186# printf(%c) for utf
187try printfc {printf("%c %c\n", $1, substr($1,2,1))}
188������������������������������	��� ���
189���������������������	��� ���
190������������	�� ��
191������������������	��� ���
192��������	���� ����
193
194!!!!
195