xref: /aosp_15_r20/external/bc/tests/history.py (revision 5a6e848804d15c18a0125914844ee4eb0bda4fcf)
1*5a6e8488SAndroid Build Coastguard Worker#! /usr/bin/python
2*5a6e8488SAndroid Build Coastguard Worker#
3*5a6e8488SAndroid Build Coastguard Worker# SPDX-License-Identifier: BSD-2-Clause
4*5a6e8488SAndroid Build Coastguard Worker#
5*5a6e8488SAndroid Build Coastguard Worker# Copyright (c) 2018-2024 Gavin D. Howard and contributors.
6*5a6e8488SAndroid Build Coastguard Worker#
7*5a6e8488SAndroid Build Coastguard Worker# Redistribution and use in source and binary forms, with or without
8*5a6e8488SAndroid Build Coastguard Worker# modification, are permitted provided that the following conditions are met:
9*5a6e8488SAndroid Build Coastguard Worker#
10*5a6e8488SAndroid Build Coastguard Worker# * Redistributions of source code must retain the above copyright notice, this
11*5a6e8488SAndroid Build Coastguard Worker#   list of conditions and the following disclaimer.
12*5a6e8488SAndroid Build Coastguard Worker#
13*5a6e8488SAndroid Build Coastguard Worker# * Redistributions in binary form must reproduce the above copyright notice,
14*5a6e8488SAndroid Build Coastguard Worker#   this list of conditions and the following disclaimer in the documentation
15*5a6e8488SAndroid Build Coastguard Worker#   and/or other materials provided with the distribution.
16*5a6e8488SAndroid Build Coastguard Worker#
17*5a6e8488SAndroid Build Coastguard Worker# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*5a6e8488SAndroid Build Coastguard Worker# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*5a6e8488SAndroid Build Coastguard Worker# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*5a6e8488SAndroid Build Coastguard Worker# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*5a6e8488SAndroid Build Coastguard Worker# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*5a6e8488SAndroid Build Coastguard Worker# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*5a6e8488SAndroid Build Coastguard Worker# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*5a6e8488SAndroid Build Coastguard Worker# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*5a6e8488SAndroid Build Coastguard Worker# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*5a6e8488SAndroid Build Coastguard Worker# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*5a6e8488SAndroid Build Coastguard Worker# POSSIBILITY OF SUCH DAMAGE.
28*5a6e8488SAndroid Build Coastguard Worker#
29*5a6e8488SAndroid Build Coastguard Worker
30*5a6e8488SAndroid Build Coastguard Workerimport os, sys
31*5a6e8488SAndroid Build Coastguard Workerimport time
32*5a6e8488SAndroid Build Coastguard Workerimport signal
33*5a6e8488SAndroid Build Coastguard Workerimport traceback
34*5a6e8488SAndroid Build Coastguard Worker
35*5a6e8488SAndroid Build Coastguard Workertry:
36*5a6e8488SAndroid Build Coastguard Worker	import pexpect
37*5a6e8488SAndroid Build Coastguard Workerexcept ImportError:
38*5a6e8488SAndroid Build Coastguard Worker	print("Could not find pexpect. Skipping...")
39*5a6e8488SAndroid Build Coastguard Worker	sys.exit(0)
40*5a6e8488SAndroid Build Coastguard Worker
41*5a6e8488SAndroid Build Coastguard Worker# Housekeeping.
42*5a6e8488SAndroid Build Coastguard Workerscript = sys.argv[0]
43*5a6e8488SAndroid Build Coastguard Workertestdir = os.path.dirname(script)
44*5a6e8488SAndroid Build Coastguard Worker
45*5a6e8488SAndroid Build Coastguard Workerif "BC_TEST_OUTPUT_DIR" in os.environ:
46*5a6e8488SAndroid Build Coastguard Worker	outputdir = os.environ["BC_TEST_OUTPUT_DIR"]
47*5a6e8488SAndroid Build Coastguard Workerelse:
48*5a6e8488SAndroid Build Coastguard Worker	outputdir = testdir
49*5a6e8488SAndroid Build Coastguard Worker
50*5a6e8488SAndroid Build Coastguard Workerprompt = ">>> "
51*5a6e8488SAndroid Build Coastguard Worker
52*5a6e8488SAndroid Build Coastguard Worker# This array is for escaping characters that are necessary to escape when
53*5a6e8488SAndroid Build Coastguard Worker# outputting to pexpect. Since pexpect takes regexes, these characters confuse
54*5a6e8488SAndroid Build Coastguard Worker# it unless we escape them.
55*5a6e8488SAndroid Build Coastguard Workerescapes = [
56*5a6e8488SAndroid Build Coastguard Worker	']',
57*5a6e8488SAndroid Build Coastguard Worker	'[',
58*5a6e8488SAndroid Build Coastguard Worker	'+',
59*5a6e8488SAndroid Build Coastguard Worker]
60*5a6e8488SAndroid Build Coastguard Worker
61*5a6e8488SAndroid Build Coastguard Worker# UTF-8 stress tests.
62*5a6e8488SAndroid Build Coastguard Workerutf8_stress1 = "ᆬḰ䋔䗅㜲ತ咡䒢岤䳰稨⣡嶣㷡嶏ⵐ䄺嵕ਅ奰痚㆜䊛拂䅙૩➋䛿ቬ竳Ϳᅠ❄产翷䮊௷Ỉ䷒䳜㛠➕傎ᗋᏯਕ䆐悙癐㺨"
63*5a6e8488SAndroid Build Coastguard Workerutf8_stress2 = "韠싧돳넨큚ꉿ뮴픷ꉲ긌�최릙걆鳬낽ꪁ퍼鈴핐黙헶ꪈ뮩쭀锻끥鉗겉욞며뛯꬐�ﻼ�度錐�"
64*5a6e8488SAndroid Build Coastguard Workerutf8_stress3 = "곻�䣹昲蜴Ὓ桢㎏⚦珢畣갴ﭱ鶶ๅ⶛뀁彻ꖒ䔾ꢚﱤ햔햞㐹�鼳뵡▿ⶾ꠩�纞⊐佧�ⵟ霘紳㱔籠뎼⊓搧硤"
65*5a6e8488SAndroid Build Coastguard Workerutf8_stress4 = "ᄀ������"
66*5a6e8488SAndroid Build Coastguard Worker
67*5a6e8488SAndroid Build Coastguard Worker# An easy array for UTF-8 tests.
68*5a6e8488SAndroid Build Coastguard Workerutf8_stress_strs = [
69*5a6e8488SAndroid Build Coastguard Worker	utf8_stress1,
70*5a6e8488SAndroid Build Coastguard Worker	utf8_stress2,
71*5a6e8488SAndroid Build Coastguard Worker	utf8_stress3,
72*5a6e8488SAndroid Build Coastguard Worker	utf8_stress4,
73*5a6e8488SAndroid Build Coastguard Worker]
74*5a6e8488SAndroid Build Coastguard Worker
75*5a6e8488SAndroid Build Coastguard Worker
76*5a6e8488SAndroid Build Coastguard Workerdef expect(child, data):
77*5a6e8488SAndroid Build Coastguard Worker	child.expect(data)
78*5a6e8488SAndroid Build Coastguard Worker
79*5a6e8488SAndroid Build Coastguard Worker
80*5a6e8488SAndroid Build Coastguard Worker# Eats all of the child's data.
81*5a6e8488SAndroid Build Coastguard Worker# @param child  The child whose data should be eaten.
82*5a6e8488SAndroid Build Coastguard Workerdef eat(child):
83*5a6e8488SAndroid Build Coastguard Worker	while child.buffer is not None and len(child.buffer) > 0:
84*5a6e8488SAndroid Build Coastguard Worker		expect(child, ".+")
85*5a6e8488SAndroid Build Coastguard Worker
86*5a6e8488SAndroid Build Coastguard Worker
87*5a6e8488SAndroid Build Coastguard Worker# Send data to a child. This makes sure the buffers are empty first.
88*5a6e8488SAndroid Build Coastguard Worker# @param child  The child to send data to.
89*5a6e8488SAndroid Build Coastguard Worker# @param data   The data to send.
90*5a6e8488SAndroid Build Coastguard Workerdef send(child, data):
91*5a6e8488SAndroid Build Coastguard Worker	eat(child)
92*5a6e8488SAndroid Build Coastguard Worker	child.send(data)
93*5a6e8488SAndroid Build Coastguard Worker
94*5a6e8488SAndroid Build Coastguard Worker
95*5a6e8488SAndroid Build Coastguard Workerdef wait(child):
96*5a6e8488SAndroid Build Coastguard Worker	if child.isalive():
97*5a6e8488SAndroid Build Coastguard Worker		child.sendeof()
98*5a6e8488SAndroid Build Coastguard Worker		time.sleep(1)
99*5a6e8488SAndroid Build Coastguard Worker		if child.isalive():
100*5a6e8488SAndroid Build Coastguard Worker			child.kill(signal.SIGTERM)
101*5a6e8488SAndroid Build Coastguard Worker			time.sleep(1)
102*5a6e8488SAndroid Build Coastguard Worker			if child.isalive():
103*5a6e8488SAndroid Build Coastguard Worker				child.kill(signal.SIGKILL)
104*5a6e8488SAndroid Build Coastguard Worker	child.wait()
105*5a6e8488SAndroid Build Coastguard Worker
106*5a6e8488SAndroid Build Coastguard Worker
107*5a6e8488SAndroid Build Coastguard Worker# Check that the child output the expected line. If history is false, then
108*5a6e8488SAndroid Build Coastguard Worker# the output should change.
109*5a6e8488SAndroid Build Coastguard Workerdef check_line(child, expected, prompt=">>> ", history=True):
110*5a6e8488SAndroid Build Coastguard Worker	send(child, "\n")
111*5a6e8488SAndroid Build Coastguard Worker	prefix = "\r\n" if history else ""
112*5a6e8488SAndroid Build Coastguard Worker	expect(child, prefix + expected + "\r\n" + prompt)
113*5a6e8488SAndroid Build Coastguard Worker
114*5a6e8488SAndroid Build Coastguard Worker
115*5a6e8488SAndroid Build Coastguard Worker# Write a string to output, checking all of the characters are output,
116*5a6e8488SAndroid Build Coastguard Worker# one-by-one.
117*5a6e8488SAndroid Build Coastguard Workerdef write_str(child, s):
118*5a6e8488SAndroid Build Coastguard Worker	for c in s:
119*5a6e8488SAndroid Build Coastguard Worker		send(child, c)
120*5a6e8488SAndroid Build Coastguard Worker		if c in escapes:
121*5a6e8488SAndroid Build Coastguard Worker			expect(child, "\\{}".format(c))
122*5a6e8488SAndroid Build Coastguard Worker		else:
123*5a6e8488SAndroid Build Coastguard Worker			expect(child, c)
124*5a6e8488SAndroid Build Coastguard Worker
125*5a6e8488SAndroid Build Coastguard Worker
126*5a6e8488SAndroid Build Coastguard Worker# Check the bc banner.
127*5a6e8488SAndroid Build Coastguard Worker# @param child  The child process.
128*5a6e8488SAndroid Build Coastguard Workerdef bc_banner(child):
129*5a6e8488SAndroid Build Coastguard Worker	bc_banner1 = "bc [0-9]+\\.[0-9]+\\.[0-9]+\r\n"
130*5a6e8488SAndroid Build Coastguard Worker	bc_banner2 = "Copyright \\(c\\) 2018-[2-9][0-9][0-9][0-9] Gavin D. Howard and contributors\r\n"
131*5a6e8488SAndroid Build Coastguard Worker	bc_banner3 = "Report bugs at: https://git.gavinhoward.com/gavin/bc\r\n\r\n"
132*5a6e8488SAndroid Build Coastguard Worker	bc_banner4 = "This is free software with ABSOLUTELY NO WARRANTY.\r\n\r\n"
133*5a6e8488SAndroid Build Coastguard Worker	expect(child, bc_banner1)
134*5a6e8488SAndroid Build Coastguard Worker	expect(child, bc_banner2)
135*5a6e8488SAndroid Build Coastguard Worker	expect(child, bc_banner3)
136*5a6e8488SAndroid Build Coastguard Worker	expect(child, bc_banner4)
137*5a6e8488SAndroid Build Coastguard Worker	expect(child, prompt)
138*5a6e8488SAndroid Build Coastguard Worker
139*5a6e8488SAndroid Build Coastguard Worker
140*5a6e8488SAndroid Build Coastguard Worker# Common UTF-8 testing function. The index is the index into utf8_stress_strs
141*5a6e8488SAndroid Build Coastguard Worker# for which stress string to use.
142*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
143*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
144*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
145*5a6e8488SAndroid Build Coastguard Worker# @param idx   The index of the UTF-8 stress string.
146*5a6e8488SAndroid Build Coastguard Workerdef test_utf8(exe, args, env, idx, bc=True):
147*5a6e8488SAndroid Build Coastguard Worker
148*5a6e8488SAndroid Build Coastguard Worker	# Because both bc and dc use this, make sure the banner doesn't pop.
149*5a6e8488SAndroid Build Coastguard Worker	env["BC_BANNER"] = "0"
150*5a6e8488SAndroid Build Coastguard Worker
151*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env, encoding='utf-8', codec_errors='ignore')
152*5a6e8488SAndroid Build Coastguard Worker
153*5a6e8488SAndroid Build Coastguard Worker	try:
154*5a6e8488SAndroid Build Coastguard Worker
155*5a6e8488SAndroid Build Coastguard Worker		# Write the stress string.
156*5a6e8488SAndroid Build Coastguard Worker		send(child, utf8_stress_strs[idx])
157*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
158*5a6e8488SAndroid Build Coastguard Worker
159*5a6e8488SAndroid Build Coastguard Worker		if bc:
160*5a6e8488SAndroid Build Coastguard Worker			send(child, "quit")
161*5a6e8488SAndroid Build Coastguard Worker		else:
162*5a6e8488SAndroid Build Coastguard Worker			send(child, "q")
163*5a6e8488SAndroid Build Coastguard Worker
164*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
165*5a6e8488SAndroid Build Coastguard Worker
166*5a6e8488SAndroid Build Coastguard Worker		wait(child)
167*5a6e8488SAndroid Build Coastguard Worker
168*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
169*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
170*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
171*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
172*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
173*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
174*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
175*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
176*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
177*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
178*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
179*5a6e8488SAndroid Build Coastguard Worker
180*5a6e8488SAndroid Build Coastguard Worker	return child
181*5a6e8488SAndroid Build Coastguard Worker
182*5a6e8488SAndroid Build Coastguard Worker# A random UTF-8 test with insert.
183*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
184*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
185*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
186*5a6e8488SAndroid Build Coastguard Workerdef test_utf8_0(exe, args, env, bc=True):
187*5a6e8488SAndroid Build Coastguard Worker
188*5a6e8488SAndroid Build Coastguard Worker	# Because both bc and dc use this, make sure the banner doesn't pop.
189*5a6e8488SAndroid Build Coastguard Worker	env["BC_BANNER"] = "0"
190*5a6e8488SAndroid Build Coastguard Worker
191*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env, encoding='utf-8', codec_errors='ignore')
192*5a6e8488SAndroid Build Coastguard Worker
193*5a6e8488SAndroid Build Coastguard Worker	try:
194*5a6e8488SAndroid Build Coastguard Worker
195*5a6e8488SAndroid Build Coastguard Worker		# Just random UTF-8 I generated somewhow, plus ensuring that insert works.
196*5a6e8488SAndroid Build Coastguard Worker		write_str(child, "ﴪáá̵̗��ã")
197*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[D\x1b[D\x1b[D\x1b\x1b[Aℐ")
198*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
199*5a6e8488SAndroid Build Coastguard Worker
200*5a6e8488SAndroid Build Coastguard Worker		if bc:
201*5a6e8488SAndroid Build Coastguard Worker			send(child, "quit")
202*5a6e8488SAndroid Build Coastguard Worker		else:
203*5a6e8488SAndroid Build Coastguard Worker			send(child, "q")
204*5a6e8488SAndroid Build Coastguard Worker
205*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
206*5a6e8488SAndroid Build Coastguard Worker		eat(child)
207*5a6e8488SAndroid Build Coastguard Worker
208*5a6e8488SAndroid Build Coastguard Worker		wait(child)
209*5a6e8488SAndroid Build Coastguard Worker
210*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
211*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
212*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
213*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
214*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
215*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
216*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
217*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
218*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
219*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
220*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
221*5a6e8488SAndroid Build Coastguard Worker
222*5a6e8488SAndroid Build Coastguard Worker	return child
223*5a6e8488SAndroid Build Coastguard Worker
224*5a6e8488SAndroid Build Coastguard Worker
225*5a6e8488SAndroid Build Coastguard Workerdef test_utf8_1(exe, args, env, bc=True):
226*5a6e8488SAndroid Build Coastguard Worker	return test_utf8(exe, args, env, 0, bc)
227*5a6e8488SAndroid Build Coastguard Worker
228*5a6e8488SAndroid Build Coastguard Worker
229*5a6e8488SAndroid Build Coastguard Workerdef test_utf8_2(exe, args, env, bc=True):
230*5a6e8488SAndroid Build Coastguard Worker	return test_utf8(exe, args, env, 1, bc)
231*5a6e8488SAndroid Build Coastguard Worker
232*5a6e8488SAndroid Build Coastguard Worker
233*5a6e8488SAndroid Build Coastguard Workerdef test_utf8_3(exe, args, env, bc=True):
234*5a6e8488SAndroid Build Coastguard Worker	return test_utf8(exe, args, env, 2, bc)
235*5a6e8488SAndroid Build Coastguard Worker
236*5a6e8488SAndroid Build Coastguard Worker
237*5a6e8488SAndroid Build Coastguard Workerdef test_utf8_4(exe, args, env, bc=True):
238*5a6e8488SAndroid Build Coastguard Worker	return test_utf8(exe, args, env, 3, bc)
239*5a6e8488SAndroid Build Coastguard Worker
240*5a6e8488SAndroid Build Coastguard Worker
241*5a6e8488SAndroid Build Coastguard Worker# This tests a SIGINT with reset followed by a SIGQUIT.
242*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
243*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
244*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
245*5a6e8488SAndroid Build Coastguard Workerdef test_sigint_sigquit(exe, args, env):
246*5a6e8488SAndroid Build Coastguard Worker
247*5a6e8488SAndroid Build Coastguard Worker	# Because both bc and dc use this, make sure the banner doesn't pop.
248*5a6e8488SAndroid Build Coastguard Worker	env["BC_BANNER"] = "0"
249*5a6e8488SAndroid Build Coastguard Worker
250*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
251*5a6e8488SAndroid Build Coastguard Worker
252*5a6e8488SAndroid Build Coastguard Worker	try:
253*5a6e8488SAndroid Build Coastguard Worker		send(child, "\t")
254*5a6e8488SAndroid Build Coastguard Worker		expect(child, "\t")
255*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x03")
256*5a6e8488SAndroid Build Coastguard Worker		# send(child, "\x1c")
257*5a6e8488SAndroid Build Coastguard Worker		wait(child)
258*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
259*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
260*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
261*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
262*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
263*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
264*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
265*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
266*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
267*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
268*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
269*5a6e8488SAndroid Build Coastguard Worker
270*5a6e8488SAndroid Build Coastguard Worker	return child
271*5a6e8488SAndroid Build Coastguard Worker
272*5a6e8488SAndroid Build Coastguard Worker
273*5a6e8488SAndroid Build Coastguard Worker# Test for EOF.
274*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
275*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
276*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
277*5a6e8488SAndroid Build Coastguard Workerdef test_eof(exe, args, env):
278*5a6e8488SAndroid Build Coastguard Worker
279*5a6e8488SAndroid Build Coastguard Worker	# Because both bc and dc use this, make sure the banner doesn't pop.
280*5a6e8488SAndroid Build Coastguard Worker	env["BC_BANNER"] = "0"
281*5a6e8488SAndroid Build Coastguard Worker
282*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
283*5a6e8488SAndroid Build Coastguard Worker
284*5a6e8488SAndroid Build Coastguard Worker	try:
285*5a6e8488SAndroid Build Coastguard Worker		send(child, "123")
286*5a6e8488SAndroid Build Coastguard Worker		expect(child, "123")
287*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x01")
288*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x04")
289*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x04")
290*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x04")
291*5a6e8488SAndroid Build Coastguard Worker		wait(child)
292*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
293*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
294*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
295*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
296*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
297*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
298*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
299*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
300*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
301*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
302*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
303*5a6e8488SAndroid Build Coastguard Worker
304*5a6e8488SAndroid Build Coastguard Worker	return child
305*5a6e8488SAndroid Build Coastguard Worker
306*5a6e8488SAndroid Build Coastguard Worker
307*5a6e8488SAndroid Build Coastguard Worker# Test for quiting SIGINT.
308*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
309*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
310*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
311*5a6e8488SAndroid Build Coastguard Workerdef test_sigint(exe, args, env):
312*5a6e8488SAndroid Build Coastguard Worker
313*5a6e8488SAndroid Build Coastguard Worker	# Because both bc and dc use this, make sure the banner doesn't pop.
314*5a6e8488SAndroid Build Coastguard Worker	env["BC_BANNER"] = "0"
315*5a6e8488SAndroid Build Coastguard Worker
316*5a6e8488SAndroid Build Coastguard Worker	env["BC_SIGINT_RESET"] = "0"
317*5a6e8488SAndroid Build Coastguard Worker	env["DC_SIGINT_RESET"] = "0"
318*5a6e8488SAndroid Build Coastguard Worker
319*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
320*5a6e8488SAndroid Build Coastguard Worker
321*5a6e8488SAndroid Build Coastguard Worker	try:
322*5a6e8488SAndroid Build Coastguard Worker		send(child, "\t")
323*5a6e8488SAndroid Build Coastguard Worker		expect(child, "\t")
324*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x03")
325*5a6e8488SAndroid Build Coastguard Worker		wait(child)
326*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
327*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
328*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
329*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
330*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
331*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
332*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
333*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
334*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
335*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
336*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
337*5a6e8488SAndroid Build Coastguard Worker
338*5a6e8488SAndroid Build Coastguard Worker	return child
339*5a6e8488SAndroid Build Coastguard Worker
340*5a6e8488SAndroid Build Coastguard Worker
341*5a6e8488SAndroid Build Coastguard Worker# Test for SIGTSTP.
342*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
343*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
344*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
345*5a6e8488SAndroid Build Coastguard Workerdef test_sigtstp(exe, args, env):
346*5a6e8488SAndroid Build Coastguard Worker
347*5a6e8488SAndroid Build Coastguard Worker	# This test does not work on FreeBSD, so skip.
348*5a6e8488SAndroid Build Coastguard Worker	if sys.platform.startswith("freebsd"):
349*5a6e8488SAndroid Build Coastguard Worker		sys.exit(0)
350*5a6e8488SAndroid Build Coastguard Worker
351*5a6e8488SAndroid Build Coastguard Worker	# Because both bc and dc use this, make sure the banner doesn't pop.
352*5a6e8488SAndroid Build Coastguard Worker	env["BC_BANNER"] = "0"
353*5a6e8488SAndroid Build Coastguard Worker
354*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
355*5a6e8488SAndroid Build Coastguard Worker
356*5a6e8488SAndroid Build Coastguard Worker	try:
357*5a6e8488SAndroid Build Coastguard Worker		send(child, "\t")
358*5a6e8488SAndroid Build Coastguard Worker		expect(child, "\t")
359*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x13")
360*5a6e8488SAndroid Build Coastguard Worker		time.sleep(1)
361*5a6e8488SAndroid Build Coastguard Worker		if not child.isalive():
362*5a6e8488SAndroid Build Coastguard Worker			print("child exited early")
363*5a6e8488SAndroid Build Coastguard Worker			print(str(child))
364*5a6e8488SAndroid Build Coastguard Worker			print(str(child.buffer))
365*5a6e8488SAndroid Build Coastguard Worker			sys.exit(1)
366*5a6e8488SAndroid Build Coastguard Worker		child.kill(signal.SIGCONT)
367*5a6e8488SAndroid Build Coastguard Worker		send(child, "quit")
368*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
369*5a6e8488SAndroid Build Coastguard Worker		wait(child)
370*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
371*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
372*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
373*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
374*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
375*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
376*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
377*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
378*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
379*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
380*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
381*5a6e8488SAndroid Build Coastguard Worker
382*5a6e8488SAndroid Build Coastguard Worker	return child
383*5a6e8488SAndroid Build Coastguard Worker
384*5a6e8488SAndroid Build Coastguard Worker
385*5a6e8488SAndroid Build Coastguard Worker# Test for SIGSTOP.
386*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
387*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
388*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
389*5a6e8488SAndroid Build Coastguard Workerdef test_sigstop(exe, args, env):
390*5a6e8488SAndroid Build Coastguard Worker
391*5a6e8488SAndroid Build Coastguard Worker	# Because both bc and dc use this, make sure the banner doesn't pop.
392*5a6e8488SAndroid Build Coastguard Worker	env["BC_BANNER"] = "0"
393*5a6e8488SAndroid Build Coastguard Worker
394*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
395*5a6e8488SAndroid Build Coastguard Worker
396*5a6e8488SAndroid Build Coastguard Worker	try:
397*5a6e8488SAndroid Build Coastguard Worker		send(child, "\t")
398*5a6e8488SAndroid Build Coastguard Worker		expect(child, "\t")
399*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x14")
400*5a6e8488SAndroid Build Coastguard Worker		time.sleep(1)
401*5a6e8488SAndroid Build Coastguard Worker		if not child.isalive():
402*5a6e8488SAndroid Build Coastguard Worker			print("child exited early")
403*5a6e8488SAndroid Build Coastguard Worker			print(str(child))
404*5a6e8488SAndroid Build Coastguard Worker			print(str(child.buffer))
405*5a6e8488SAndroid Build Coastguard Worker			sys.exit(1)
406*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x13")
407*5a6e8488SAndroid Build Coastguard Worker		time.sleep(1)
408*5a6e8488SAndroid Build Coastguard Worker		if not child.isalive():
409*5a6e8488SAndroid Build Coastguard Worker			print("child exited early")
410*5a6e8488SAndroid Build Coastguard Worker			print(str(child))
411*5a6e8488SAndroid Build Coastguard Worker			print(str(child.buffer))
412*5a6e8488SAndroid Build Coastguard Worker			sys.exit(1)
413*5a6e8488SAndroid Build Coastguard Worker		child.kill(signal.SIGCONT)
414*5a6e8488SAndroid Build Coastguard Worker		send(child, "quit")
415*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
416*5a6e8488SAndroid Build Coastguard Worker		wait(child)
417*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
418*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
419*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
420*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
421*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
422*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
423*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
424*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
425*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
426*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
427*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
428*5a6e8488SAndroid Build Coastguard Worker
429*5a6e8488SAndroid Build Coastguard Worker	return child
430*5a6e8488SAndroid Build Coastguard Worker
431*5a6e8488SAndroid Build Coastguard Worker
432*5a6e8488SAndroid Build Coastguard Workerdef test_bc_utf8_0(exe, args, env):
433*5a6e8488SAndroid Build Coastguard Worker	return test_utf8_0(exe, args, env, True)
434*5a6e8488SAndroid Build Coastguard Worker
435*5a6e8488SAndroid Build Coastguard Worker
436*5a6e8488SAndroid Build Coastguard Workerdef test_bc_utf8_1(exe, args, env):
437*5a6e8488SAndroid Build Coastguard Worker	return test_utf8_1(exe, args, env, True)
438*5a6e8488SAndroid Build Coastguard Worker
439*5a6e8488SAndroid Build Coastguard Worker
440*5a6e8488SAndroid Build Coastguard Workerdef test_bc_utf8_2(exe, args, env):
441*5a6e8488SAndroid Build Coastguard Worker	return test_utf8_2(exe, args, env, True)
442*5a6e8488SAndroid Build Coastguard Worker
443*5a6e8488SAndroid Build Coastguard Worker
444*5a6e8488SAndroid Build Coastguard Workerdef test_bc_utf8_3(exe, args, env):
445*5a6e8488SAndroid Build Coastguard Worker	return test_utf8_3(exe, args, env, True)
446*5a6e8488SAndroid Build Coastguard Worker
447*5a6e8488SAndroid Build Coastguard Worker
448*5a6e8488SAndroid Build Coastguard Workerdef test_bc_utf8_4(exe, args, env):
449*5a6e8488SAndroid Build Coastguard Worker	return test_utf8_4(exe, args, env, True)
450*5a6e8488SAndroid Build Coastguard Worker
451*5a6e8488SAndroid Build Coastguard Worker
452*5a6e8488SAndroid Build Coastguard Worker# Basic bc test.
453*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
454*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
455*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
456*5a6e8488SAndroid Build Coastguard Workerdef test_bc1(exe, args, env):
457*5a6e8488SAndroid Build Coastguard Worker
458*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
459*5a6e8488SAndroid Build Coastguard Worker
460*5a6e8488SAndroid Build Coastguard Worker	try:
461*5a6e8488SAndroid Build Coastguard Worker		bc_banner(child)
462*5a6e8488SAndroid Build Coastguard Worker		write_str(child, "1")
463*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "1")
464*5a6e8488SAndroid Build Coastguard Worker		write_str(child, "1")
465*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "1")
466*5a6e8488SAndroid Build Coastguard Worker		send(child, "quit")
467*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
468*5a6e8488SAndroid Build Coastguard Worker		wait(child)
469*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
470*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
471*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
472*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
473*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
474*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
475*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
476*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
477*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
478*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
479*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
480*5a6e8488SAndroid Build Coastguard Worker
481*5a6e8488SAndroid Build Coastguard Worker	return child
482*5a6e8488SAndroid Build Coastguard Worker
483*5a6e8488SAndroid Build Coastguard Worker
484*5a6e8488SAndroid Build Coastguard Worker# SIGINT with no history.
485*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
486*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
487*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
488*5a6e8488SAndroid Build Coastguard Workerdef test_bc2(exe, args, env):
489*5a6e8488SAndroid Build Coastguard Worker
490*5a6e8488SAndroid Build Coastguard Worker	env["TERM"] = "dumb"
491*5a6e8488SAndroid Build Coastguard Worker
492*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
493*5a6e8488SAndroid Build Coastguard Worker
494*5a6e8488SAndroid Build Coastguard Worker	try:
495*5a6e8488SAndroid Build Coastguard Worker		bc_banner(child)
496*5a6e8488SAndroid Build Coastguard Worker		child.sendline("1")
497*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "1", history=False)
498*5a6e8488SAndroid Build Coastguard Worker		time.sleep(1)
499*5a6e8488SAndroid Build Coastguard Worker		child.sendintr()
500*5a6e8488SAndroid Build Coastguard Worker		child.sendline("quit")
501*5a6e8488SAndroid Build Coastguard Worker		wait(child)
502*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
503*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
504*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
505*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
506*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
507*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
508*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
509*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
510*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
511*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
512*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
513*5a6e8488SAndroid Build Coastguard Worker
514*5a6e8488SAndroid Build Coastguard Worker	return child
515*5a6e8488SAndroid Build Coastguard Worker
516*5a6e8488SAndroid Build Coastguard Worker
517*5a6e8488SAndroid Build Coastguard Worker# Left and right arrows.
518*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
519*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
520*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
521*5a6e8488SAndroid Build Coastguard Workerdef test_bc3(exe, args, env):
522*5a6e8488SAndroid Build Coastguard Worker
523*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
524*5a6e8488SAndroid Build Coastguard Worker
525*5a6e8488SAndroid Build Coastguard Worker	try:
526*5a6e8488SAndroid Build Coastguard Worker		bc_banner(child)
527*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[D\x1b[D\x1b[C\x1b[C")
528*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
529*5a6e8488SAndroid Build Coastguard Worker		expect(child, prompt)
530*5a6e8488SAndroid Build Coastguard Worker		send(child, "12\x1b[D3\x1b[C4\x1bOD5\x1bOC6")
531*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
532*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "132546")
533*5a6e8488SAndroid Build Coastguard Worker		send(child, "12\x023\x064")
534*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
535*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "1324")
536*5a6e8488SAndroid Build Coastguard Worker		send(child, "12\x1b[H3\x1bOH\x01\x1b[H45\x1bOF6\x05\x1b[F7\x1bOH8")
537*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
538*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "84531267")
539*5a6e8488SAndroid Build Coastguard Worker		send(child, "quit")
540*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
541*5a6e8488SAndroid Build Coastguard Worker		wait(child)
542*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
543*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
544*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
545*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
546*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
547*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
548*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
549*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
550*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
551*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
552*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
553*5a6e8488SAndroid Build Coastguard Worker
554*5a6e8488SAndroid Build Coastguard Worker	return child
555*5a6e8488SAndroid Build Coastguard Worker
556*5a6e8488SAndroid Build Coastguard Worker
557*5a6e8488SAndroid Build Coastguard Worker# Up and down arrows.
558*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
559*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
560*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
561*5a6e8488SAndroid Build Coastguard Workerdef test_bc4(exe, args, env):
562*5a6e8488SAndroid Build Coastguard Worker
563*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
564*5a6e8488SAndroid Build Coastguard Worker
565*5a6e8488SAndroid Build Coastguard Worker	try:
566*5a6e8488SAndroid Build Coastguard Worker		bc_banner(child)
567*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[A\x1bOA\x1b[B\x1bOB")
568*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
569*5a6e8488SAndroid Build Coastguard Worker		expect(child, prompt)
570*5a6e8488SAndroid Build Coastguard Worker		write_str(child, "15")
571*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "15")
572*5a6e8488SAndroid Build Coastguard Worker		write_str(child, "2^16")
573*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "65536")
574*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[A\x1bOA")
575*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
576*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "15")
577*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[A\x1bOA\x1b[A\x1b[B")
578*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "65536")
579*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[A\x1bOA\x0e\x1b[A\x1b[A\x1b[A\x1b[B\x10\x1b[B\x1b[B\x1bOB\x1b[B\x1bOA")
580*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
581*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "65536")
582*5a6e8488SAndroid Build Coastguard Worker		send(child, "quit")
583*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
584*5a6e8488SAndroid Build Coastguard Worker		wait(child)
585*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
586*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
587*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
588*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
589*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
590*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
591*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
592*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
593*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
594*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
595*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
596*5a6e8488SAndroid Build Coastguard Worker
597*5a6e8488SAndroid Build Coastguard Worker	return child
598*5a6e8488SAndroid Build Coastguard Worker
599*5a6e8488SAndroid Build Coastguard Worker
600*5a6e8488SAndroid Build Coastguard Worker# Clear screen.
601*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
602*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
603*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
604*5a6e8488SAndroid Build Coastguard Workerdef test_bc5(exe, args, env):
605*5a6e8488SAndroid Build Coastguard Worker
606*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
607*5a6e8488SAndroid Build Coastguard Worker
608*5a6e8488SAndroid Build Coastguard Worker	try:
609*5a6e8488SAndroid Build Coastguard Worker		bc_banner(child)
610*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x0c")
611*5a6e8488SAndroid Build Coastguard Worker		send(child, "quit")
612*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
613*5a6e8488SAndroid Build Coastguard Worker		wait(child)
614*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
615*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
616*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
617*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
618*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
619*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
620*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
621*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
622*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
623*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
624*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
625*5a6e8488SAndroid Build Coastguard Worker
626*5a6e8488SAndroid Build Coastguard Worker	return child
627*5a6e8488SAndroid Build Coastguard Worker
628*5a6e8488SAndroid Build Coastguard Worker
629*5a6e8488SAndroid Build Coastguard Worker# Printed material without a newline.
630*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
631*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
632*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
633*5a6e8488SAndroid Build Coastguard Workerdef test_bc6(exe, args, env):
634*5a6e8488SAndroid Build Coastguard Worker
635*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
636*5a6e8488SAndroid Build Coastguard Worker
637*5a6e8488SAndroid Build Coastguard Worker	try:
638*5a6e8488SAndroid Build Coastguard Worker		bc_banner(child)
639*5a6e8488SAndroid Build Coastguard Worker		send(child, "print \"Enter number: \"")
640*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
641*5a6e8488SAndroid Build Coastguard Worker		expect(child, "Enter number: ")
642*5a6e8488SAndroid Build Coastguard Worker		send(child, "4\x1b[A\x1b[A")
643*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
644*5a6e8488SAndroid Build Coastguard Worker		send(child, "quit")
645*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
646*5a6e8488SAndroid Build Coastguard Worker		wait(child)
647*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
648*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
649*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
650*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
651*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
652*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
653*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
654*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
655*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
656*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
657*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
658*5a6e8488SAndroid Build Coastguard Worker
659*5a6e8488SAndroid Build Coastguard Worker	return child
660*5a6e8488SAndroid Build Coastguard Worker
661*5a6e8488SAndroid Build Coastguard Worker
662*5a6e8488SAndroid Build Coastguard Worker# Word start and word end.
663*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
664*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
665*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
666*5a6e8488SAndroid Build Coastguard Workerdef test_bc7(exe, args, env):
667*5a6e8488SAndroid Build Coastguard Worker
668*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
669*5a6e8488SAndroid Build Coastguard Worker
670*5a6e8488SAndroid Build Coastguard Worker	try:
671*5a6e8488SAndroid Build Coastguard Worker		bc_banner(child)
672*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1bb\x1bb\x1bf\x1bf")
673*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
674*5a6e8488SAndroid Build Coastguard Worker		expect(child, prompt)
675*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[0~\x1b[3a")
676*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
677*5a6e8488SAndroid Build Coastguard Worker		expect(child, prompt)
678*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[0;4\x1b[0A")
679*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
680*5a6e8488SAndroid Build Coastguard Worker		expect(child, prompt)
681*5a6e8488SAndroid Build Coastguard Worker		send(child, "\t")
682*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb")
683*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf")
684*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
685*5a6e8488SAndroid Build Coastguard Worker		expect(child, prompt)
686*5a6e8488SAndroid Build Coastguard Worker		write_str(child, "12 + 34 + 56 + 78 + 90")
687*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "270")
688*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[A")
689*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb")
690*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf")
691*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "270")
692*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[A")
693*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1bh\x1bh\x1bf + 14 ")
694*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
695*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "284")
696*5a6e8488SAndroid Build Coastguard Worker		send(child, "quit")
697*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
698*5a6e8488SAndroid Build Coastguard Worker		wait(child)
699*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
700*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
701*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
702*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
703*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
704*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
705*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
706*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
707*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
708*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
709*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
710*5a6e8488SAndroid Build Coastguard Worker
711*5a6e8488SAndroid Build Coastguard Worker	return child
712*5a6e8488SAndroid Build Coastguard Worker
713*5a6e8488SAndroid Build Coastguard Worker
714*5a6e8488SAndroid Build Coastguard Worker# Backspace.
715*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
716*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
717*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
718*5a6e8488SAndroid Build Coastguard Workerdef test_bc8(exe, args, env):
719*5a6e8488SAndroid Build Coastguard Worker
720*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
721*5a6e8488SAndroid Build Coastguard Worker
722*5a6e8488SAndroid Build Coastguard Worker	try:
723*5a6e8488SAndroid Build Coastguard Worker		bc_banner(child)
724*5a6e8488SAndroid Build Coastguard Worker		send(child, "12\x1b[D3\x1b[C4\x08\x7f")
725*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
726*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "13")
727*5a6e8488SAndroid Build Coastguard Worker		send(child, "quit")
728*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
729*5a6e8488SAndroid Build Coastguard Worker		wait(child)
730*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
731*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
732*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
733*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
734*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
735*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
736*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
737*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
738*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
739*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
740*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
741*5a6e8488SAndroid Build Coastguard Worker
742*5a6e8488SAndroid Build Coastguard Worker	return child
743*5a6e8488SAndroid Build Coastguard Worker
744*5a6e8488SAndroid Build Coastguard Worker
745*5a6e8488SAndroid Build Coastguard Worker# Backspace and delete words.
746*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
747*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
748*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
749*5a6e8488SAndroid Build Coastguard Workerdef test_bc9(exe, args, env):
750*5a6e8488SAndroid Build Coastguard Worker
751*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
752*5a6e8488SAndroid Build Coastguard Worker
753*5a6e8488SAndroid Build Coastguard Worker	try:
754*5a6e8488SAndroid Build Coastguard Worker		bc_banner(child)
755*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[0;5D\x1b[0;5D\x1b[0;5D\x1b[0;5C\x1b[0;5D\x1bd\x1b[3~\x1b[d\x1b[d\x1b[d\x1b[d\x7f\x7f\x7f")
756*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
757*5a6e8488SAndroid Build Coastguard Worker		expect(child, prompt)
758*5a6e8488SAndroid Build Coastguard Worker		write_str(child, "12 + 34 + 56 + 78 + 90")
759*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "270")
760*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[A")
761*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[0;5D\x1b[0;5D\x1b[0;5D\x1b[0;5C\x1b[0;5D\x1bd\x1b[3~\x1b[d\x1b[d\x1b[d\x1b[d\x7f\x7f\x7f")
762*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
763*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "102")
764*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[A")
765*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x17\x17")
766*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
767*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "46")
768*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x17\x17")
769*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
770*5a6e8488SAndroid Build Coastguard Worker		expect(child, prompt)
771*5a6e8488SAndroid Build Coastguard Worker		send(child, "quit")
772*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
773*5a6e8488SAndroid Build Coastguard Worker		wait(child)
774*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
775*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
776*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
777*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
778*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
779*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
780*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
781*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
782*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
783*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
784*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
785*5a6e8488SAndroid Build Coastguard Worker
786*5a6e8488SAndroid Build Coastguard Worker	return child
787*5a6e8488SAndroid Build Coastguard Worker
788*5a6e8488SAndroid Build Coastguard Worker
789*5a6e8488SAndroid Build Coastguard Worker# Backspace and delete words 2.
790*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
791*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
792*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
793*5a6e8488SAndroid Build Coastguard Workerdef test_bc10(exe, args, env):
794*5a6e8488SAndroid Build Coastguard Worker
795*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
796*5a6e8488SAndroid Build Coastguard Worker
797*5a6e8488SAndroid Build Coastguard Worker	try:
798*5a6e8488SAndroid Build Coastguard Worker		bc_banner(child)
799*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[3~\x1b[3~")
800*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
801*5a6e8488SAndroid Build Coastguard Worker		expect(child, prompt)
802*5a6e8488SAndroid Build Coastguard Worker		send(child, "    \x1b[3~\x1b[3~")
803*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
804*5a6e8488SAndroid Build Coastguard Worker		expect(child, prompt)
805*5a6e8488SAndroid Build Coastguard Worker		write_str(child, "12 + 34 + 56 + 78 + 90")
806*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "270")
807*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[A\x1b[A\x1b[A\x1b[B\x1b[B\x1b[B\x1b[A")
808*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
809*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "270")
810*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[A\x1b[0;5D\x1b[0;5D\x0b")
811*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
812*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "180")
813*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[A\x1521")
814*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "21")
815*5a6e8488SAndroid Build Coastguard Worker		send(child, "quit")
816*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
817*5a6e8488SAndroid Build Coastguard Worker		wait(child)
818*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
819*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
820*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
821*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
822*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
823*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
824*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
825*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
826*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
827*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
828*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
829*5a6e8488SAndroid Build Coastguard Worker
830*5a6e8488SAndroid Build Coastguard Worker	return child
831*5a6e8488SAndroid Build Coastguard Worker
832*5a6e8488SAndroid Build Coastguard Worker
833*5a6e8488SAndroid Build Coastguard Worker# Swap.
834*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
835*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
836*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
837*5a6e8488SAndroid Build Coastguard Workerdef test_bc11(exe, args, env):
838*5a6e8488SAndroid Build Coastguard Worker
839*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
840*5a6e8488SAndroid Build Coastguard Worker
841*5a6e8488SAndroid Build Coastguard Worker	try:
842*5a6e8488SAndroid Build Coastguard Worker		bc_banner(child)
843*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[A\x02\x14")
844*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
845*5a6e8488SAndroid Build Coastguard Worker		expect(child, prompt)
846*5a6e8488SAndroid Build Coastguard Worker		write_str(child, "12 + 34 + 56 + 78")
847*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "180")
848*5a6e8488SAndroid Build Coastguard Worker		send(child, "\x1b[A\x02\x14")
849*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "189")
850*5a6e8488SAndroid Build Coastguard Worker		send(child, "quit")
851*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
852*5a6e8488SAndroid Build Coastguard Worker		wait(child)
853*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
854*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
855*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
856*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
857*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
858*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
859*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
860*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
861*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
862*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
863*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
864*5a6e8488SAndroid Build Coastguard Worker
865*5a6e8488SAndroid Build Coastguard Worker	return child
866*5a6e8488SAndroid Build Coastguard Worker
867*5a6e8488SAndroid Build Coastguard Worker
868*5a6e8488SAndroid Build Coastguard Worker# Non-fatal error.
869*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
870*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
871*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
872*5a6e8488SAndroid Build Coastguard Workerdef test_bc12(exe, args, env):
873*5a6e8488SAndroid Build Coastguard Worker
874*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
875*5a6e8488SAndroid Build Coastguard Worker
876*5a6e8488SAndroid Build Coastguard Worker	try:
877*5a6e8488SAndroid Build Coastguard Worker		bc_banner(child)
878*5a6e8488SAndroid Build Coastguard Worker		send(child, "12 +")
879*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
880*5a6e8488SAndroid Build Coastguard Worker		time.sleep(1)
881*5a6e8488SAndroid Build Coastguard Worker		if not child.isalive():
882*5a6e8488SAndroid Build Coastguard Worker			print("child exited early")
883*5a6e8488SAndroid Build Coastguard Worker			print(str(child))
884*5a6e8488SAndroid Build Coastguard Worker			print(str(child.buffer))
885*5a6e8488SAndroid Build Coastguard Worker			sys.exit(1)
886*5a6e8488SAndroid Build Coastguard Worker		send(child, "quit")
887*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
888*5a6e8488SAndroid Build Coastguard Worker		wait(child)
889*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
890*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
891*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
892*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
893*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
894*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
895*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
896*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
897*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
898*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
899*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
900*5a6e8488SAndroid Build Coastguard Worker
901*5a6e8488SAndroid Build Coastguard Worker	return child
902*5a6e8488SAndroid Build Coastguard Worker
903*5a6e8488SAndroid Build Coastguard Worker
904*5a6e8488SAndroid Build Coastguard Workerdef test_dc_utf8_0(exe, args, env):
905*5a6e8488SAndroid Build Coastguard Worker	return test_utf8_0(exe, args, env, False)
906*5a6e8488SAndroid Build Coastguard Worker
907*5a6e8488SAndroid Build Coastguard Worker
908*5a6e8488SAndroid Build Coastguard Workerdef test_dc_utf8_1(exe, args, env):
909*5a6e8488SAndroid Build Coastguard Worker	return test_utf8_1(exe, args, env, False)
910*5a6e8488SAndroid Build Coastguard Worker
911*5a6e8488SAndroid Build Coastguard Worker
912*5a6e8488SAndroid Build Coastguard Workerdef test_dc_utf8_2(exe, args, env):
913*5a6e8488SAndroid Build Coastguard Worker	return test_utf8_2(exe, args, env, False)
914*5a6e8488SAndroid Build Coastguard Worker
915*5a6e8488SAndroid Build Coastguard Worker
916*5a6e8488SAndroid Build Coastguard Workerdef test_dc_utf8_3(exe, args, env):
917*5a6e8488SAndroid Build Coastguard Worker	return test_utf8_3(exe, args, env, False)
918*5a6e8488SAndroid Build Coastguard Worker
919*5a6e8488SAndroid Build Coastguard Worker
920*5a6e8488SAndroid Build Coastguard Workerdef test_dc_utf8_4(exe, args, env):
921*5a6e8488SAndroid Build Coastguard Worker	return test_utf8_4(exe, args, env, False)
922*5a6e8488SAndroid Build Coastguard Worker
923*5a6e8488SAndroid Build Coastguard Worker
924*5a6e8488SAndroid Build Coastguard Worker# Basic dc test.
925*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
926*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
927*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
928*5a6e8488SAndroid Build Coastguard Workerdef test_dc1(exe, args, env):
929*5a6e8488SAndroid Build Coastguard Worker
930*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
931*5a6e8488SAndroid Build Coastguard Worker
932*5a6e8488SAndroid Build Coastguard Worker	try:
933*5a6e8488SAndroid Build Coastguard Worker		write_str(child, "1pR")
934*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "1")
935*5a6e8488SAndroid Build Coastguard Worker		write_str(child, "1pR")
936*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "1")
937*5a6e8488SAndroid Build Coastguard Worker		write_str(child, "q")
938*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
939*5a6e8488SAndroid Build Coastguard Worker		wait(child)
940*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
941*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
942*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
943*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
944*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
945*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
946*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
947*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
948*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
949*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
950*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
951*5a6e8488SAndroid Build Coastguard Worker
952*5a6e8488SAndroid Build Coastguard Worker	return child
953*5a6e8488SAndroid Build Coastguard Worker
954*5a6e8488SAndroid Build Coastguard Worker
955*5a6e8488SAndroid Build Coastguard Worker# SIGINT with quit.
956*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
957*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
958*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
959*5a6e8488SAndroid Build Coastguard Workerdef test_dc2(exe, args, env):
960*5a6e8488SAndroid Build Coastguard Worker
961*5a6e8488SAndroid Build Coastguard Worker	env["TERM"] = "dumb"
962*5a6e8488SAndroid Build Coastguard Worker
963*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
964*5a6e8488SAndroid Build Coastguard Worker
965*5a6e8488SAndroid Build Coastguard Worker	try:
966*5a6e8488SAndroid Build Coastguard Worker		child.sendline("1pR")
967*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "1", history=False)
968*5a6e8488SAndroid Build Coastguard Worker		time.sleep(1)
969*5a6e8488SAndroid Build Coastguard Worker		child.sendintr()
970*5a6e8488SAndroid Build Coastguard Worker		child.sendline("q")
971*5a6e8488SAndroid Build Coastguard Worker		wait(child)
972*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
973*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
974*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
975*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
976*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
977*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
978*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
979*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
980*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
981*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
982*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
983*5a6e8488SAndroid Build Coastguard Worker
984*5a6e8488SAndroid Build Coastguard Worker	return child
985*5a6e8488SAndroid Build Coastguard Worker
986*5a6e8488SAndroid Build Coastguard Worker
987*5a6e8488SAndroid Build Coastguard Worker# Execute string.
988*5a6e8488SAndroid Build Coastguard Worker# @param exe   The executable.
989*5a6e8488SAndroid Build Coastguard Worker# @param args  The arguments to pass to the executable.
990*5a6e8488SAndroid Build Coastguard Worker# @param env   The environment.
991*5a6e8488SAndroid Build Coastguard Workerdef test_dc3(exe, args, env):
992*5a6e8488SAndroid Build Coastguard Worker
993*5a6e8488SAndroid Build Coastguard Worker	child = pexpect.spawn(exe, args=args, env=env)
994*5a6e8488SAndroid Build Coastguard Worker
995*5a6e8488SAndroid Build Coastguard Worker	try:
996*5a6e8488SAndroid Build Coastguard Worker		write_str(child, "[1 15+pR]x")
997*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "16")
998*5a6e8488SAndroid Build Coastguard Worker		write_str(child, "1pR")
999*5a6e8488SAndroid Build Coastguard Worker		check_line(child, "1")
1000*5a6e8488SAndroid Build Coastguard Worker		write_str(child, "q")
1001*5a6e8488SAndroid Build Coastguard Worker		send(child, "\n")
1002*5a6e8488SAndroid Build Coastguard Worker		wait(child)
1003*5a6e8488SAndroid Build Coastguard Worker	except pexpect.TIMEOUT:
1004*5a6e8488SAndroid Build Coastguard Worker		traceback.print_tb(sys.exc_info()[2])
1005*5a6e8488SAndroid Build Coastguard Worker		print("timed out")
1006*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
1007*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
1008*5a6e8488SAndroid Build Coastguard Worker	except pexpect.EOF:
1009*5a6e8488SAndroid Build Coastguard Worker		print("EOF")
1010*5a6e8488SAndroid Build Coastguard Worker		print(str(child))
1011*5a6e8488SAndroid Build Coastguard Worker		print(str(child.buffer))
1012*5a6e8488SAndroid Build Coastguard Worker		print(str(child.before))
1013*5a6e8488SAndroid Build Coastguard Worker		sys.exit(2)
1014*5a6e8488SAndroid Build Coastguard Worker
1015*5a6e8488SAndroid Build Coastguard Worker	return child
1016*5a6e8488SAndroid Build Coastguard Worker
1017*5a6e8488SAndroid Build Coastguard Worker
1018*5a6e8488SAndroid Build Coastguard Worker# The array of bc tests.
1019*5a6e8488SAndroid Build Coastguard Workerbc_tests = [
1020*5a6e8488SAndroid Build Coastguard Worker	test_bc_utf8_0,
1021*5a6e8488SAndroid Build Coastguard Worker	test_bc_utf8_1,
1022*5a6e8488SAndroid Build Coastguard Worker	test_bc_utf8_2,
1023*5a6e8488SAndroid Build Coastguard Worker	test_bc_utf8_3,
1024*5a6e8488SAndroid Build Coastguard Worker	test_bc_utf8_4,
1025*5a6e8488SAndroid Build Coastguard Worker	test_sigint_sigquit,
1026*5a6e8488SAndroid Build Coastguard Worker	test_eof,
1027*5a6e8488SAndroid Build Coastguard Worker	test_sigint,
1028*5a6e8488SAndroid Build Coastguard Worker	test_sigtstp,
1029*5a6e8488SAndroid Build Coastguard Worker	test_sigstop,
1030*5a6e8488SAndroid Build Coastguard Worker	test_bc1,
1031*5a6e8488SAndroid Build Coastguard Worker	test_bc2,
1032*5a6e8488SAndroid Build Coastguard Worker	test_bc3,
1033*5a6e8488SAndroid Build Coastguard Worker	test_bc4,
1034*5a6e8488SAndroid Build Coastguard Worker	test_bc5,
1035*5a6e8488SAndroid Build Coastguard Worker	test_bc6,
1036*5a6e8488SAndroid Build Coastguard Worker	test_bc7,
1037*5a6e8488SAndroid Build Coastguard Worker	test_bc8,
1038*5a6e8488SAndroid Build Coastguard Worker	test_bc9,
1039*5a6e8488SAndroid Build Coastguard Worker	test_bc10,
1040*5a6e8488SAndroid Build Coastguard Worker	test_bc11,
1041*5a6e8488SAndroid Build Coastguard Worker	test_bc12,
1042*5a6e8488SAndroid Build Coastguard Worker]
1043*5a6e8488SAndroid Build Coastguard Worker
1044*5a6e8488SAndroid Build Coastguard Worker# The array of dc tests.
1045*5a6e8488SAndroid Build Coastguard Workerdc_tests = [
1046*5a6e8488SAndroid Build Coastguard Worker	test_dc_utf8_0,
1047*5a6e8488SAndroid Build Coastguard Worker	test_dc_utf8_1,
1048*5a6e8488SAndroid Build Coastguard Worker	test_dc_utf8_2,
1049*5a6e8488SAndroid Build Coastguard Worker	test_dc_utf8_3,
1050*5a6e8488SAndroid Build Coastguard Worker	test_dc_utf8_4,
1051*5a6e8488SAndroid Build Coastguard Worker	test_sigint_sigquit,
1052*5a6e8488SAndroid Build Coastguard Worker	test_eof,
1053*5a6e8488SAndroid Build Coastguard Worker	test_sigint,
1054*5a6e8488SAndroid Build Coastguard Worker	test_dc1,
1055*5a6e8488SAndroid Build Coastguard Worker	test_dc2,
1056*5a6e8488SAndroid Build Coastguard Worker	test_dc3,
1057*5a6e8488SAndroid Build Coastguard Worker]
1058*5a6e8488SAndroid Build Coastguard Worker
1059*5a6e8488SAndroid Build Coastguard Worker
1060*5a6e8488SAndroid Build Coastguard Worker# Print the usage and exit with an error.
1061*5a6e8488SAndroid Build Coastguard Workerdef usage():
1062*5a6e8488SAndroid Build Coastguard Worker	print("usage: {} [-t] dir [-a] test_idx [exe options...]".format(script))
1063*5a6e8488SAndroid Build Coastguard Worker	print("       The valid values for dir are: 'bc' and 'dc'.")
1064*5a6e8488SAndroid Build Coastguard Worker	print("       The max test_idx for bc is {}.".format(len(bc_tests) - 1))
1065*5a6e8488SAndroid Build Coastguard Worker	print("       The max test_idx for dc is {}.".format(len(dc_tests) - 1))
1066*5a6e8488SAndroid Build Coastguard Worker	print("       If -a is given, the number of tests for dir is printed.")
1067*5a6e8488SAndroid Build Coastguard Worker	print("       No tests are run.")
1068*5a6e8488SAndroid Build Coastguard Worker	sys.exit(1)
1069*5a6e8488SAndroid Build Coastguard Worker
1070*5a6e8488SAndroid Build Coastguard Worker
1071*5a6e8488SAndroid Build Coastguard Worker# Must run this script alone.
1072*5a6e8488SAndroid Build Coastguard Workerif __name__ != "__main__":
1073*5a6e8488SAndroid Build Coastguard Worker	usage()
1074*5a6e8488SAndroid Build Coastguard Worker
1075*5a6e8488SAndroid Build Coastguard Workerif len(sys.argv) < 2:
1076*5a6e8488SAndroid Build Coastguard Worker	usage()
1077*5a6e8488SAndroid Build Coastguard Worker
1078*5a6e8488SAndroid Build Coastguard Workeridx = 1
1079*5a6e8488SAndroid Build Coastguard Worker
1080*5a6e8488SAndroid Build Coastguard Workerexedir = sys.argv[idx]
1081*5a6e8488SAndroid Build Coastguard Worker
1082*5a6e8488SAndroid Build Coastguard Workeridx += 1
1083*5a6e8488SAndroid Build Coastguard Worker
1084*5a6e8488SAndroid Build Coastguard Workerif exedir == "-t":
1085*5a6e8488SAndroid Build Coastguard Worker	do_test = True
1086*5a6e8488SAndroid Build Coastguard Worker	exedir = sys.argv[idx]
1087*5a6e8488SAndroid Build Coastguard Worker	idx += 1
1088*5a6e8488SAndroid Build Coastguard Workerelse:
1089*5a6e8488SAndroid Build Coastguard Worker	do_test = False
1090*5a6e8488SAndroid Build Coastguard Worker
1091*5a6e8488SAndroid Build Coastguard Workertest_idx = sys.argv[idx]
1092*5a6e8488SAndroid Build Coastguard Worker
1093*5a6e8488SAndroid Build Coastguard Workeridx += 1
1094*5a6e8488SAndroid Build Coastguard Worker
1095*5a6e8488SAndroid Build Coastguard Workerif test_idx == "-a":
1096*5a6e8488SAndroid Build Coastguard Worker	if exedir == "bc":
1097*5a6e8488SAndroid Build Coastguard Worker		l = len(bc_tests)
1098*5a6e8488SAndroid Build Coastguard Worker	else:
1099*5a6e8488SAndroid Build Coastguard Worker		l = len(dc_tests)
1100*5a6e8488SAndroid Build Coastguard Worker	print("{}".format(l))
1101*5a6e8488SAndroid Build Coastguard Worker	sys.exit(0)
1102*5a6e8488SAndroid Build Coastguard Worker
1103*5a6e8488SAndroid Build Coastguard Workertest_idx = int(test_idx)
1104*5a6e8488SAndroid Build Coastguard Worker
1105*5a6e8488SAndroid Build Coastguard Worker# Set a default executable unless we have one.
1106*5a6e8488SAndroid Build Coastguard Workerif len(sys.argv) >= idx + 1:
1107*5a6e8488SAndroid Build Coastguard Worker	exe = sys.argv[idx]
1108*5a6e8488SAndroid Build Coastguard Workerelse:
1109*5a6e8488SAndroid Build Coastguard Worker	exe = testdir + "/../bin/" + exedir
1110*5a6e8488SAndroid Build Coastguard Worker
1111*5a6e8488SAndroid Build Coastguard Workerexebase = os.path.basename(exe)
1112*5a6e8488SAndroid Build Coastguard Worker
1113*5a6e8488SAndroid Build Coastguard Worker# Use the correct options.
1114*5a6e8488SAndroid Build Coastguard Workerif exebase == "bc":
1115*5a6e8488SAndroid Build Coastguard Worker	halt = "halt\n"
1116*5a6e8488SAndroid Build Coastguard Worker	options = "-l"
1117*5a6e8488SAndroid Build Coastguard Worker	test_array = bc_tests
1118*5a6e8488SAndroid Build Coastguard Workerelse:
1119*5a6e8488SAndroid Build Coastguard Worker	halt = "q\n"
1120*5a6e8488SAndroid Build Coastguard Worker	options = "-x"
1121*5a6e8488SAndroid Build Coastguard Worker	test_array = dc_tests
1122*5a6e8488SAndroid Build Coastguard Worker
1123*5a6e8488SAndroid Build Coastguard Worker# More command-line processing.
1124*5a6e8488SAndroid Build Coastguard Workerif len(sys.argv) > idx + 1:
1125*5a6e8488SAndroid Build Coastguard Worker	exe = [ exe, sys.argv[idx + 1:], options ]
1126*5a6e8488SAndroid Build Coastguard Workerelse:
1127*5a6e8488SAndroid Build Coastguard Worker	exe = [ exe, options ]
1128*5a6e8488SAndroid Build Coastguard Worker
1129*5a6e8488SAndroid Build Coastguard Worker# This is the environment necessary for most tests.
1130*5a6e8488SAndroid Build Coastguard Workerenv = {
1131*5a6e8488SAndroid Build Coastguard Worker	"BC_BANNER": "1",
1132*5a6e8488SAndroid Build Coastguard Worker	"BC_PROMPT": "1",
1133*5a6e8488SAndroid Build Coastguard Worker	"DC_PROMPT": "1",
1134*5a6e8488SAndroid Build Coastguard Worker	"BC_TTY_MODE": "1",
1135*5a6e8488SAndroid Build Coastguard Worker	"DC_TTY_MODE": "1",
1136*5a6e8488SAndroid Build Coastguard Worker	"BC_SIGINT_RESET": "1",
1137*5a6e8488SAndroid Build Coastguard Worker	"DC_SIGINT_RESET": "1",
1138*5a6e8488SAndroid Build Coastguard Worker}
1139*5a6e8488SAndroid Build Coastguard Worker
1140*5a6e8488SAndroid Build Coastguard Worker# Make sure to include the outside environment.
1141*5a6e8488SAndroid Build Coastguard Workerenv.update(os.environ)
1142*5a6e8488SAndroid Build Coastguard Workerenv.pop("BC_ENV_ARGS", None)
1143*5a6e8488SAndroid Build Coastguard Workerenv.pop("BC_LINE_LENGTH", None)
1144*5a6e8488SAndroid Build Coastguard Workerenv.pop("DC_ENV_ARGS", None)
1145*5a6e8488SAndroid Build Coastguard Workerenv.pop("DC_LINE_LENGTH", None)
1146*5a6e8488SAndroid Build Coastguard Worker
1147*5a6e8488SAndroid Build Coastguard Worker# Run the correct test.
1148*5a6e8488SAndroid Build Coastguard Workerchild = test_array[test_idx](exe[0], exe[1:], env)
1149*5a6e8488SAndroid Build Coastguard Worker
1150*5a6e8488SAndroid Build Coastguard Workerchild.close()
1151*5a6e8488SAndroid Build Coastguard Worker
1152*5a6e8488SAndroid Build Coastguard Workerexit = child.exitstatus
1153*5a6e8488SAndroid Build Coastguard Worker
1154*5a6e8488SAndroid Build Coastguard Workerif exit is not None and exit != 0:
1155*5a6e8488SAndroid Build Coastguard Worker	print("child failed; expected exit code 0, got {}".format(exit))
1156*5a6e8488SAndroid Build Coastguard Worker	print(str(child))
1157*5a6e8488SAndroid Build Coastguard Worker	sys.exit(1)
1158