Skip to content

Instantly share code, notes, and snippets.

@alphaville
Created January 25, 2013 03:30
Show Gist options
  • Select an option

  • Save alphaville/4631512 to your computer and use it in GitHub Desktop.

Select an option

Save alphaville/4631512 to your computer and use it in GitHub Desktop.
A prototype for Unit Tests in C. Works on Netbeans.
/*
* File: dataloader_test.c
* Author: imt
*
* Created on Jan 23, 2013, 11:12:09 AM
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <stdbool.h>
#include "io/dataloader.h"
const char* test_name = "Data Loader";
const char* test_failed = "%%TEST_FAILED%% time=0 testname=%s (%s) message=%s\n";
void inline fail(char* test, char* msg) {
printf(test_failed, test, test_name, msg);
}
static int ASSERT_COUNT = 0;
void inline myassert(bool condition, const char* test, char* msg) {
ASSERT_COUNT++;
if (!condition) {
fail(test, msg);
}
}
void test_dataloader_vector1() {
clock_t begin, end;
zreal time_spent;
zvect data;
int size = 0;
begin = clock();
int status = load_vector_data("huge_data", (zvect*) & data, &size);
myassert(0 == status, "test_dataloader_vector1", "Status not OK");
myassert(14400 == size, "test_dataloader_vector1", "Wrong size");
end = clock();
time_spent = (zreal) 1000 * (end - begin) / CLOCKS_PER_SEC;
myassert(time_spent < 5.0f, "test_dataloader_vector1", "x");
}
void test_dataloader_vector2() {
// What if the file does not exist...
zvect data;
int size = 0;
int status = load_vector_data("data-unknown.dat", & data, &size);
myassert(FILE_NOT_FOUND == status, "test_dataloader_vector2", "Wrong status code");
}
void test_dataloader() {
const char* test = "test_dataloader";
zvect data;
int size = 0;
load_vector_data("data", &data, &size);
myassert(data[0] == 1.0f, test, "wrong data @0");
myassert(data[1] == 2.0f, test, "wrong data @1");
myassert(data[2] == 3.0f, test, "wrong data @2");
myassert(data[3] == 4.0f, test, "wrong data @3");
myassert(data[4] == 10.0f, test, "wrong data @4");
myassert(data[5] == 20.0f, test, "wrong data @5");
myassert(data[6] == 3.01e4, test, "wrong data @6");
myassert(data[7] == 40.01f, test, "wrong data @7");
myassert(data[8] == 9.1f, test, "wrong data @8");
myassert(size == 9, "test_dataloader", "wrong size");
load_vector_data("data", &data, &size);
myassert(size == 9, "test_dataloader", "wrong size after reloading");
}
void test_matrix() {
const char* test = "test_matrix";
zvect data;
int nrows, ncols;
int i;
load_matrix_data("A_SQUARE.dat", &data, &nrows, &ncols);
myassert(nrows == 4, test, "nrows!=4");
myassert(ncols == 4, test, "ncols!=4");
for (i = 0; i < 16; i++) {
myassert(data[i] == 1.0f * (i + 1), test, "wrong data");
}
load_matrix_data("SINGLE_ROW.dat", &data, &nrows, &ncols);
myassert(nrows == 1, test, "SINGLE_ROW: nrows!=1");
myassert(ncols == 10, test, "SINGLE_ROW: ncols!=10");
myassert(data[0] == 1, test, "wrong data @0");
myassert(data[9] == 10, test, "wrong data @9");
load_matrix_data("SINGLE_ROW_1.dat", &data, &nrows, &ncols);
myassert(nrows == 1, test, "SINGLE_ROW: nrows!=1");
myassert(ncols == 10, test, "SINGLE_ROW: ncols!=10");
myassert(data[0] == 1, test, "wrong data @0");
myassert(data[9] == 10, test, "wrong data @9");
load_matrix_data("huge_data", &data, &nrows, &ncols);
myassert(ncols == 1, test, "huge_data: wrong columns count");
myassert(nrows == 14400, test, "huge_data: wrong rows count");
myassert(data[0] == 123.0f, test, "huge_data: wrong data @0");
myassert(data[98] == 69.1f, test, "huge_data: wrong data @98");
myassert(data[99] == 12.0f, test, "huge_data: wrong data @99");
myassert(data[100] == 22.0f, test, "huge_data: wrong data @100");
myassert(data[1377] == -999.2f, test, "huge_data: wrong data @1377");
}
void test_matrix_2() {
const char* test = "test_matrix_2";
zvect data;
int nrows, ncols;
int i;
load_matrix_data("A_SQUARE_1.dat", &data, &nrows, &ncols);
myassert(nrows == 4, test, "nrows!=4");
myassert(ncols == 4, test, "ncols!=4");
for (i = 0; i < 16; i++) {
myassert(data[i] == 1.0f * (i + 1), test, "wrong data");
}
load_matrix_data("A_SQUARE_2.dat", &data, &nrows, &ncols);
printf("nrows=%d\n", nrows);
myassert(nrows == 4, test, "nrows!=4");
myassert(ncols == 4, test, "ncols!=4");
for (i = 0; i < 16; i++) {
myassert(data[i] == 1.0f * (i + 1), test, "wrong data");
}
}
void test_matrix_3() {
const char* test = "test_matrix_2";
zvect data;
int nrows, ncols;
int i;
load_matrix_data("A_SQUARE_2.dat", &data, &nrows, &ncols);
myassert(nrows == 4, test, "nrows!=4");
myassert(ncols == 4, test, "ncols!=4");
for (i = 0; i < 16; i++) {
myassert(data[i] == 1.0f * (i + 1), test, "wrong data");
}
}
void init(const char* test_name) {
printf("%%SUITE_STARTING%% %s\n", test_name);
printf("%%SUITE_STARTED%%\n");
}
void complete() {
printf("Total Assertions : %d\n", ASSERT_COUNT);
printf("%%SUITE_FINISHED%% time=0\n");
}
void test_function(void(* funct)(void), const char* test_name) {
clock_t begin, end;
zreal time_spent;
printf("%%TEST_STARTED%% %s (dataloader_test)\n", test_name);
begin = clock();
funct();
end = clock();
time_spent = (zreal) (end - begin) / CLOCKS_PER_SEC;
printf("%%TEST_FINISHED%% time=%#.2f %s (dataloader_test) \n", time_spent, test_name);
}
int main(int argc, char** argv) {
init(test_name);
test_function(&test_dataloader_vector1, "V1");
test_function(&test_dataloader_vector2, "V2");
test_function(&test_dataloader, "DL");
test_function(&test_matrix, "M1");
test_function(&test_matrix_2, "M2");
test_function(&test_matrix_3, "M3");
complete();
return (EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment