file: Finish updating the new file test
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
55f3f06ded
commit
7b358259bd
28
lib/file.cpp
28
lib/file.cpp
|
@ -50,21 +50,17 @@ const unsigned int File :: get_version()
|
||||||
|
|
||||||
bool File :: exists()
|
bool File :: exists()
|
||||||
{
|
{
|
||||||
return g_file_test(filename.c_str(), G_FILE_TEST_EXISTS);
|
return g_file_test(get_filepath(), G_FILE_TEST_EXISTS);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool File :: open_read()
|
bool File :: open_read()
|
||||||
{
|
{
|
||||||
if (!exists()) {
|
if (!exists())
|
||||||
dprint("ERROR: File does not exist\n");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
std::fstream::open(get_filepath(), std::fstream::in);
|
std::fstream::open(get_filepath(), std::fstream::in);
|
||||||
if (std::fstream::fail()) {
|
if (std::fstream::fail())
|
||||||
dprint("ERROR: File could not be opened for reading\n");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
mode = OPEN_READ;
|
mode = OPEN_READ;
|
||||||
std::fstream::operator>>(version);
|
std::fstream::operator>>(version);
|
||||||
|
@ -74,16 +70,12 @@ bool File :: open_read()
|
||||||
|
|
||||||
bool File :: open_write()
|
bool File :: open_write()
|
||||||
{
|
{
|
||||||
if (g_mkdir_with_parents(find_dir(), 0755) != 0) {
|
if (g_mkdir_with_parents(find_dir(), 0755) != 0)
|
||||||
dprint("ERROR: Could not make directory\n");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
std::fstream::open(get_filepath(), std::fstream::out);
|
std::fstream::open(get_filepath(), std::fstream::out);
|
||||||
if (std::fstream::fail()) {
|
if (std::fstream::fail())
|
||||||
dprint("ERROR: Could not open file for writing\n");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
mode = OPEN_WRITE;
|
mode = OPEN_WRITE;
|
||||||
std::fstream::operator<<(FILE_VERSION) << std::endl;
|
std::fstream::operator<<(FILE_VERSION) << std::endl;
|
||||||
|
@ -92,16 +84,8 @@ bool File :: open_write()
|
||||||
|
|
||||||
bool File :: open(OpenMode m)
|
bool File :: open(OpenMode m)
|
||||||
{
|
{
|
||||||
if (filename == "") {
|
if ((filename == "") || (m == NOT_OPEN) || (mode != NOT_OPEN))
|
||||||
dprint("ERROR: No filename specified\n");
|
|
||||||
return false;
|
return false;
|
||||||
} else if (m == NOT_OPEN) {
|
|
||||||
dprint("ERROR: NOT_OPEN is not a legal OpenMode\n");
|
|
||||||
return false;
|
|
||||||
} else if (mode != NOT_OPEN) {
|
|
||||||
dprint("ERROR: File is already open\n");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (m == OPEN_READ)
|
else if (m == OPEN_READ)
|
||||||
return open_read();
|
return open_read();
|
||||||
|
|
112
tests/file
112
tests/file
|
@ -1,112 +0,0 @@
|
||||||
#!/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 "-D file.txt" "$DATA_DIR/file.txt"
|
|
||||||
test_file "file.txt" INVALID
|
|
||||||
test_file "-D -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
|
|
||||||
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"
|
|
|
@ -1,23 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2014 (c) Anna Schumaker.
|
* Copyright 2014 (c) Anna Schumaker.
|
||||||
* Do stuff with files
|
|
||||||
*
|
|
||||||
* Usage: files -D|-L [-c -g -o {R, W, N} -O -r -v -w] name [DATA]
|
|
||||||
*
|
|
||||||
* -D: FILE_TYPE_DATA
|
|
||||||
* -L: FILE_TYPE_LEGACY
|
|
||||||
*
|
|
||||||
* -c: Test closing the file
|
|
||||||
* -g: Read file using getline()
|
|
||||||
* -o: Open the file for READ, WRITE, or NOT_OPEN
|
|
||||||
* -O: Open the file a second time
|
|
||||||
* -r: Read data from file
|
|
||||||
* -v: Print version and exit
|
|
||||||
* -w: Write data to file
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
#include <file.h>
|
#include <file.h>
|
||||||
#include <print.h>
|
|
||||||
#include "test.h"
|
#include "test.h"
|
||||||
|
|
||||||
|
|
||||||
|
@ -37,132 +21,50 @@ static void test_filepath()
|
||||||
test_equal(test :: data_dir_exists(), false);
|
test_equal(test :: data_dir_exists(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_open()
|
||||||
|
{
|
||||||
|
File a("");
|
||||||
|
|
||||||
|
test_equal(a.open(OPEN_READ), false);
|
||||||
|
test_equal(a.open(OPEN_WRITE), false);
|
||||||
|
|
||||||
|
File b("file.txt");
|
||||||
|
test_equal(b.open(NOT_OPEN), false);
|
||||||
|
test_equal(b.open(OPEN_READ), false);
|
||||||
|
test_equal(b.open(OPEN_WRITE), true);
|
||||||
|
test_equal(b.open(OPEN_WRITE), false);
|
||||||
|
b.close();
|
||||||
|
|
||||||
|
test_equal(test :: data_file_exists("file.txt"), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_io()
|
||||||
|
{
|
||||||
|
File a("file.txt");
|
||||||
|
|
||||||
|
test_equal(a.open(OPEN_WRITE), true);
|
||||||
|
a << "ABCDE FGHIJ KLMNO PQRST UVWXYZ" << std::endl;
|
||||||
|
a.close();
|
||||||
|
test_equal(a.exists(), true);
|
||||||
|
|
||||||
|
File b("file.txt");
|
||||||
|
std::string res;
|
||||||
|
|
||||||
|
test_equal(b.open(OPEN_READ), true);
|
||||||
|
b >> res;
|
||||||
|
test_equal(res, (std::string)"ABCDE");
|
||||||
|
b >> res;
|
||||||
|
test_equal(res, (std::string)"FGHIJ");
|
||||||
|
res = b.getline();
|
||||||
|
test_equal(res, (std::string)"KLMNO PQRST UVWXYZ");
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
test :: rm_data_dir();
|
||||||
|
|
||||||
run_test("File Constructor Test", test_filepath);
|
run_test("File Constructor Test", test_filepath);
|
||||||
|
run_test("File Open Test", test_open);
|
||||||
|
run_test("File I/O Test", test_io);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*#include <unistd.h>
|
|
||||||
|
|
||||||
enum action_t { CLOSE, OPEN, PATHS, READ, VERSION, WRITE };
|
|
||||||
|
|
||||||
int print_version(File &f)
|
|
||||||
{
|
|
||||||
print("%u\n", f.get_version());
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int print_filepath(File &f)
|
|
||||||
{
|
|
||||||
print("%s\n", f.get_filepath());
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int open_file(File &f, OpenMode mode)
|
|
||||||
{
|
|
||||||
if (f.open(mode) == true)
|
|
||||||
return 0;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
|
||||||
{
|
|
||||||
int c, ret;
|
|
||||||
action_t action = PATHS;
|
|
||||||
FileLocHint hint = FILE_TYPE_INVALID;
|
|
||||||
OpenMode mode = NOT_OPEN;
|
|
||||||
bool second_open = false;
|
|
||||||
bool getline = false;
|
|
||||||
std::string file;
|
|
||||||
std::string data;
|
|
||||||
|
|
||||||
while ((c = getopt(argc, argv, "cDgo:Orvw")) != -1) {
|
|
||||||
switch (c) {
|
|
||||||
case 'c':
|
|
||||||
action = CLOSE;
|
|
||||||
break;
|
|
||||||
case 'D':
|
|
||||||
hint = FILE_TYPE_DATA;
|
|
||||||
break;
|
|
||||||
case 'g':
|
|
||||||
getline = true;
|
|
||||||
break;
|
|
||||||
case 'o':
|
|
||||||
action = OPEN;
|
|
||||||
switch (optarg[0]) {
|
|
||||||
case 'R':
|
|
||||||
mode = OPEN_READ;
|
|
||||||
break;
|
|
||||||
case 'W':
|
|
||||||
mode = OPEN_WRITE;
|
|
||||||
break;
|
|
||||||
case 'N':
|
|
||||||
mode = NOT_OPEN;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
print("Invalid open mode\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'O':
|
|
||||||
second_open = true;
|
|
||||||
break;
|
|
||||||
case 'r':
|
|
||||||
action = READ;
|
|
||||||
break;
|
|
||||||
case 'v':
|
|
||||||
action = VERSION;
|
|
||||||
break;
|
|
||||||
case 'w':
|
|
||||||
action = WRITE;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (optind < argc)
|
|
||||||
file = argv[optind++];
|
|
||||||
if (optind < argc)
|
|
||||||
data = argv[optind++];
|
|
||||||
|
|
||||||
File f(file, hint);
|
|
||||||
switch (action) {
|
|
||||||
case CLOSE:
|
|
||||||
ret = open_file(f, OPEN_WRITE);
|
|
||||||
if (ret == 0) {
|
|
||||||
f.close();
|
|
||||||
ret = open_file(f, OPEN_WRITE);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
case OPEN:
|
|
||||||
ret = open_file(f, mode);
|
|
||||||
if ((ret == 0) && (second_open == true))
|
|
||||||
ret = open_file(f, mode);
|
|
||||||
return ret;
|
|
||||||
case PATHS:
|
|
||||||
return print_filepath(f);
|
|
||||||
case READ:
|
|
||||||
ret = open_file(f, OPEN_READ);
|
|
||||||
if (ret == 0) {
|
|
||||||
do {
|
|
||||||
if (getline == true)
|
|
||||||
data = f.getline();
|
|
||||||
else
|
|
||||||
f >> data;
|
|
||||||
if (f.good() == false)
|
|
||||||
break;
|
|
||||||
print("%s\n", data.c_str());
|
|
||||||
} while (true);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
case VERSION:
|
|
||||||
return print_version(f);
|
|
||||||
case WRITE:
|
|
||||||
ret = open_file(f, OPEN_WRITE);
|
|
||||||
if (ret == 0)
|
|
||||||
f << data << std::endl;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
|
@ -72,6 +72,18 @@ namespace test
|
||||||
{
|
{
|
||||||
return g_file_test(data_dir().c_str(), G_FILE_TEST_IS_DIR);
|
return g_file_test(data_dir().c_str(), G_FILE_TEST_IS_DIR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool data_file_exists(const std::string &name)
|
||||||
|
{
|
||||||
|
return g_file_test(data_file(name).c_str(), G_FILE_TEST_EXISTS);
|
||||||
|
}
|
||||||
|
|
||||||
|
void rm_data_dir()
|
||||||
|
{
|
||||||
|
std::string cmd = "rm -r " + data_dir() + " 2>/dev/null";
|
||||||
|
if (data_dir_exists())
|
||||||
|
system(cmd.c_str());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#define run_test(name, func, ...) \
|
#define run_test(name, func, ...) \
|
||||||
|
|
Loading…
Reference in New Issue