#!/bin/bash
# RAC test-suite - needs bash version 2 or higher
# Aurelio Marinho Jargas & Thobias Salazar Trevisan

#TODO [3  aaa  ]9   ]9[   [8]]   [[8]
# enable or disable tests here
do_opts_test=1
do_cols_test=1
do_rows_test=1
do_emu_test=1

# set DEBUG to 1 for verbose testings
DEBUG=0
[ "$1" = '-v' ] && DEBUG=1

# string comparisons - en_EN
RAC_EMPTY="Try \`rac --help' for more information."
RAC_VERSION='rac version 1.0'
RAC_HELP='Usage: rac [OPTIONS] ADDRESS [FILE]...'

# string comparisons - pt_BR
RAC_EMPTY="Tente \`rac --help' para mais informações."
RAC_VERSION='rac versão 1.1'
RAC_HELP='Uso: rac ENDEREÇO [ARQUIVO]'

# path config
RAC='./rac'
RACTMP=${TMP:-/tmp}/rac-tests


sample_text(){ sed '/^###TXT$/,/^###$/!d;/^###/d;s/^# //' $0; }
emu_result() { sed '/^###EMU$/,/^###$/!d;/^###/d;s/^# //' $0; }

all_tests=(
'  1   % 1'
' -1   % 9'
'  :   % 123456789'
'  : 1 % 1'
'  :-3 % 1234567'
' 1:   % 123456789'
'-3:   % 789'

' 1: 1 % 1'
' 1: 5 % 12345'
' 1:-3 % 1234567'
' 1:-1 % 123456789'
' 5: 1 % 54321'
' 5: 5 % 5'
' 5:-3 % 567'
' 5:-1 % 56789'
'-3: 1 % 7654321'
'-3: 5 % 765'
'-3:-3 % 7'
'-3:-1 % 789'
'-1: 1 % 987654321'
'-1: 5 % 98765'
'-1:-3 % 987'
'-1:-1 % 9'

'1;3;5 % 135'
'1;1;1 % 111'
'-1;-1 % 99'
'3; -1 % 39'
'1; 7: % 1789'
'-1;:3 % 9123'
'7;-3: % 7789'

' 5~ 1 % 56789'
' 1~ 5 % 16'
' 5~ 2 % 579'
'-5~ 2 % 579'
' 5~-2 % 531'
'-5~-2 % 531'

'   999  % '
'  -999  % '
' 999:   % 9'
'-999:   % 123456789'
'  : 999 % 123456789'
'  :-999 % 1'

' 1: 999 % 123456789'
' 1:-999 % 1'
'-1: 999 % 9'
'-1:-999 % 987654321'
' 999: 1 % 987654321'
' 999:-1 % 9'
'-999: 1 % 1'
'-999:-1 % 123456789'

' 999~ 1 % '
' 999~-1 % '
'-999~ 1 % '
'-999~-1 % '
' 1~ 999 % 1'
' 1~-999 % 1'
'-1~ 999 % 9'
'-1~-999 % 9'

' 1;5~2  % 1579'
'-1;-3:;5~-2 % 9789531'
)

all_opts=(
''          "$RAC_EMPTY"
'-V'        "$RAC_VERSION"
'--version' "$RAC_VERSION"
'-h'        "$RAC_HELP"
'--help'    "$RAC_HELP"
)


total_tests=${#all_tests[*]}
total_opts=${#all_opts[*]}

#---------------------------------------------------------------
### CMDLINE OPTIONS TEST
#---------------------------------------------------------------

if [ "$do_opts_test" = 1 ]; then
	i=0
	while [ $i -lt $total_opts ]; do
		option="${all_opts[$i]}"   ; i=$((i+1))
		resultok="${all_opts[$i]}" ; i=$((i+1))
		[ "$DEBUG" = 1 ] && echo "+ executing $option"
		result=$($RAC $option 2>&1 | head -1)
		[ "$result" != "$resultok" ] &&
		echo "ERROR: <$option> expected '$resultok', got '$result'"
	done
fi

#---------------------------------------------------------------
### COLUMNS TEST
#---------------------------------------------------------------

if [ "$do_cols_test" = 1 ]; then
	i=0
	while [ $i -lt $total_tests ]; do
		test_data=${all_tests[$i]} ; i=$((i+1))
		address="${test_data% %*}"
		address="${address//;/,}"    # change ; to ,
		address="[$address]"         # add braces
		resultok="${test_data#*% }"
		[ "$DEBUG" = 1 ] && echo "+ executing $address"
		result=$(echo 123456789 | $RAC "$address")
		[ "$result" != "$resultok" ] &&
		echo "ERROR: < $address > expected '$resultok', got '$result'"
	done
fi

#---------------------------------------------------------------
### LINES TESTS
#---------------------------------------------------------------

if [ "$do_rows_test" = 1 ]; then
	i=0
	while [ $i -lt $total_tests ]; do
		test_data=${all_tests[$i]} ; i=$((i+1))
		address="${test_data% %*}"
		resultok="${test_data#*% }"
		[ "$DEBUG" = 1 ] && echo "+ executing $address"
		result=$(sample_text | sed 10d | $RAC "$address" |
		         sed 's/:.*//' | tr -d '\n')
		[ "$result" != "$resultok" ] &&
		echo "ERROR: < $address > expected '$resultok', got '$result'"
	done
fi

#---------------------------------------------------------------
### EMULATION TESTS
#---------------------------------------------------------------

if [ "$do_emu_test" = 1 ]; then
	for cmd in \
	'head'                 '1:10'        \
	'head -5'              '1:5'         \
	'head -1'              '1'           \
	'tail'                 '-10:-1'      \
	'tail -5'              '-5:-1'       \
	'tail -1'              '-1'          \
	'tac'                  '-1:1'        \
	'rev'                  '[-1:1]'      \
	'sed -n 5p'            '5'           \
	'sed -n 3,5p'          '3:5'         \
	'sed 25q'              '1:25'        \
	'cut -c1,3,5'          '[1,3,5]'     \
	'cut -c5-'             '[5:-1]'      \
	'cut -c1,3,5-'         '[1,3,5:-1]'  \
	'rev|cut -c1'          '[-1]'        \
	'cut -c1-5|rev'        '[5:1]'       \
	'tail -5|tac'          '-1:-5'       \
	'tail -1|rev|cut -c1'  '-1[-1]'      \
	#'sed -n 3,7p|cut -c10-20' '3:7 | [10:20]' \
	do
		[ "$emulated" ] || { emulated=$cmd ; continue ; }
		echo "------(  $emulated  )-------(  $cmd  )-------"
		sample_text | $RAC $cmd
		emulated=''
	done > $RACTMP
	emu_result | diff -u - $RACTMP
	rm -f ${RACTMP:-/tmp/aaaaa}
fi


###TXT
# 1:345678901234567890
# 2:345678901234567890
# 3:345678901234567890
# 4:345678901234567890
# 5:345678901234567890
# 6:345678901234567890
# 7:345678901234567890
# 8:345678901234567890
# 9:345678901234567890
# 0:345678901234567890
###


###EMU
# ------(  head  )-------(  1:10  )-------
# 1:345678901234567890
# 2:345678901234567890
# 3:345678901234567890
# 4:345678901234567890
# 5:345678901234567890
# 6:345678901234567890
# 7:345678901234567890
# 8:345678901234567890
# 9:345678901234567890
# 0:345678901234567890
# ------(  head -5  )-------(  1:5  )-------
# 1:345678901234567890
# 2:345678901234567890
# 3:345678901234567890
# 4:345678901234567890
# 5:345678901234567890
# ------(  head -1  )-------(  1  )-------
# 1:345678901234567890
# ------(  tail  )-------(  -10:-1  )-------
# 1:345678901234567890
# 2:345678901234567890
# 3:345678901234567890
# 4:345678901234567890
# 5:345678901234567890
# 6:345678901234567890
# 7:345678901234567890
# 8:345678901234567890
# 9:345678901234567890
# 0:345678901234567890
# ------(  tail -5  )-------(  -5:-1  )-------
# 6:345678901234567890
# 7:345678901234567890
# 8:345678901234567890
# 9:345678901234567890
# 0:345678901234567890
# ------(  tail -1  )-------(  -1  )-------
# 0:345678901234567890
# ------(  tac  )-------(  -1:1  )-------
# 0:345678901234567890
# 9:345678901234567890
# 8:345678901234567890
# 7:345678901234567890
# 6:345678901234567890
# 5:345678901234567890
# 4:345678901234567890
# 3:345678901234567890
# 2:345678901234567890
# 1:345678901234567890
# ------(  rev  )-------(  [-1:1]  )-------
# 098765432109876543:1
# 098765432109876543:2
# 098765432109876543:3
# 098765432109876543:4
# 098765432109876543:5
# 098765432109876543:6
# 098765432109876543:7
# 098765432109876543:8
# 098765432109876543:9
# 098765432109876543:0
# ------(  sed -n 5p  )-------(  5  )-------
# 5:345678901234567890
# ------(  sed -n 3,5p  )-------(  3:5  )-------
# 3:345678901234567890
# 4:345678901234567890
# 5:345678901234567890
# ------(  sed 25q  )-------(  1:25  )-------
# 1:345678901234567890
# 2:345678901234567890
# 3:345678901234567890
# 4:345678901234567890
# 5:345678901234567890
# 6:345678901234567890
# 7:345678901234567890
# 8:345678901234567890
# 9:345678901234567890
# 0:345678901234567890
# ------(  cut -c1,3,5  )-------(  [1,3,5]  )-------
# 135
# 235
# 335
# 435
# 535
# 635
# 735
# 835
# 935
# 035
# ------(  cut -c5-  )-------(  [5:-1]  )-------
# 5678901234567890
# 5678901234567890
# 5678901234567890
# 5678901234567890
# 5678901234567890
# 5678901234567890
# 5678901234567890
# 5678901234567890
# 5678901234567890
# 5678901234567890
# ------(  cut -c1,3,5-  )-------(  [1,3,5:-1]  )-------
# 135678901234567890
# 235678901234567890
# 335678901234567890
# 435678901234567890
# 535678901234567890
# 635678901234567890
# 735678901234567890
# 835678901234567890
# 935678901234567890
# 035678901234567890
# ------(  rev|cut -c1  )-------(  [-1]  )-------
# 0
# 0
# 0
# 0
# 0
# 0
# 0
# 0
# 0
# 0
# ------(  cut -c1-5|rev  )-------(  [5:1]  )-------
# 543:1
# 543:2
# 543:3
# 543:4
# 543:5
# 543:6
# 543:7
# 543:8
# 543:9
# 543:0
# ------(  tail -5|tac  )-------(  -1:-5  )-------
# 0:345678901234567890
# 9:345678901234567890
# 8:345678901234567890
# 7:345678901234567890
# 6:345678901234567890
# ------(  tail -1|rev|cut -c1  )-------(  -1[-1]  )-------
# 0
###
