Working through ex17 extra credit (again) and writing tests this time

This commit is contained in:
Dan Buch 2012-04-13 08:36:43 -04:00
parent d6c08d37b9
commit e172f73c82
4 changed files with 293 additions and 0 deletions

2
.gitignore vendored
View File

@ -8,6 +8,8 @@ ex14
ex15
ex16
ex17
ex17-*
!ex17-*.c
ex18
ex19
ex20

201
ex17-ec1.c Normal file
View File

@ -0,0 +1,201 @@
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define MAX_DATA 512
#define MAX_ROWS 100
struct Address {
int id;
int set;
char name[MAX_DATA];
char email[MAX_DATA];
};
struct Database {
struct Address rows[MAX_ROWS];
};
struct Connection {
FILE *file;
struct Database *db;
};
void Database_close(struct Connection *conn);
void die(struct Connection *conn, const char *message)
{
if(errno) {
perror(message);
} else {
printf("ERROR: %s\n", message);
}
Database_close(conn);
exit(1);
}
void Address_print(struct Address *addr)
{
printf("%d %s %s\n",
addr->id, addr->name, addr->email);
}
void Database_load(struct Connection *conn)
{
int rc = fread(conn->db, sizeof(struct Database), 1, conn->file);
if(rc != 1) die(conn, "Failed to load database.");
}
struct Connection* Database_open(const char *filename, char mode)
{
struct Connection *conn = malloc(sizeof(struct Connection));
if(!conn) die(NULL, "Memory error");
conn->db = malloc(sizeof(struct Database));
if(!conn->db) die(conn, "Memory error");
if(mode == 'c') {
conn->file = fopen(filename, "w");
} else {
conn->file = fopen(filename, "r+");
if(conn->file) {
Database_load(conn);
}
}
if(!conn->file) die(conn, "Failed to open the file");
return conn;
}
void Database_close(struct Connection *conn)
{
if(conn) {
if(conn->file) fclose(conn->file);
if(conn->db) free(conn->db);
free(conn);
}
}
void Database_write(struct Connection *conn)
{
rewind(conn->file);
int rc = fwrite(conn->db, sizeof(struct Database), 1, conn->file);
if(rc != 1) die(conn, "Failed to write database.");
rc = fflush(conn->file);
if(rc == -1) die(conn, "Cannot flush database.");
}
void Database_create(struct Connection *conn)
{
int i = 0;
for(i = 0; i < MAX_ROWS; i++) {
// make a prototype to initialize it
struct Address addr = {.id = i, .set = 0};
// then just assign it
conn->db->rows[i] = addr;
}
}
void Database_set(struct Connection *conn, int id, const char *name, const char *email)
{
struct Address *addr = &conn->db->rows[id];
if(addr->set) die(conn, "Already set, delete it first");
addr->set = 1;
// WARNING: bug, read the "How To Break It" and fix this
char *res = strncpy(addr->name, name, MAX_DATA);
// demonstrate the strncpy bug
if(!res) die(conn, "Name copy failed");
res = strncpy(addr->email, email, MAX_DATA);
if(!res) die(conn, "Email copy failed");
}
void Database_get(struct Connection *conn, int id)
{
struct Address *addr = &conn->db->rows[id];
if(addr->set) {
Address_print(addr);
} else {
die(conn, "ID is not set");
}
}
void Database_delete(struct Connection *conn, int id)
{
struct Address addr = {.id = id, .set = 0};
conn->db->rows[id] = addr;
}
void Database_list(struct Connection *conn)
{
int i = 0;
struct Database *db = conn->db;
for(i = 0; i < MAX_ROWS; i++) {
struct Address *cur = &db->rows[i];
if(cur->set) {
Address_print(cur);
}
}
}
int main(int argc, char *argv[])
{
if(argc < 3) die(NULL, "USAGE: ex17 <dbfile> <action> [action params]");
char *filename = argv[1];
char action = argv[2][0];
struct Connection *conn = Database_open(filename, action);
int id = 0;
if(argc > 3) id = atoi(argv[3]);
if(id >= MAX_ROWS) die(conn, "There's not that many records.");
switch(action) {
case 'c':
Database_create(conn);
Database_write(conn);
break;
case 'g':
if(argc != 4) die(conn, "Need an id to get");
Database_get(conn, id);
break;
case 's':
if(argc != 6) die(conn, "Need id, name, email to set");
Database_set(conn, id, argv[4], argv[5]);
Database_write(conn);
break;
case 'd':
if(argc != 4) die(conn, "Need id to delete");
Database_delete(conn, id);
Database_write(conn);
break;
case 'l':
Database_list(conn);
break;
default:
die(conn, "Invalid action, only: c=create, g=get, s=set, d=del, l=list");
}
Database_close(conn);
return 0;
}

35
test.sh Normal file
View File

@ -0,0 +1,35 @@
EXIT_CODE=0
ASSERT_COUNT=0
function assert_equal(){
if [[ $1 == $2 ]] ; then
echo -n "."
else
echo -n "F"
EXIT_CODE=1
fi
ASSERT_COUNT=$(expr $ASSERT_COUNT + 1)
}
function assert_not_equal(){
if [[ $1 != $2 ]] ; then
echo -n "."
else
echo -n "F"
EXIT_CODE=1
fi
ASSERT_COUNT=$(expr $ASSERT_COUNT + 1)
}
function test_finish(){
echo
if [ $EXIT_CODE -eq 0 ]; then
echo "PASS"
else
echo "FAIL"
fi
exit $EXIT_CODE
}

55
test_ex17.sh Executable file
View File

@ -0,0 +1,55 @@
#!/bin/bash
source './test.sh'
prog=${1-'./ex17'}
test_db=db.$$.$RANDOM.dat
# ----------------------------------------------------------------------------
$prog 2>&1 >/dev/null
assert_equal $? 1
$prog $test_db c 2>&1 >/dev/null
assert_equal $? 0
$prog $test_db s 2>&1 >/dev/null
assert_equal $? 1
$prog $test_db s 1 ham ham@foo.bar
assert_equal $? 0
$prog $test_db s 2 derp derp@foo.bar
assert_equal $? 0
$prog $test_db s 3 herp herp@foo.bar
assert_equal $? 0
$prog $test_db s 4 zap zap@foo.bar
assert_equal $? 0
$prog $test_db l > $$.out
grep '1 ham ham@foo.bar' $$.out >/dev/null
assert_equal $? 0
$prog $test_db l > $$.out
grep '2 derp derp@foo.bar' $$.out >/dev/null
assert_equal $? 0
$prog $test_db l > $$.out
grep '3 herp herp@foo.bar' $$.out >/dev/null
assert_equal $? 0
$prog $test_db l > $$.out
grep '4 zap zap@foo.bar' $$.out >/dev/null
assert_equal $? 0
$prog $test_db g 1 > $$.out
grep '1 ham ham@foo.bar' $$.out >/dev/null
assert_equal $? 0
$prog $test_db d 1
$prog $test_db l > $$.out
grep 'ham@foo.bar' $$.out >/dev/null
assert_not_equal $? 0
# ----------------------------------------------------------------------------
rm -f $$.out
rm -f $test_db
test_finish