#!/bin/sh

source $testsuitedir/lib_test.sh

attend_port_libre $PORT_TESTS

cat > PROGRAMME << EOP
	:goto client :when :clone
	:new INT, STR
	2 -> &0
	:open \$wa TCP < "0.0.0.0" "$PORT_TESTS"
:label loop_wait
	:wait \$wa > \$req
	:goto req :when :clone
	:close \$req
	:decrease &0
	:goto loop_wait :when @&0 > 0
	:delete &0,&1
	:close \$wa
	:shutdown
:label req
	:read \$req ~s LINE -> &1
	@&1 / "BLA[A-Z]*BLA" -> &1
	@&1 + "\\n" -> &1
	:write \$req ~s @&1
	:close \$req
	:delete &0,&1
	:shutdown -1
:label client
	:wait 1
	:new STR
	"ABLABBLACBLADBLAE\\n" -> &0
	:open \$req TCP > "localhost" "$PORT_TESTS"
	:write \$req ~s @&0
	:read \$req ~s LINE -> &0
	@&0 % "^." -> &0
	@&0 + "\\n" -> &0
	:close \$req
	:open \$req TCP > "localhost" "$PORT_TESTS"
	:write \$req ~s @&0
	:read \$req ~s LINE -> &0
	@&0 + "\\n" -> &0
	:close \$req
	:write :stdout ~s @&0
	:delete &0
	:shutdown -1
EOP

cat > MEMOIRE_INITIALE << EOM
# Memory:
# Aliases:
# Free addresses:
# Free blocks:
# End of memory
EOM

cat > MEMOIRE_REF << EOM
# Memory:
# Aliases:
# Free addresses:
# From &0 on 2 addresses
# Free blocks:
# Block of 2 free addresses from &0
# End of memory
EOM

cat > STDOUT_REF << EOS
BLACBLADBLA
EOS

cat > STDERR_REF << EOS
EOS

cat > CODE_RETOUR_REF << EOC
0
EOC

test_basique
