~skiqqy/q

ref: ddd2ed1afc463d2fe9074639694b3eb1fa26c628 q/test.sh -rw-r--r-- 2.5 KiB
ddd2ed1aStephen Cochrane Added helper funcs 3 months ago
                                                                                
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
#!/bin/bash
# Tests for q

qdir=".q/test"
q="./q -d $qdir"
fail=0
tid=0 # Test id
RED='\033[0;31m'
GREEN='\033[92m'
NC='\033[0m'

put()
{
	printf "%s\n%s\n" "$1" "$2"
}

# Print expected output, 1-> title, 2-> ID, 3-> prio, 4-> desc
expect()
{
	cat << EOF
Title: $1
ID: $2
Priority: $3
$4
EOF
}

# Used to assert a test case.
assert()
{
	printf "Test %s " "$tid"

	if ! diff "$1" "$2" > /dev/null
	then
		printf "${RED}failed${NC}. Line $(caller)\n"
		((fail++))
	else
		printf "${GREEN}passed${NC}.\n"
	fi
	((tid++))
}

rm -rf "$qdir" # Ensure that q creates the qdir if its missing.

# Start clean everytime.
echo y | $q clean
assert <($q peek) <(printf '')
put a b | $q push
put c d | $q push 1
put e f | $q push 3

assert <($q peek) <(expect a 0 0 b)
assert <($q pop)  <(expect a 0 0 b)
assert <($q peek 2) <(expect c 1 1 d && echo; expect e 2 3 f)
assert <($q pop 2) <(expect c 1 1 d && echo; expect e 2 3 f)
assert <($q pop) <(printf '')

put g h | $q push 10
assert <($q pop) <(expect g 3 10 h)
put g h | $q push 10
assert <($q peek)  <(expect g 4 10 h)
printf "%s\n%s\n%s\n" z 1 z | $q ed 4 > /dev/null
assert <($q peek) <(expect z 4 1 z)
printf "\n\n\n" | $q ed 4 > /dev/null # Assert that all empty means nothing changed
assert <($q peek) <(expect z 4 1 z)
printf "%s\n%s\n%s\n" z z z | $q ed 4 > /dev/null
assert <($q pop) <(expect z 4 1 z)

put a b | $q push
put e f | $q push 3
put c d | $q push 1
assert <($q peek 2) <(expect a 5 0 b; echo; expect c 7 1 d)
assert <($q pop all) <(expect a 5 0 b; echo; expect c 7 1 d; echo; expect e 6 3 f)
assert <($q pop all) <(printf '')

put a b | $q push
put e f | $q push 3
put c d | $q push 1
echo y | $q clean
assert <($q peek) <(printf '')

put a b | $q push
put e f | $q push
put c d | $q push

assert <($q peek all) <(expect c 2 0 d; echo; expect e 1 0 f; echo; expect a 0 0 b)
assert <($q pop  all) <(expect c 2 0 d; echo; expect e 1 0 f; echo; expect a 0 0 b)

assert <(put c d | $q push abcd) <(echo "Priority must be an int in [0,inf)")
assert <(put c d | $q push -1) <(echo "Priority must be an int in [0,inf)")
assert <($q peek) <(printf '')
assert <($q pop) <(printf '')

echo y | $q clean
put a b | $q push 5
put e f | $q push
put c d | $q push
assert <($q peek all) <(expect c 2 5 d; echo; expect e 1 5 f; echo; expect a 0 5 b)
printf "%s\n%s\n%s\n" a 0 b | $q ed 0 > /dev/null
assert <($q pop  all) <(expect a 0 0 b; echo; expect c 2 5 d; echo; expect e 1 5 f)
assert <($q peek) <(printf '')
assert <($q pop) <(printf '')
assert <(printf "%s\n%s\n%s\n" a 0 b | $q ed 0) <(echo "Invalid ID")

# TODO: Add more tests
exit "$fail"