ocarina/tests/file

118 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
# Copyright 2014 (c) Anna Schumaker
. $(dirname $0)/_functions
function test_file
{
test_equal "./src/file.run $1" "$2"
}
function test_chmod
{
touch $2
chmod $1 $2
}
###
#
# Test filepaths
#
new_test "Filepath Test"
test_file -D INVALID
test_file -L INVALID
test_file "-D file.txt" "$DATA_DIR/file.txt"
test_file "-L file.txt" "$LEGACY_DIR/file.txt"
test_file "file.txt" INVALID
test_file "-D -v file.txt" "0"
test_file "-L -v file.txt" "0"
if [ -d $DATA_DIR ]; then
echo "ERROR: $DATA_DIR should not exist!"
exit 1
fi
###
#
# Test opening files
#
echo
new_test "File Open Test"
# Generic open testing
test_file "-o N file.txt" "ERROR: A file with hint = FILE_TYPE_INVALID cannot be opened"
test_file "-D -o N file.txt" "ERROR: NOT_OPEN is not a legal OpenMode"
test_file "-D -o W -O file.txt" "ERROR: File is already open" # This test creates a file
test_file "-D -o R -O file.txt" "ERROR: File is already open"
rm $DATA_DIR/*
# Test opening for read
test_file "-D -o R file.txt" "ERROR: File does not exist"
test_chmod -r $DATA_DIR/file.txt
test_file "-D -o R file.txt" "ERROR: File could not be opened for reading"
rm -r $DATA_DIR
# Test opening for write
test_file "-L -o W file.txt" "ERROR: Cannot write to legacy files"
touch $DATA_DIR
test_file "-D -o W file.txt" "ERROR: Could not make directory"
rm $DATA_DIR
mkdir -p $DATA_DIR
test_chmod -w $DATA_DIR/file.txt
test_file "-D -o W file.txt" "ERROR: Could not open file for writing"
rm -rf $DATA_DIR
###
#
# Test closing files
#
echo
new_test "File Close Test"
test_file "-D -c file.txt" ""
###
#
# Test FILE IO
#
data="ABCDE FGHIJ KLMNO PQRST UVWXYZ"
echo
new_test "File IO Test"
# Write to file
./src/file.run -D -w file.txt "$data"
start_test
assert_equal "$(cat $DATA_DIR/file.txt)" "0
$data"
# Read data back from file
test_file "-D -r file.txt" "ABCDE
FGHIJ
KLMNO
PQRST
UVWXYZ"
# Write different data to file
./src/file.run -D -w file.txt " $data"
start_test
assert_equal "$(cat $DATA_DIR/file.txt)" "0
$data"
# Read data back in a single line
test_file "-D -r -g file.txt" "$data"