Browse Source

* add tests for new i/o functions

master
Alex 'AdUser' Z 8 years ago
parent
commit
245222705d
  1. 3
      tests/CMakeLists.txt
  2. 78
      tests/io.c

3
tests/CMakeLists.txt

@ -4,5 +4,8 @@ add_test("test/bitmap" "test-bitmap")
add_executable("test-record" "record.c") add_executable("test-record" "record.c")
add_test("test/record" "test-record") add_test("test/record" "test-record")
add_executable("test-io" "io.c" "../src/database.c" "../src/bitmap.c")
add_test("test/io" "test-io")
add_executable("test-database" "database.c" "../src/database.c" "../src/bitmap.c") add_executable("test-database" "database.c" "../src/database.c" "../src/bitmap.c")
add_test("test/database" "test-database") add_test("test/database" "test-database")

78
tests/io.c

@ -0,0 +1,78 @@
#include "../src/common.h"
#include "../src/record.h"
#include "../src/io.h"
#include "../src/simdb.h"
int main() {
simdb_t *db;
simdb_urec_t *data;
simdb_urec_t rec[2];
char *path = "test.db";
int mode = 0, ret = 0;
unlink(path);
db = simdb_open(path, mode, &ret);
assert(db == NULL);
assert(ret == -1); /* no such file */
ret = simdb_create(path);
assert(ret == true);
db = simdb_open(path, mode, &ret);
assert(db != NULL);
ret = simdb_read(db, 1, 1, &data);
assert(ret == 0);
memset(&rec[0], 0xAA, SIMDB_REC_LEN);
memset(&rec[1], 0xAA, SIMDB_REC_LEN);
rec[0].used = 1;
rec[1].used = 1;
ret = simdb_write(db, 1, 2, rec);
assert(ret == SIMDB_ERR_READONLY); /* database open in read-only mode */
simdb_close(db);
mode |= SIMDB_FLAG_WRITE;
db = simdb_open(path, mode, &ret);
assert(db != NULL);
ret = simdb_write(db, 1, 2, rec);
assert(ret == 2); /* success */
ret = simdb_read(db, 1, 0, &data);
assert(ret == SIMDB_ERR_USAGE);
ret = simdb_read(db, 0, 0, &data);
assert(ret == SIMDB_ERR_USAGE);
ret = simdb_read(db, 0, 1, &data);
assert(ret == SIMDB_ERR_USAGE);
/* valid request : single record */
ret = simdb_read(db, 1, 1, &data);
assert(ret == 1);
free(data);
/* valid request : multiple records */
ret = simdb_read(db, 1, 2, &data);
assert(ret == 2);
free(data);
/* range beyond end of file */
ret = simdb_read(db, 1, 4, &data);
assert(ret == 2);
free(data);
/* start beyond end of file */
ret = simdb_read(db, 3, 4, &data);
assert(ret == 0);
simdb_close(db);
unlink(path);
return 0;
}
Loading…
Cancel
Save