#!/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"