Added tests for 'stack' class
All checks were successful
dc / build (push) Successful in 35s

This commit is contained in:
Marco Cetica 2024-02-29 10:04:45 +01:00
parent f7932f9566
commit e61a7debfa
Signed by: marco
GPG Key ID: 45060A949E90D0FD
8 changed files with 177 additions and 0 deletions

10
tests/test_clear Normal file
View File

@ -0,0 +1,10 @@
#!/bin/sh
utest() {
PROGRAM="$PWD/build/dc"
EXPECTED="Cannot print empty stack"
ACTUAL=$("$PROGRAM" -e '5 4 c p' 2>&1) || true
assert_eq "$EXPECTED" "$ACTUAL"
}
# vim: ts=4 sw=4 softtabstop=4 expandtab:

21
tests/test_count_digits Normal file
View File

@ -0,0 +1,21 @@
#!/bin/sh
utest() {
PROGRAM="$PWD/build/dc"
EXPECTED="4"
ACTUAL=$("$PROGRAM" -e '5430 Z p')
assert_eq "$EXPECTED" "$ACTUAL"
# Test with floats
PROGRAM="$PWD/build/dc"
EXPECTED="4"
ACTUAL=$("$PROGRAM" -e '54.30 Z p')
assert_eq "$EXPECTED" "$ACTUAL"
# Test with strings
PROGRAM="$PWD/build/dc"
EXPECTED="11"
ACTUAL=$("$PROGRAM" -e '[ Hello World ] Z p')
assert_eq "$EXPECTED" "$ACTUAL"
}
# vim: ts=4 sw=4 softtabstop=4 expandtab:

9
tests/test_dump Normal file
View File

@ -0,0 +1,9 @@
#!/bin/sh
utest() {
PROGRAM="$PWD/build/dc"
EXPECTED="25 20 15 10 5"
ACTUAL=$("$PROGRAM" -e '5 10 15 20 25 f' | tr '\n' ' ' | sed 's/ $/\n/')
assert_eq "$EXPECTED" "$ACTUAL"
}
# vim: ts=4 sw=4 softtabstop=4 expandtab:

9
tests/test_dup Normal file
View File

@ -0,0 +1,9 @@
#!/bin/sh
utest() {
PROGRAM="$PWD/build/dc"
EXPECTED="2"
ACTUAL=$("$PROGRAM" -e '5 d z p')
assert_eq "$EXPECTED" "$ACTUAL"
}
# vim: ts=4 sw=4 softtabstop=4 expandtab:

9
tests/test_pop Normal file
View File

@ -0,0 +1,9 @@
#!/bin/sh
utest() {
PROGRAM="$PWD/build/dc"
EXPECTED="5"
ACTUAL=$("$PROGRAM" -e '5 10 R p')
assert_eq "$EXPECTED" "$ACTUAL"
}
# vim: ts=4 sw=4 softtabstop=4 expandtab:

95
tests/test_prt Normal file
View File

@ -0,0 +1,95 @@
#!/bin/sh
test_print() {
PROGRAM="$PWD/build/dc"
EXPECTED="5"
ACTUAL=$("$PROGRAM" -e '5 p')
assert_eq "$EXPECTED" "$ACTUAL"
# Test empty stack
EXPECTED="Cannot print empty stack"
ACTUAL=$("$PROGRAM" -e 'p' 2>&1) || true
assert_eq "$EXPECTED" "$ACTUAL"
# Test non numerical values
EXPECTED="foo"
ACTUAL=$("$PROGRAM" -e '[ foo ] p' 2>&1) || true
assert_eq "$EXPECTED" "$ACTUAL"
}
test_print_bin() {
PROGRAM="$PWD/build/dc"
EXPECTED="1111b"
ACTUAL=$("$PROGRAM" -e '15 pb')
assert_eq "$EXPECTED" "$ACTUAL"
# Test empty stack
EXPECTED="Cannot print empty stack"
ACTUAL=$("$PROGRAM" -e 'pb' 2>&1) || true
assert_eq "$EXPECTED" "$ACTUAL"
# Test non numerical values
EXPECTED="This output radix requires integer values"
ACTUAL=$("$PROGRAM" -e '[ foo ] pb' 2>&1) || true
assert_eq "$EXPECTED" "$ACTUAL"
}
test_print_hex() {
PROGRAM="$PWD/build/dc"
EXPECTED="Fh"
ACTUAL=$("$PROGRAM" -e '15 ph')
assert_eq "$EXPECTED" "$ACTUAL"
# Test empty stack
EXPECTED="Cannot print empty stack"
ACTUAL=$("$PROGRAM" -e 'ph' 2>&1) || true
assert_eq "$EXPECTED" "$ACTUAL"
# Test non numerical values
EXPECTED="This output radix requires integer values"
ACTUAL=$("$PROGRAM" -e '[ foo ] ph' 2>&1) || true
assert_eq "$EXPECTED" "$ACTUAL"
}
test_print_oct() {
PROGRAM="$PWD/build/dc"
EXPECTED="17o"
ACTUAL=$("$PROGRAM" -e '15 po')
assert_eq "$EXPECTED" "$ACTUAL"
# Test empty stack
EXPECTED="Cannot print empty stack"
ACTUAL=$("$PROGRAM" -e 'po' 2>&1) || true
assert_eq "$EXPECTED" "$ACTUAL"
# Test non numerical values
EXPECTED="This output radix requires integer values"
ACTUAL=$("$PROGRAM" -e '[ foo ] po' 2>&1) || true
assert_eq "$EXPECTED" "$ACTUAL"
}
test_print_no_newline() {
PROGRAM="$PWD/build/dc"
EXPECTED="5"
ACTUAL=$("$PROGRAM" -e '5 P')
assert_eq "$EXPECTED" "$ACTUAL"
# Test empty stack
EXPECTED="Cannot print empty stack"
ACTUAL=$("$PROGRAM" -e 'P' 2>&1) || true
assert_eq "$EXPECTED" "$ACTUAL"
# Test non numerical values
EXPECTED="foo"
ACTUAL=$("$PROGRAM" -e '[ foo ] P' 2>&1) || true
assert_eq "$EXPECTED" "$ACTUAL"
}
utest() {
test_print
test_print_no_newline
test_print_bin
test_print_hex
test_print_oct
}
# vim: ts=4 sw=4 softtabstop=4 expandtab:

15
tests/test_stack_size Normal file
View File

@ -0,0 +1,15 @@
#!/bin/sh
utest() {
PROGRAM="$PWD/build/dc"
EXPECTED="4"
ACTUAL=$("$PROGRAM" -e '5 4 3 1 z p')
assert_eq "$EXPECTED" "$ACTUAL"
# Test empty stack
PROGRAM="$PWD/build/dc"
EXPECTED="0"
ACTUAL=$("$PROGRAM" -e 'z p')
assert_eq "$EXPECTED" "$ACTUAL"
}
# vim: ts=4 sw=4 softtabstop=4 expandtab:

9
tests/test_swap Normal file
View File

@ -0,0 +1,9 @@
#!/bin/sh
utest() {
PROGRAM="$PWD/build/dc"
EXPECTED="5"
ACTUAL=$("$PROGRAM" -e '5 10 r p')
assert_eq "$EXPECTED" "$ACTUAL"
}
# vim: ts=4 sw=4 softtabstop=4 expandtab: