#! /bin/sh

test () {
echo "Test $1" >&2
if which valgrind > /dev/null ; then
        valgrind --leak-check=full ./programa > /tmp/salidaPROG${1}.$USER 2>&1
else
        echo "valgrind not installed: untested" >&2
        ./programa > /tmp/salidaPROG${1}.$USER 2>&1
fi
diff -w /tmp/salidaOK${1}.$USER /tmp/salidaPROG${1}.$USER | egrep -v '^..==|^..pla|Teclee' > /tmp/diferencias${1}.$USER
grep leak /tmp/salidaPROG${1}.$USER | tee -a /tmp/leaks.$USER >&2
numdiferencias=`wc -l < /tmp/diferencias${1}.$USER`
if [ $numdiferencias -gt $2 ] ; then
        cat >&2 <<++
DIFFS
Lines which start with < are the expected output
Lines which start with > are the current output
++
        cat /tmp/diferencias${1}.$USER >&2
        return 1
else
        return 0
fi
}


test1 () {
# option: 4
rm -f /tmp/salida1OK.* /tmp/salida1PROG.*
cat <<++ > /tmp/salida1OK.$USER

		<MENU>

[1]	Load data of the wifi networks into an array
[2]	List active networks
[3]	Show the data of a selected network
[4]	Exit

++
test 1 <<++
4
++
}

test2 () {
# options: 1,2,4
rm -f /tmp/salida2OK.* /tmp/salida2PROG.*
cat <<++ > /tmp/salida2OK.$USER

		<MENU>

[1]	Load data of the wifi networks into an array
[2]	List active networks
[3]	Show the data of a selected network
[4]	Exit

	Select an option:
		<MENU>

[1]	Load data of the wifi networks into an array
[2]	List active networks
[3]	Show the data of a selected network
[4]	Exit

	Select an option:
Network		SSID		MAC	       Mode	Channel	Encrypted	Quality
[1]	      WiFi-UC3M	5F:F3:B1:9D:50:2C	  6	   48	         0	      0
[2]	   Livebox-F568	61:99:7E:9F:EC:FC	  5	   13	         1	     33
[3]	       TESTGAST	B5:97:88:A8:99:28	  2	   64	         0	     39
[4]	        eduroam	7F:48:FD:C8:C9:73	  0	   16	         0	      0
[5]	 GAST-WIFI-LDAP	4E:38:49:B5:87:9F	  6	   49	         0	     24
[6]	          WLITa	64:EA:9A:6B:D4:8C	  2	   41	         1	      8

		<MENU>

[1]	Load data of the wifi networks into an array
[2]	List active networks
[3]	Show the data of a selected network
[4]	Exit

++
test 2 <<++
1
2
4
++
}


test3 () {
# options: 1,3,4
rm -f /tmp/salida3OK.* /tmp/salida3PROG.*
cat <<++ > /tmp/salida3OK.$USER

		<MENU>

[1]	Load data of the wifi networks into an array
[2]	List active networks
[3]	Show the data of a selected network
[4]	Exit

	Select an option:
		<MENU>

[1]	Load data of the wifi networks into an array
[2]	List active networks
[3]	Show the data of a selected network
[4]	Exit

	Select an option:
		Teclee un número de red:
SSID:        WiFi-UC3M
MAC: 5F:F3:B1:9D:50:2C
Mode:                6
Channel:              48
Encrypted:          0
Quality:             0

		<MENU>

[1]	Load data of the wifi networks into an array
[2]	List active networks
[3]	Show the data of a selected network
[4]	Exit

	Select an option:
		Teclee un número de red:
SSID:         TESTGAST
MAC: B5:97:88:A8:99:28
Mode:                2
Channel:              64
Encrypted:          0
Quality:            39

		<MENU>

[1]	Load data of the wifi networks into an array
[2]	List active networks
[3]	Show the data of a selected network
[4]	Exit

	Select an option:
		Teclee un número de red:
SSID:        WiFi-UC3M
MAC: 5F:F3:B1:9D:50:2C
Mode:                6
Channel:              48
Encrypted:          0
Quality:             0

		<MENU>

[1]	Load data of the wifi networks into an array
[2]	List active networks
[3]	Show the data of a selected network
[4]	Exit

	Select an option:
		Teclee un número de red:
SSID:     Livebox-F568
MAC: 61:99:7E:9F:EC:FC
Mode:                5
Channel:              13
Encrypted:          1
Quality:            33

		<MENU>

[1]	Load data of the wifi networks into an array
[2]	List active networks
[3]	Show the data of a selected network
[4]	Exit

++

test 3 <<++
1
3
1
3
3
3
1
3
2
4
++
}


! gcc -g -Wall -pipe *.c data_read.o -o programa && echo "FAILED: (gcc failed)" && exit 1
test1 3 || echo "FAILED"
test2 3 || echo "FAILED"
test3 3 || echo "FAILED"
