Implemented basic unit tests

This commit is contained in:
Mario Danic
2007-06-01 12:57:09 +00:00
parent 89f291f412
commit 44b504640f
9 changed files with 1011 additions and 15 deletions

18
test/test.c Normal file
View File

@ -0,0 +1,18 @@
#include "test.h"
int main(int argc, char **argv)
{
test_tree();
test_exclude();
test_file_hashtable();
return 0;
}
#include "test.h"
int main(int argc, char **argv)
{
test_tree();
test_exclude();
test_file_hashtable();
return 0;
}

26
test/test.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef TEST_H_
#define TEST_H_
#include <sys/types.h>
#include <stddef.h>
void test_tree();
void test_exclude();
void test_file_hashtable();
#endif /*TEST_H_*/
#ifndef TEST_H_
#define TEST_H_
#include <sys/types.h>
#include <stddef.h>
void test_tree();
void test_exclude();
void test_file_hashtable();
#endif /*TEST_H_*/

58
test/test_exclude.c Normal file
View File

@ -0,0 +1,58 @@
/*
* Unit test for exclude.h
*/
#include "exclude.h"
#include "test.h"
#include <assert.h>
#include <stdio.h>
void test_exclude()
{
struct iso_hash_table table = { {0,}, 0};
printf("Testing exclude hashtable...");
assert( !iso_exclude_lookup(&table, "/dir") );
assert( !iso_exclude_lookup(&table, "/otherdir") );
iso_exclude_add_path(&table, "/otherdir");
assert( iso_exclude_lookup(&table, "/otherdir") );
assert( !iso_exclude_lookup(&table, "/dir") );
iso_exclude_add_path(&table, "/dir");
assert( iso_exclude_lookup(&table, "/otherdir") );
assert( iso_exclude_lookup(&table, "/dir") );
iso_exclude_empty(&table);
assert( !iso_exclude_lookup(&table, "/dir") );
assert( !iso_exclude_lookup(&table, "/otherdir") );
printf("\tOK\n");
}
/*
* Unit test for exclude.h
*/
#include "exclude.h"
#include "test.h"
#include <assert.h>
#include <stdio.h>
void test_exclude()
{
struct iso_hash_table table = { {0,}, 0};
printf("Testing exclude hashtable...");
assert( !iso_exclude_lookup(&table, "/dir") );
assert( !iso_exclude_lookup(&table, "/otherdir") );
iso_exclude_add_path(&table, "/otherdir");
assert( iso_exclude_lookup(&table, "/otherdir") );
assert( !iso_exclude_lookup(&table, "/dir") );
iso_exclude_add_path(&table, "/dir");
assert( iso_exclude_lookup(&table, "/otherdir") );
assert( iso_exclude_lookup(&table, "/dir") );
iso_exclude_empty(&table);
assert( !iso_exclude_lookup(&table, "/dir") );
assert( !iso_exclude_lookup(&table, "/otherdir") );
printf("\tOK\n");
}

104
test/test_file_hashtable.c Normal file
View File

@ -0,0 +1,104 @@
/*
* Unit test for exclude.h
*/
#include "test.h"
#include "file.h"
#include "tree.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void test_cache_inodes()
{
struct iso_file_table *table;
struct iso_tree_node_file *file1;
struct iso_tree_node_file *file2;
struct iso_tree_node_file *file3;
struct iso_file *iso1;
printf("Testing file hashtable with cache inodes...");
table = iso_file_table_new(1);
assert( table );
assert( table->cache_inodes );
file1 = calloc(1, sizeof(struct iso_tree_node_file) );
file1->node.name = "fileName";
file1->node.attrib.st_size = 12;
file1->node.attrib.st_dev = 15;
file1->node.attrib.st_ino = 204;
file1->path = "/tmp/filename";
file1->sort_weight = 1;
iso1 = iso_file_new(file1);
assert( !strcmp(iso1->path, "/tmp/filename") );
//TODO
free( file1 );
printf("\tOK\n");
}
void test_file_hashtable()
{
test_cache_inodes();
}
/*
* Unit test for exclude.h
*/
#include "test.h"
#include "file.h"
#include "tree.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void test_cache_inodes()
{
struct iso_file_table *table;
struct iso_tree_node_file *file1;
struct iso_tree_node_file *file2;
struct iso_tree_node_file *file3;
struct iso_file *iso1;
printf("Testing file hashtable with cache inodes...");
table = iso_file_table_new(1);
assert( table );
assert( table->cache_inodes );
file1 = calloc(1, sizeof(struct iso_tree_node_file) );
file1->node.name = "fileName";
file1->node.attrib.st_size = 12;
file1->node.attrib.st_dev = 15;
file1->node.attrib.st_ino = 204;
file1->path = "/tmp/filename";
file1->sort_weight = 1;
iso1 = iso_file_new(file1);
assert( !strcmp(iso1->path, "/tmp/filename") );
//TODO
free( file1 );
printf("\tOK\n");
}
void test_file_hashtable()
{
test_cache_inodes();
}

258
test/test_tree.c Normal file
View File

@ -0,0 +1,258 @@
/*
* Unit test for tree.h
*/
#include "libisofs.h"
#include "tree.h"
#include "test.h"
#include <assert.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
static void test_new_root() {
struct iso_tree_node_dir *root;
printf("Testing iso_tree_new_root()...");
root = iso_tree_new_root();
assert(root);
assert(root->nchildren == 0);
assert(root->children == NULL);
assert(root->node.parent == NULL);
assert(root->node.name == NULL);
assert( S_ISDIR(root->node.attrib.st_mode) );
printf("\tOK\n");
}
static void test_add_dir() {
struct iso_tree_node_dir *root;
struct iso_tree_node_dir *dir;
printf("Testing iso_tree_add_dir()...");
root = iso_tree_new_root();
dir = iso_tree_add_dir(root, "New dir name");
assert(root);
assert(dir);
assert(dir->node.parent == root);
assert(root->nchildren == 1);
assert(root->children[0] == (struct iso_tree_node *)dir);
assert( strcmp(dir->node.name, "New dir name") == 0 );
assert( S_ISDIR(dir->node.attrib.st_mode) );
printf("\tOK\n");
}
static void test_add_file() {
struct iso_tree_node_dir *root;
struct iso_tree_node_file *file;
printf("Testing iso_tree_add_file()...");
root = iso_tree_new_root();
file = (struct iso_tree_node_file *)
iso_tree_add_file(root, "/tmp/libisofs_test/README");
assert(root);
assert(file);
assert(file->node.parent == root);
assert(root->nchildren == 1);
assert(root->children[0] == (struct iso_tree_node *)file);
assert( strcmp(file->node.name, "README") == 0 );
assert( strcmp(file->path, "/tmp/libisofs_test/README") == 0 );
assert( S_ISREG(file->node.attrib.st_mode) );
printf("\tOK\n");
}
static void test_add_symlink() {
struct iso_tree_node_dir *root;
struct iso_tree_node *lnk;
printf("Testing iso_tree_add_symlink()...");
root = iso_tree_new_root();
lnk = iso_tree_add_symlink(root, "read", "/tmp/libisofs_test/README");
assert(root);
assert(lnk);
assert(lnk->parent == root);
assert(root->nchildren == 1);
assert(root->children[0] == (struct iso_tree_node *)lnk);
assert( strcmp(lnk->name, "read") == 0 );
assert( strcmp( ((struct iso_tree_node_symlink*)lnk)->dest,
"/tmp/libisofs_test/README") == 0 );
assert( S_ISLNK(lnk->attrib.st_mode) );
printf("\tOK\n");
}
static void test_radd_dir() {
struct iso_tree_node_dir *root;
struct iso_tree_node_dir *dir;
struct iso_tree_node_dir *child;
struct iso_tree_radd_dir_behavior behavior = {0,0,0};
printf("Testing iso_tree_radd_dir()...");
root = iso_tree_new_root();
assert(root);
dir = iso_tree_radd_dir(root, "/tmp/libisofs_test", &behavior);
assert(dir);
assert(dir->node.parent == root);
assert(root->nchildren == 1);
assert(root->children[0] == (struct iso_tree_node *)dir);
assert( S_ISDIR(dir->node.attrib.st_mode) );
assert(dir->nchildren == 5);
assert( strcmp(dir->node.name, "libisofs_test") == 0 );
/* test _root_ children */
child = (struct iso_tree_node_dir *)dir->children[0];
assert( S_ISDIR(child->node.attrib.st_mode) );
assert( child->nchildren == 2);
assert( strcmp(child->node.name, "dir1") == 0 );
child = (struct iso_tree_node_dir *)dir->children[1];
assert( S_ISDIR(child->node.attrib.st_mode) );
assert( child->nchildren == 0);
assert( strcmp(child->node.name, "dir2") == 0 );
//TODO write really full test
printf("\tOK\n");
//iso_tree_print( (struct iso_tree_node *)root, 4 );
}
void test_tree()
{
test_new_root();
test_add_dir();
test_add_file();
test_add_symlink();
test_radd_dir();
}
/*
* Unit test for tree.h
*/
#include "libisofs.h"
#include "tree.h"
#include "test.h"
#include <assert.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
static void test_new_root() {
struct iso_tree_node_dir *root;
printf("Testing iso_tree_new_root()...");
root = iso_tree_new_root();
assert(root);
assert(root->nchildren == 0);
assert(root->children == NULL);
assert(root->node.parent == NULL);
assert(root->node.name == NULL);
assert( S_ISDIR(root->node.attrib.st_mode) );
printf("\tOK\n");
}
static void test_add_dir() {
struct iso_tree_node_dir *root;
struct iso_tree_node_dir *dir;
printf("Testing iso_tree_add_dir()...");
root = iso_tree_new_root();
dir = iso_tree_add_dir(root, "New dir name");
assert(root);
assert(dir);
assert(dir->node.parent == root);
assert(root->nchildren == 1);
assert(root->children[0] == (struct iso_tree_node *)dir);
assert( strcmp(dir->node.name, "New dir name") == 0 );
assert( S_ISDIR(dir->node.attrib.st_mode) );
printf("\tOK\n");
}
static void test_add_file() {
struct iso_tree_node_dir *root;
struct iso_tree_node_file *file;
printf("Testing iso_tree_add_file()...");
root = iso_tree_new_root();
file = (struct iso_tree_node_file *)
iso_tree_add_file(root, "/tmp/libisofs_test/README");
assert(root);
assert(file);
assert(file->node.parent == root);
assert(root->nchildren == 1);
assert(root->children[0] == (struct iso_tree_node *)file);
assert( strcmp(file->node.name, "README") == 0 );
assert( strcmp(file->path, "/tmp/libisofs_test/README") == 0 );
assert( S_ISREG(file->node.attrib.st_mode) );
printf("\tOK\n");
}
static void test_add_symlink() {
struct iso_tree_node_dir *root;
struct iso_tree_node *lnk;
printf("Testing iso_tree_add_symlink()...");
root = iso_tree_new_root();
lnk = iso_tree_add_symlink(root, "read", "/tmp/libisofs_test/README");
assert(root);
assert(lnk);
assert(lnk->parent == root);
assert(root->nchildren == 1);
assert(root->children[0] == (struct iso_tree_node *)lnk);
assert( strcmp(lnk->name, "read") == 0 );
assert( strcmp( ((struct iso_tree_node_symlink*)lnk)->dest,
"/tmp/libisofs_test/README") == 0 );
assert( S_ISLNK(lnk->attrib.st_mode) );
printf("\tOK\n");
}
static void test_radd_dir() {
struct iso_tree_node_dir *root;
struct iso_tree_node_dir *dir;
struct iso_tree_node_dir *child;
struct iso_tree_radd_dir_behavior behavior = {0,0,0};
printf("Testing iso_tree_radd_dir()...");
root = iso_tree_new_root();
assert(root);
dir = iso_tree_radd_dir(root, "/tmp/libisofs_test", &behavior);
assert(dir);
assert(dir->node.parent == root);
assert(root->nchildren == 1);
assert(root->children[0] == (struct iso_tree_node *)dir);
assert( S_ISDIR(dir->node.attrib.st_mode) );
assert(dir->nchildren == 5);
assert( strcmp(dir->node.name, "libisofs_test") == 0 );
/* test _root_ children */
child = (struct iso_tree_node_dir *)dir->children[0];
assert( S_ISDIR(child->node.attrib.st_mode) );
assert( child->nchildren == 2);
assert( strcmp(child->node.name, "dir1") == 0 );
child = (struct iso_tree_node_dir *)dir->children[1];
assert( S_ISDIR(child->node.attrib.st_mode) );
assert( child->nchildren == 0);
assert( strcmp(child->node.name, "dir2") == 0 );
//TODO write really full test
printf("\tOK\n");
//iso_tree_print( (struct iso_tree_node *)root, 4 );
}
void test_tree()
{
test_new_root();
test_add_dir();
test_add_file();
test_add_symlink();
test_radd_dir();
}