Add tests for chunk.c
Some checks failed
studiorailgun/Renderer/pipeline/head There was a failure building this commit

This commit is contained in:
austin 2024-12-04 20:20:39 -05:00
parent bbf14ffd87
commit 52c98beb7a
10 changed files with 134 additions and 37 deletions

View File

@ -949,7 +949,8 @@ WARN_LOGFILE = DoxygenWarningLog.txt
# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
# Note: If this tag is empty the current directory is searched.
INPUT = ../src \
INPUT = ../src/main/java \
../src/main/c/includes \
./src \
./src/mainpage.md

View File

@ -1243,6 +1243,7 @@ Small collision engine code formatting
(14/04/2024)
Fix testing apparatus for native code on windows
Fix doxygen also pointing at native code lib folder

View File

@ -53,7 +53,7 @@ typedef struct {
/**
* The size of the dimension of the memory of the sparse array
*/
#define SPARSE_ARRAY_RAW_DIM SPARSE_ARRAY_CHUNK_DIM * MAIN_ARRAY_DIM + SPARSE_ARRAY_BORDER_SIZE
#define SPARSE_ARRAY_RAW_DIM ((SPARSE_ARRAY_CHUNK_DIM * MAIN_ARRAY_DIM) + SPARSE_ARRAY_BORDER_SIZE)
/**
* The size of a sparse array in number of elements
@ -116,21 +116,26 @@ typedef struct {
/**
* Creates a sparse chunk array
*/
LIBRARY_API SparseChunkArray * fluid_create_sparse_array();
LIBRARY_API SparseChunkArray * fluid_sparse_array_create();
/**
* Frees a sparse chunk array
*/
LIBRARY_API void fluid_free_sparse_array(SparseChunkArray * array);
LIBRARY_API void fluid_sparse_array_free(SparseChunkArray * array);
/**
* Adds a chunk to the sparse array
*/
LIBRARY_API void fluid_add_chunk(SparseChunkArray * array, Chunk * chunk, int x, int y, int z);
LIBRARY_API void fluid_sparse_array_add_chunk(SparseChunkArray * array, Chunk * chunk, int x, int y, int z);
/**
* Adds a chunk to the sparse array
*/
LIBRARY_API void fluid_remove_chunk(SparseChunkArray * array, Chunk * chunk, int x, int y, int z, int copy);
LIBRARY_API void fluid_sparse_array_remove_chunk(SparseChunkArray * array, Chunk * chunk, int x, int y, int z, int copy);
/**
* Gets the index for a point in the chunk
*/
LIBRARY_API int fluid_sparse_array_get_index(SparseChunkArray * array, int x, int y, int z);
#endif

View File

@ -1,10 +1,12 @@
#include <stdint.h>
#include "chunk.h"
#ifndef UTILITIES_H
#define UTILITIES_H
#define SWAP(x0,x) {float *tmp=x0;x0=x;x=tmp;}
#define IX(i,j,k) ((i)+(N)*(j)+(N*N)*(k))
#define IX(i,j,k) ((i)+(DIM)*(j)+(DIM*DIM)*(k))
#define CK(m,n,o) ((m)+(n)*(3)+(o)*(3)*(3))
#define GET_ARR_RAW(src,i) src[i]
#define ARR_EXISTS(chunk_mask,m,n,o) (chunk_mask & CHUNK_INDEX_ARR[CK(m,n,o)]) > 0

View File

@ -14,7 +14,7 @@ int solveOffset(int chunkPos);
/**
* Creates a sparse chunk array
*/
LIBRARY_API SparseChunkArray * fluid_create_sparse_array(){
LIBRARY_API SparseChunkArray * fluid_sparse_array_create(){
//allocate the object itself
SparseChunkArray * rVal = (SparseChunkArray *)malloc(sizeof(SparseChunkArray));
@ -38,7 +38,7 @@ LIBRARY_API SparseChunkArray * fluid_create_sparse_array(){
/**
* Frees a sparse chunk array
*/
LIBRARY_API void fluid_free_sparse_array(SparseChunkArray * array){
LIBRARY_API void fluid_sparse_array_free(SparseChunkArray * array){
//free the constituent arrays
free(array->d);
@ -61,15 +61,18 @@ LIBRARY_API void fluid_free_sparse_array(SparseChunkArray * array){
/**
* Adds a chunk to the sparse array
*/
LIBRARY_API void fluid_add_chunk(SparseChunkArray * array, Chunk * chunk, int x, int y, int z){
LIBRARY_API void fluid_sparse_array_add_chunk(SparseChunkArray * array, Chunk * chunk, int x, int y, int z){
//solve chunk offsets
int offsetX = solveOffset(x);
int offsetY = solveOffset(y);
int offsetZ = solveOffset(z);
int minPos = SPARSE_ARRAY_BORDER_SIZE / 2;
int N = DIM;
int i, j, k;
if(array->chunks[GCI(x,y,z)] != NULL){
return;
}
for(int m = 0; m < MAIN_ARRAY_DIM; m++){
for(int n = 0; n < MAIN_ARRAY_DIM; n++){
for(int o = 0; o < MAIN_ARRAY_DIM; o++){
@ -129,13 +132,12 @@ LIBRARY_API void fluid_add_chunk(SparseChunkArray * array, Chunk * chunk, int x,
/**
* Adds a chunk to the sparse array
*/
LIBRARY_API void fluid_remove_chunk(SparseChunkArray * array, Chunk * chunk, int x, int y, int z, int copy){
LIBRARY_API void fluid_sparse_array_remove_chunk(SparseChunkArray * array, Chunk * chunk, int x, int y, int z, int copy){
//solve chunk offsets
int offsetX = solveOffset(x);
int offsetY = solveOffset(y);
int offsetZ = solveOffset(z);
int minPos = SPARSE_ARRAY_BORDER_SIZE / 2;
int N = DIM;
int i, j, k;
for(int m = 0; m < MAIN_ARRAY_DIM; m++){
@ -244,6 +246,13 @@ LIBRARY_API void fluid_remove_chunk(SparseChunkArray * array, Chunk * chunk, int
array->chunks[GCI(x,y,z)] = NULL;
}
/**
* Gets the index for a point in the chunk
*/
LIBRARY_API int fluid_sparse_array_get_index(SparseChunkArray * array, int x, int y, int z){
return GVI(x,y,z);
}
/**
* Gets the index of the chunk at a given position within the sparse array
*/

View File

@ -1,5 +0,0 @@
int TestAsdf(){
return 0;
}

View File

@ -1,19 +0,0 @@
#include <stdio.h>
#include "fluid/chunk.h"
int fluid_FluidMemoryManagementTests(int argc, char **argv){
//allocate a sparse array
SparseChunkArray * sparseArray = fluid_create_sparse_array();
//free a sparse array
fluid_free_sparse_array(sparseArray);
return 0;
}

View File

@ -0,0 +1,69 @@
#include <stdio.h>
#include <stdlib.h>
#include "fluid/chunk.h"
#include "fluid/chunkmask.h"
#include "fluid/utilities.h"
#include "../test.h"
int fluid_chunk_tests(int argc, char **argv){
int rVal = 0;
//allocate a sparse array
SparseChunkArray * sparseArray = fluid_sparse_array_create();
//create chunks to add to the sparse array
Chunk * chunk1 = (Chunk *)malloc(sizeof(Chunk));
chunk1->d[CENTER_LOC] = (float *)malloc(DIM * DIM * DIM * sizeof(float));
chunk1->d0[CENTER_LOC] = (float *)malloc(DIM * DIM * DIM * sizeof(float));
chunk1->u[CENTER_LOC] = (float *)malloc(DIM * DIM * DIM * sizeof(float));
chunk1->v[CENTER_LOC] = (float *)malloc(DIM * DIM * DIM * sizeof(float));
chunk1->w[CENTER_LOC] = (float *)malloc(DIM * DIM * DIM * sizeof(float));
chunk1->u0[CENTER_LOC] = (float *)malloc(DIM * DIM * DIM * sizeof(float));
chunk1->v0[CENTER_LOC] = (float *)malloc(DIM * DIM * DIM * sizeof(float));
chunk1->w0[CENTER_LOC] = (float *)malloc(DIM * DIM * DIM * sizeof(float));
for(int x = 0; x < DIM; x++){
for(int y = 0; y < DIM; y++){
for(int z = 0; z < DIM; z++){
chunk1->d[CENTER_LOC][x * DIM * DIM + y * DIM + z] = 1;
}
}
}
Chunk * chunk2 = (Chunk *)malloc(sizeof(Chunk));
chunk2->d[CENTER_LOC] = (float *)malloc(DIM * DIM * DIM * sizeof(float));
chunk2->d0[CENTER_LOC] = (float *)malloc(DIM * DIM * DIM * sizeof(float));
chunk2->u[CENTER_LOC] = (float *)malloc(DIM * DIM * DIM * sizeof(float));
chunk2->v[CENTER_LOC] = (float *)malloc(DIM * DIM * DIM * sizeof(float));
chunk2->w[CENTER_LOC] = (float *)malloc(DIM * DIM * DIM * sizeof(float));
chunk2->u0[CENTER_LOC] = (float *)malloc(DIM * DIM * DIM * sizeof(float));
chunk2->v0[CENTER_LOC] = (float *)malloc(DIM * DIM * DIM * sizeof(float));
chunk2->w0[CENTER_LOC] = (float *)malloc(DIM * DIM * DIM * sizeof(float));
for(int x = 0; x < DIM; x++){
for(int y = 0; y < DIM; y++){
for(int z = 0; z < DIM; z++){
chunk1->d[CENTER_LOC][x * DIM * DIM + y * DIM + z] = 2;
}
}
}
//add a chunk
fluid_sparse_array_add_chunk(sparseArray,chunk1,0,0,0);
rVal =+ assertEquals(sparseArray->d[fluid_sparse_array_get_index(sparseArray,0,0,0)],0,"index 0,0,0 in the sparse array should be a border value -- %d %d\n");
rVal =+ assertEquals(chunk1->d[CENTER_LOC][IX(1,1,1)],1,"chunk1 should have a value of 1 -- %d %d\n");
rVal =+ assertEquals(sparseArray->d[fluid_sparse_array_get_index(sparseArray,1,1,1)],chunk1->d[CENTER_LOC][IX(1,1,1)],"index 1,1,1 in the sparse array should be 0,0,0 of the chunk stored at 0,0,0 -- %d %d\n");
//make sure adding a second chunk doesn't override the first one
fluid_sparse_array_add_chunk(sparseArray,chunk2,1,0,0);
rVal =+ assertEquals(sparseArray->d[fluid_sparse_array_get_index(sparseArray,0,0,0)],0,"index 0,0,0 in the sparse array should be a border value -- %d %d\n");
rVal =+ assertEquals(sparseArray->d[fluid_sparse_array_get_index(sparseArray,1,1,1)],chunk1->d[CENTER_LOC][IX(1,1,1)],"index 1,1,1 in the sparse array should be 0,0,0 of the chunk stored at 0,0,0 -- %d %d\n");
rVal =+ assertEquals(sparseArray->d[fluid_sparse_array_get_index(sparseArray,16,16,16)],chunk1->d[CENTER_LOC][IX(15,15,15)],"index 16,16,16 in the sparse array should be 15,15,15 of the chunk stored at 0,0,0 -- %d %d\n");
rVal =+ assertEquals(sparseArray->d[fluid_sparse_array_get_index(sparseArray,17,17,17)],chunk2->d[CENTER_LOC][IX(1,1,1)],"index 17,17,17 in the sparse array should be 1,1,1 of the chunk stored at 1,0,0 -- %d %d\n");
//free a sparse array
fluid_sparse_array_free(sparseArray);
return rVal;
}

26
test/c/test.c Normal file
View File

@ -0,0 +1,26 @@
#include <stdlib.h>
#include <stdio.h>
#include "test.h"
int assertEquals(int a, int b, char * msg){
int rVal = (a != b);
if(rVal){
printf(msg, a, b);
}
return rVal;
}
int assertNotEquals(int a, int b, char * msg){
int rVal = (a == b);
if(rVal){
printf(msg, a, b);
}
return rVal;
}
int test(){
return 0;
}

8
test/c/test.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef TEST_H
#define TEST_H
int assertEquals(int a, int b, char * msg);
int assertNotEquals(int a, int b, char * msg);
#endif