aboutsummaryrefslogtreecommitdiff
path: root/test.sh
blob: d27aebc32df34b1e52405bfb9ae0e21a39dd0332 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/sh

if [ $# -lt 1 ]; then
	echo "Usage $0 test_number [start] [end]"
	exit
fi

MAX_INT=2147483647
MIN_INT=-2147483648
MAX_INT1=2147483648
MIN_INT1=-2147483649

MAX_LONG=9223372036854775807
MIN_LONG=-9223372036854775808
MAX_LONG1=9223372036854775808
MIN_LONG1=-9223372036854775809

test_status=0

red() {
	printf "%s%s%s" "$(tput setaf 1)" "$1" "$(tput sgr 0)"
	test_status=1
}

green() {
	printf "%s%s%s" "$(tput setaf 2)" "$1" "$(tput sgr 0)"
}

assert() {
	if [ $# -ne 2 ]; then
		red "[KO] "
		return 0
	fi
	if [ -z "$1" ]; then
		red "[KO] "
		return 0
	fi
	if [ -z "$2" ]; then
		red "[KO] "
		return 0
	fi
	if [ "$1" = "$2" ]; then
		green "[OK] "
	else
		red "[KO] "
	fi
}

assert_ok() {
	assert "$1" "OK"
}

assert_error() {
	assert "$1" "Error"
}

if [ "$1" = "--bench" ]
then
    [ "$#" -ne 3 ] && echo 'Usage: ./test.sh --bench num_bench stack_len' && exit 1
    total=0
    num_bench="$2"
    stack_len="$3"
    for i in $(seq "$num_bench")
    do
        instruction_count="$(seq "$stack_len" | shuf | xargs ./push_swap | wc -l)"
        total=$((total + instruction_count))
        printf "%4d: %7d, current average: %7d\n" "$i" "$instruction_count" $((total / i))
    done
    printf "Final Total:   %7d\n" "$total"
    printf "Final Average: %7d\n" $((total / num_bench))
    exit 0
fi

if [ "$1" = "--perm" ]
then
    [ -z "$2" ] && echo 'Usage: ./test.sh --perm stack_len' && exit 1
    stack_len="$2"
    python <<EOF |
from itertools import permutations
print('\n'.join([' '.join(map(str, p)) for p in permutations(range(1, $stack_len + 1))]))
EOF
    while read -r perm
    do

        instructions=$(./push_swap $perm)
        instruction_count="$(echo "$instructions" | wc -w)"
        check=$(printf "%s\n" "$instructions" | ./checker $perm)
        printf "%s: %3d, check: %s, instructions: %s\n" "$perm" "$instruction_count" "$check" "$(echo $instructions)"
    done
    exit 0
fi

if [ "$1" = "--error" ]
then
	printf "No arguments:\n"
	if [ -z "$(./checker)" ]
   	then
        green "[OK] "
	else
        red "[KO] "
	fi
	if [ -z "$(./push_swap)" ]
   	then
        green "[OK] "
	else
        red "[KO] "
	fi

	printf "\nNot digit character\n"
	assert_error "$(./checker 1 2 3 four 2>&1)"
	assert_error "$(./checker -1 2 3-3 2>&1)"
	assert_error "$(./checker 1_000 23 0 -1 2>&1)"
	assert_error "$(./push_swap 1 2 3 four 2>&1)"
	assert_error "$(./push_swap -1 2 3-3 2>&1)"
	assert_error "$(./push_swap 1_000 23 0 -1 2>&1)"

	printf "\nDuplicates\n"
	assert_error "$(./checker 1 1 2 3 2>&1)"
	assert_error "$(./checker 1 2 3 3 2>&1)"
	assert_error "$(./checker -1 0 2 0 2>&1)"
	assert_error "$(./checker -100 2 -100 2>&1)"
	assert_error "$(./checker 1 1 2>&1)"
	assert_error "$(./push_swap 1 1 2 3 2>&1)"
	assert_error "$(./push_swap 1 2 3 3 2>&1)"
	assert_error "$(./push_swap -1 0 2 0 2>&1)"
	assert_error "$(./push_swap -100 2 -100 2>&1)"
	assert_error "$(./push_swap 1 1 2>&1)"

	printf "\nMax/min int\n"
	assert_ok    "$(printf "" | ./checker $MIN_INT 2>&1)"
	assert_ok    "$(printf "" | ./checker $MAX_INT 2>&1)"
	assert_error "$(printf "" | ./checker $MIN_INT1 2>&1)"
	assert_error "$(printf "" | ./checker $MAX_INT1 2>&1)"
	if [ -z "$(./push_swap $MIN_INT 2>&1)" ]
	then
        green "[OK] "
	else
        red "[KO] "
	fi
	if [ -z  "$(./push_swap $MAX_INT 2>&1)" ]
	then
        green "[OK] "
	else
        red "[KO] "
	fi
    assert_error "$(./push_swap $MIN_INT1 2>&1)"
    assert_error "$(./push_swap $MAX_INT1 2>&1)"


	printf "\nMax/min long\n"
	assert_error "$(printf "" | ./checker $MIN_LONG 2>&1)"
	assert_error "$(printf "" | ./checker $MAX_LONG 2>&1)"
	assert_error "$(printf "" | ./checker $MIN_LONG1 2>&1)"
	assert_error "$(printf "" | ./checker $MAX_LONG1 2>&1)"
	assert_error "$(./push_swap $MIN_LONG 2>&1)"
	assert_error "$(./push_swap $MAX_LONG 2>&1)"
	assert_error "$(./push_swap $MIN_LONG1 2>&1)"
	assert_error "$(./push_swap $MAX_LONG1 2>&1)"

	exit "$test_status"
fi

test_nb="$1"

if [ -z "$2" ]
then
	start=1
else
	start="$2"
fi

if [ -z "$3" ]
then
    end=100
else
    end="$3"
fi

[ -f result.log ] && rm result.log

for i in $(seq 1 "$test_nb")
do
	arg="$(seq "$start" "$end" | shuf | xargs)"
    # shellcheck disable=SC2086
	result="$(./push_swap $arg | ./checker $arg 2>&1)"

	case "$result" in
		"OK")
			green "."
			;;
		"KO")
			red  "!"
			echo "Test $i failed with: $arg" >> result.log
			;;
		"Error")
			red "!"
			echo "Test $i failed due to parsing error with: $arg" >> result.log
			;;
		*)
			red "Unexpected output"
			exit
			;;
	esac
done

exit "$test_status"