Add tests for vector operations

This commit is contained in:
hasufell 2014-05-10 23:38:37 +02:00
parent 85b8bc0cbf
commit 8dd46fc2f6
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
4 changed files with 508 additions and 1 deletions

View File

@ -16,7 +16,7 @@ endif
TARGET = test
HEADERS = cunit.h
OBJECTS = cunit.o cunit_filereader.o cunit_half_edge.o
OBJECTS = cunit.o cunit_filereader.o cunit_half_edge.o cunit_vector.o
INCS = -I. -I..
CFLAGS += $(shell $(PKG_CONFIG) --cflags gl glu glib-2.0)

View File

@ -110,6 +110,73 @@ int main(void)
return CU_get_error();
}
/* add a suite to the registry */
pSuite = CU_add_suite("vector tests",
init_suite,
clean_suite);
if (NULL == pSuite) {
CU_cleanup_registry();
return CU_get_error();
}
/* add the tests to the suite */
if (
(NULL == CU_add_test(pSuite, "test1 calculating vector product",
test_vector_product1)) ||
(NULL == CU_add_test(pSuite, "test2 calculating vector product",
test_vector_product2)) ||
(NULL == CU_add_test(pSuite, "test3 calculating vector product",
test_vector_product3)) ||
(NULL == CU_add_test(pSuite, "test4 calculating vector product",
test_vector_product4)) ||
(NULL == CU_add_test(pSuite, "test5 calculating vector product",
test_vector_product5)) ||
(NULL == CU_add_test(pSuite, "test6 calculating vector product",
test_vector_product6)) ||
(NULL == CU_add_test(pSuite, "test1 adding vectors",
test_add_vectors1)) ||
(NULL == CU_add_test(pSuite, "test2 adding vectors",
test_add_vectors2)) ||
(NULL == CU_add_test(pSuite, "test3 adding vectors",
test_add_vectors3)) ||
(NULL == CU_add_test(pSuite, "test4 adding vectors",
test_add_vectors4)) ||
(NULL == CU_add_test(pSuite, "test5 adding vectors",
test_add_vectors5)) ||
(NULL == CU_add_test(pSuite, "test6 adding vectors",
test_add_vectors6)) ||
(NULL == CU_add_test(pSuite, "test1 substracting vectors",
test_sub_vectors1)) ||
(NULL == CU_add_test(pSuite, "test2 substracting vectors",
test_sub_vectors2)) ||
(NULL == CU_add_test(pSuite, "test3 substracting vectors",
test_sub_vectors3)) ||
(NULL == CU_add_test(pSuite, "test4 substracting vectors",
test_sub_vectors4)) ||
(NULL == CU_add_test(pSuite, "test5 substracting vectors",
test_sub_vectors5)) ||
(NULL == CU_add_test(pSuite, "test6 substracting vectors",
test_sub_vectors6)) ||
(NULL == CU_add_test(pSuite, "test1 normalizing vector",
test_normalize_vector1)) ||
(NULL == CU_add_test(pSuite, "test2 normalizing vector",
test_normalize_vector2)) ||
(NULL == CU_add_test(pSuite, "test3 normalizing vector",
test_normalize_vector3)) ||
(NULL == CU_add_test(pSuite, "test4 normalizing vector",
test_normalize_vector4)) ||
(NULL == CU_add_test(pSuite, "test1 copying vector",
test_copy_vector1)) ||
(NULL == CU_add_test(pSuite, "test2 copying vector",
test_copy_vector2)) ||
(NULL == CU_add_test(pSuite, "test3 copying vector",
test_copy_vector3))
) {
CU_cleanup_registry();
return CU_get_error();
}
/* save stderr stream and close it */
my_stderr = dup(STDERR_FILENO);
close(STDERR_FILENO);

View File

@ -41,3 +41,36 @@ void test_find_center2(void);
void test_get_normalized_scale_factor1(void);
void test_get_normalized_scale_factor2(void);
/*
* vector tests
*/
void test_vector_product1(void);
void test_vector_product2(void);
void test_vector_product3(void);
void test_vector_product4(void);
void test_vector_product5(void);
void test_vector_product6(void);
void test_add_vectors1(void);
void test_add_vectors2(void);
void test_add_vectors3(void);
void test_add_vectors4(void);
void test_add_vectors5(void);
void test_add_vectors6(void);
void test_sub_vectors1(void);
void test_sub_vectors2(void);
void test_sub_vectors3(void);
void test_sub_vectors4(void);
void test_sub_vectors5(void);
void test_sub_vectors6(void);
void test_normalize_vector1(void);
void test_normalize_vector2(void);
void test_normalize_vector3(void);
void test_normalize_vector4(void);
void test_copy_vector1(void);
void test_copy_vector2(void);
void test_copy_vector3(void);

407
src/test/cunit_vector.c Normal file
View File

@ -0,0 +1,407 @@
/*
* Copyright 2011-2014 hasufell
*
* This file is part of a hasufell project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 2 of the License only.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "vector.h"
#include <CUnit/Basic.h>
#include <CUnit/Console.h>
#include <CUnit/Automated.h>
#include <stdbool.h>
#include <stdlib.h>
/**
* Test calculating vector product.
*/
void test_vector_product1(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 };
vector c;
bool retval = vector_product(&a, &b, &c);
CU_ASSERT_EQUAL(retval, true);
CU_ASSERT_EQUAL(c.x, 35);
CU_ASSERT_EQUAL(c.y, 8);
CU_ASSERT_EQUAL(c.z, -25);
}
/**
* Test calculating vector product, aliasing-safe.
*/
void test_vector_product2(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 };
bool retval = vector_product(&a, &b, &a);
CU_ASSERT_EQUAL(retval, true);
CU_ASSERT_EQUAL(a.x, 35);
CU_ASSERT_EQUAL(a.y, 8);
CU_ASSERT_EQUAL(a.z, -25);
}
/**
* Test calculating vector product, aliasing-safe.
*/
void test_vector_product3(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 };
bool retval = vector_product(&a, &b, &b);
CU_ASSERT_EQUAL(retval, true);
CU_ASSERT_EQUAL(b.x, 35);
CU_ASSERT_EQUAL(b.y, 8);
CU_ASSERT_EQUAL(b.z, -25);
}
/**
* Test error handling by passing a NULL pointer.
*/
void test_vector_product4(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 },
c;
bool retval = vector_product(&a, &b, NULL);
CU_ASSERT_EQUAL(retval, false);
}
/**
* Test error handling by passing a NULL pointer.
*/
void test_vector_product5(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 },
c;
bool retval = vector_product(NULL, &b, &c);
CU_ASSERT_EQUAL(retval, false);
}
/**
* Test error handling by passing a NULL pointer.
*/
void test_vector_product6(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 },
c;
bool retval = vector_product(&a, NULL, &c);
CU_ASSERT_EQUAL(retval, false);
}
/**
* Test adding vectors.
*/
void test_add_vectors1(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 },
c;
bool retval = add_vectors(&a, &b, &c);
CU_ASSERT_EQUAL(retval, true);
CU_ASSERT_EQUAL(c.x, 6);
CU_ASSERT_EQUAL(c.y, 5);
CU_ASSERT_EQUAL(c.z, 10);
}
/**
* Test adding vectors, aliasing-safe.
*/
void test_add_vectors2(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 };
bool retval = add_vectors(&a, &b, &a);
CU_ASSERT_EQUAL(retval, true);
CU_ASSERT_EQUAL(a.x, 6);
CU_ASSERT_EQUAL(a.y, 5);
CU_ASSERT_EQUAL(a.z, 10);
}
/**
* Test adding vectors, aliasing-safe.
*/
void test_add_vectors3(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 };
bool retval = add_vectors(&a, &b, &b);
CU_ASSERT_EQUAL(retval, true);
CU_ASSERT_EQUAL(b.x, 6);
CU_ASSERT_EQUAL(b.y, 5);
CU_ASSERT_EQUAL(b.z, 10);
}
/**
* Test error handling by passing a NULL pointer.
*/
void test_add_vectors4(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 },
c;
bool retval = add_vectors(&a, NULL, &c);
CU_ASSERT_EQUAL(retval, false);
}
/**
* Test error handling by passing a NULL pointer.
*/
void test_add_vectors5(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 },
c;
bool retval = add_vectors(&a, &b, NULL);
CU_ASSERT_EQUAL(retval, false);
}
/**
* Test error handling by passing a NULL pointer.
*/
void test_add_vectors6(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 },
c;
bool retval = add_vectors(NULL, &b, &c);
CU_ASSERT_EQUAL(retval, false);
}
/**
* Test substracting vectors.
*/
void test_sub_vectors1(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 },
c;
bool retval = sub_vectors(&a, &b, &c);
CU_ASSERT_EQUAL(retval, true);
CU_ASSERT_EQUAL(c.x, -4);
CU_ASSERT_EQUAL(c.y, 5);
CU_ASSERT_EQUAL(c.z, -4);
}
/**
* Test substracting vectors, aliasing-safe.
*/
void test_sub_vectors2(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 };
bool retval = sub_vectors(&a, &b, &a);
CU_ASSERT_EQUAL(retval, true);
CU_ASSERT_EQUAL(a.x, -4);
CU_ASSERT_EQUAL(a.y, 5);
CU_ASSERT_EQUAL(a.z, -4);
}
/**
* Test substracting vectors, aliasing-safe.
*/
void test_sub_vectors3(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 };
bool retval = sub_vectors(&a, &b, &b);
CU_ASSERT_EQUAL(retval, true);
CU_ASSERT_EQUAL(b.x, -4);
CU_ASSERT_EQUAL(b.y, 5);
CU_ASSERT_EQUAL(b.z, -4);
}
/**
* Test error handling by passing a NULL pointer.
*/
void test_sub_vectors4(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 },
c;
bool retval = sub_vectors(&a, NULL, &c);
CU_ASSERT_EQUAL(retval, false);
}
/**
* Test error handling by passing a NULL pointer.
*/
void test_sub_vectors5(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 },
c;
bool retval = sub_vectors(&a, &b, NULL);
CU_ASSERT_EQUAL(retval, false);
}
/**
* Test error handling by passing a NULL pointer.
*/
void test_sub_vectors6(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 },
c;
bool retval = sub_vectors(NULL, &b, &c);
CU_ASSERT_EQUAL(retval, false);
}
/**
* Test normalizing a vector.
*/
void test_normalize_vector1(void)
{
vector a = { 24, 0, 0 },
b;
bool retval = normalize_vector(&a, &b);
CU_ASSERT_EQUAL(retval, true);
CU_ASSERT_EQUAL(b.x, 1.0f);
CU_ASSERT_EQUAL(b.y, 0.0f);
CU_ASSERT_EQUAL(b.z, 0.0f);
}
/**
* Test normalizing a vector, aliasing-safe.
*/
void test_normalize_vector2(void)
{
vector a = { 24, 0, 0 };
bool retval = normalize_vector(&a, &a);
CU_ASSERT_EQUAL(retval, true);
CU_ASSERT_EQUAL(a.x, 1.0f);
CU_ASSERT_EQUAL(a.y, 0.0f);
CU_ASSERT_EQUAL(a.z, 0.0f);
}
/**
* Test error handling by passing a NULL pointer.
*/
void test_normalize_vector3(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 };
bool retval = normalize_vector(&a, NULL);
CU_ASSERT_EQUAL(retval, false);
}
/**
* Test error handling by passing a NULL pointer.
*/
void test_normalize_vector4(void)
{
vector a = { 1, 5, 3 },
b = { 5, 0, 7 };
bool retval = normalize_vector(NULL, &b);
CU_ASSERT_EQUAL(retval, false);
}
/**
* Test copying vectors.
*/
void test_copy_vector1(void)
{
vector a = { 1, 5, 3 },
b;
bool retval = copy_vector(&a, &b);
CU_ASSERT_EQUAL(retval, true);
CU_ASSERT_EQUAL(b.x, 1);
CU_ASSERT_EQUAL(b.y, 5);
CU_ASSERT_EQUAL(b.z, 3);
}
/**
* Test error handling by passing a NULL pointer.
*/
void test_copy_vector2(void)
{
vector a = { 1, 5, 3 },
b;
bool retval = copy_vector(&a, NULL);
CU_ASSERT_EQUAL(retval, false);
}
/**
* Test error handling by passing a NULL pointer.
*/
void test_copy_vector3(void)
{
vector a = { 1, 5, 3 },
b;
bool retval = copy_vector(NULL, &b);
CU_ASSERT_EQUAL(retval, false);
}