From e1a4194f4f0fac51d64a82d0dd910aab76fc98a4 Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Sat, 24 Oct 2026 01:40:04 +0800 Subject: split vector from ui.c Signed-off-by: Richard Qin --- lab04/tests/Makefile | 2 +- lab04/tests/test.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) (limited to 'lab04/tests') diff --git a/lab04/tests/Makefile b/lab04/tests/Makefile index 93e1d65..ef28bff 100644 --- a/lab04/tests/Makefile +++ b/lab04/tests/Makefile @@ -8,7 +8,7 @@ TARGET := test_runner all: $(TARGET) -$(TARGET): test.c ../container_of.h ../list.h ../vec.h ../polynomial.h ../expression.h ../stack.h ../matrix.h +$(TARGET): test.c ../container_of.h ../list.h ../vec.h ../polynomial.h ../expression.h ../stack.h ../matrix.h ../vector_math.h $(CC) $(CFLAGS) -I.. test.c $(LDLIBS) -o $@ run: $(TARGET) diff --git a/lab04/tests/test.c b/lab04/tests/test.c index b40e7b3..64c3947 100644 --- a/lab04/tests/test.c +++ b/lab04/tests/test.c @@ -7,6 +7,7 @@ #include "matrix.h" #include "polynomial.h" #include "vec.h" +#include "vector_math.h" #include #include @@ -523,6 +524,56 @@ static bool test_vec_mutation_edges(void) return true; } +static bool test_vector_math(void) +{ + struct vec a, b, result; + vec_init(&a, double); + vec_init(&b, double); + vec_init(&result, double); + double left[] = { 1.0, 2.0, 3.0 }; + double right[] = { 4.0, 5.0, 6.0 }; + CHECK(vec_append(&a, left, 3)); + CHECK(vec_append(&b, right, 3)); + CHECK(vector_add(&result, &a, &b)); + CHECK(result.size == 3); + CHECK(((double *)result.data)[0] == 5.0); + CHECK(((double *)result.data)[1] == 7.0); + CHECK(((double *)result.data)[2] == 9.0); + CHECK(vector_sub(&result, &a, &b)); + CHECK(((double *)result.data)[0] == -3.0); + CHECK(((double *)result.data)[1] == -3.0); + CHECK(((double *)result.data)[2] == -3.0); + double cosine = 0.0; + CHECK(vector_cosine(&a, &b, &cosine)); + CHECK(fabs(cosine - 0.9746318461970762) < 1e-12); + + CHECK(vector_add(&a, &a, &b)); + CHECK(((double *)a.data)[0] == 5.0); + CHECK(((double *)a.data)[2] == 9.0); + CHECK(vector_sub(&b, &a, &b)); + CHECK(((double *)b.data)[0] == 1.0); + CHECK(((double *)b.data)[2] == 3.0); + + double sentinel[] = { 8.0, 9.0 }; + CHECK(vec_append(&result, sentinel, 2)); + struct vec short_vector; + vec_init(&short_vector, double); + CHECK(vec_append(&short_vector, sentinel, 2)); + CHECK(!vector_add(&result, &a, &short_vector)); + CHECK(result.size == 5); + CHECK(((double *)result.data)[3] == 8.0); + CHECK(((double *)result.data)[4] == 9.0); + struct vec zero; + vec_init(&zero, double); + CHECK(!vector_cosine(&zero, &zero, &cosine)); + vec_destroy(&zero); + vec_destroy(&short_vector); + vec_destroy(&a); + vec_destroy(&b); + vec_destroy(&result); + return true; +} + static bool test_list_replace_and_move(void) { LIST_HEAD(head); @@ -1364,6 +1415,7 @@ int main(void) { "polynomial integral", test_polynomial_integral }, { "polynomial integral edge cases", test_polynomial_integral_edges }, { "vec mutation edges", test_vec_mutation_edges }, + { "vector math", test_vector_math }, { "list replace and move", test_list_replace_and_move }, { "polynomial copy and derivative edges", test_polynomial_copy_and_derivative_edges }, -- cgit v1.2.3