Renderer/test/c/fluid/islandsolver_tests.c
austin e6e469bec1
All checks were successful
studiorailgun/Renderer/pipeline/head This commit looks good
fluid work
2024-12-05 16:22:32 -05:00

143 lines
5.5 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include "fluid/chunk.h"
#include "fluid/chunkmask.h"
#include "fluid/utilities.h"
#include "fluid/islandsolver.h"
#include "../test.h"
#include "chunk_test_utils.h"
/**
* Creates a chunk at a world position
*/
Chunk * chunk_create(int x, int y, int z){
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));
chunk1->x = x;
chunk1->y = y;
chunk1->z = z;
return chunk1;
}
/**
* Frees a chunk
*/
void chunk_free(Chunk * chunk){
free(chunk->d[CENTER_LOC]);
free(chunk->d0[CENTER_LOC]);
free(chunk->u[CENTER_LOC]);
free(chunk->v[CENTER_LOC]);
free(chunk->w[CENTER_LOC]);
free(chunk->u0[CENTER_LOC]);
free(chunk->v0[CENTER_LOC]);
free(chunk->w0[CENTER_LOC]);
free(chunk);
}
/**
* Sets a chunk's value
* @param chunk The chunk
* @param i The value to set the array to
* @param arr THe array to set
*/
void chunk_set_val(Chunk * chunk, int i, int arr){
for(int x = 0; x < DIM; x++){
for(int y = 0; y < DIM; y++){
for(int z = 0; z < DIM; z++){
switch(arr){
case ARR_ID_D: {
chunk->d[CENTER_LOC][x * DIM * DIM + y * DIM + z] = i;
} break;
case ARR_ID_D0: {
chunk->d0[CENTER_LOC][x * DIM * DIM + y * DIM + z] = i;
} break;
case ARR_ID_U: {
chunk->u[CENTER_LOC][x * DIM * DIM + y * DIM + z] = i;
} break;
case ARR_ID_V: {
chunk->v[CENTER_LOC][x * DIM * DIM + y * DIM + z] = i;
} break;
case ARR_ID_W: {
chunk->w[CENTER_LOC][x * DIM * DIM + y * DIM + z] = i;
} break;
case ARR_ID_U0: {
chunk->u0[CENTER_LOC][x * DIM * DIM + y * DIM + z] = i;
} break;
case ARR_ID_V0: {
chunk->v0[CENTER_LOC][x * DIM * DIM + y * DIM + z] = i;
} break;
case ARR_ID_W0: {
chunk->w0[CENTER_LOC][x * DIM * DIM + y * DIM + z] = i;
} break;
}
}
}
}
}
int fluid_islandsolver_tests(int argc, char **argv){
int rVal = 0;
//allocate a sparse array
FluidIslandSolver * islandSolver = fluid_island_solver_create();
if(islandSolver == NULL){
printf("Failed to allocate islandSolver!\n");
return 1;
}
//create chunks to add to the sparse array
Chunk * chunk1 = chunk_create(0,0,0);
Chunk * chunk2 = chunk_create(1,0,0);
Chunk * chunk3 = chunk_create(7,0,0);
//test adding chunks
fluid_island_solver_add_chunk(islandSolver,chunk1);
fluid_island_solver_add_chunk(islandSolver,chunk2);
rVal += assertEqualsPtr((void *)islandSolver->remaining[0],(void *)chunk1,"Failed to insert chunk1 \n");
rVal += assertEqualsPtr((void *)islandSolver->remaining[1],(void *)chunk2,"Failed to insert chunk1 \n");
rVal += assertNotEqualsPtr((void *)islandSolver->remaining[1],(void *)chunk1,"Failed to insert chunk1 \n");
rVal += assertNotEqualsPtr((void *)islandSolver->remaining[0],(void *)chunk2,"Failed to insert chunk1 \n");
//test removing
fluid_island_solver_remove_chunk(islandSolver,chunk1);
rVal += assertEquals(fluid_island_solver_get_remaining(islandSolver),1,"remaining array should NOT be empty -- %d %d \n");
fluid_island_solver_remove_chunk(islandSolver,chunk2);
rVal += assertEquals(fluid_island_solver_get_remaining(islandSolver),0,"remaining array should be empty -- %d %d \n");
//solve an island
fluid_island_solver_add_chunk(islandSolver,chunk1);
fluid_island_solver_add_chunk(islandSolver,chunk2);
fluid_island_solver_add_chunk(islandSolver,chunk3);
//this should solve for the lone chunk 3 island
fluid_island_solver_solve_island(islandSolver);
rVal += assertEquals(fluid_sparse_array_get_chunk_count(islandSolver->sparseArray),1,"sparse array should contain one element %d %d \n");
//this should solve for the conjoined chunk1-chunk2 island
fluid_island_solver_solve_island(islandSolver);
rVal += assertEquals(fluid_sparse_array_get_chunk_count(islandSolver->sparseArray),2,"sparse array should contain two elements %d %d \n");
rVal += assertEquals(fluid_island_solver_get_remaining(islandSolver),0,"there should be no remaining chunks %d %d \n");
//should be able to grab the sparse array from here still
rVal += assertEqualsPtr(fluid_island_solver_get_sparse_array(islandSolver),islandSolver->sparseArray,"should be able to fetch the sparse array from the solver \n");
//free the test chunks
chunk_free(chunk1);
chunk_free(chunk2);
chunk_free(chunk3);
//free a sparse array
fluid_island_solver_free(islandSolver);
return rVal;
}