polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

test_resource.c (4035B)


      1 
      2 #include "resource.h"
      3 #include "entity.h"
      4 #include "model.h"
      5 #include "util.h"
      6 #include "procmesh.h"
      7 #include "debug.h"
      8 #include <assert.h>
      9 
     10 static void print_int_resources(struct resource_manager *m)
     11 {
     12     int *ints = (int*)m->resources;
     13 
     14     for (u32 i = 0; i < m->resource_count; i++) {
     15         printf("%d ", ints[i]);
     16     }
     17 
     18     printf("\n");
     19 }
     20 
     21 
     22 static void print_ids(struct resource_manager *m)
     23 {
     24     for (u32 i = 0; i < m->resource_count; i++) {
     25         print_id(&m->ids[i], 0);
     26         printf(" ");
     27     }
     28     printf("\n");
     29 }
     30 
     31 
     32 static void test_compact()
     33 {
     34     printf("test_compact\n");
     35     struct resource_manager r;
     36     struct resource_id ids[20];
     37     int *p;
     38     u32 i;
     39 
     40     init_resource_manager(&r, sizeof(int), 2, 6, "int");
     41 
     42     for (i = 0; i < (int)ARRAY_SIZE(ids); i++)
     43         init_id(&ids[i]);
     44 
     45     i = 0;
     46 
     47     p = new_resource(&r, &ids[i++]);
     48     assert(r.resource_count == 1); *p = 0;
     49 
     50     p = new_resource(&r, &ids[i++]); *p = 1;
     51     assert(r.resource_count == 2);
     52 
     53     p = new_resource(&r, &ids[i++]); *p = 2;
     54     p = new_resource(&r, &ids[i++]);
     55     *p = 3;
     56 
     57     assert(r.resource_count == 4);
     58     destroy_resource(&r, &ids[1]);
     59     assert(r.resource_count == 3);
     60 
     61     assert(*(int*)get_resource(&r, &ids[0]) == 0);
     62     assert(       get_resource(&r, &ids[1]) == NULL);
     63     assert(*(int*)get_resource(&r, &ids[2]) == 2);
     64 
     65     new_resource(&r, &ids[i++]);
     66     assert(r.resource_count == 4);
     67     assert(r.current_capacity >= 4);
     68 
     69     new_resource(&r, &ids[i++]);
     70     assert(r.resource_count == 5);
     71     assert(r.current_capacity >= 5);
     72 }
     73 
     74 static void test_int_resource_manager()
     75 {
     76     printf("test_int_resource_manager\n");
     77     struct resource_manager r;
     78     struct resource_id id, first_id;
     79     int *p;
     80     // 2 item case
     81     init_resource_manager(&r, sizeof(int), 1, 2, "int");
     82 
     83     init_id(&id);
     84     init_id(&first_id);
     85 
     86     p = new_resource(&r, &first_id);
     87     assert(p);
     88     *p = 42;
     89 
     90     assert(r.resource_count == 1);
     91     assert(first_id.generation == 1);
     92     assert(*(int*)get_resource(&r, &first_id) == 42);
     93 
     94     p = new_resource(&r, &id);
     95     assert(p);
     96     *p = 32;
     97     assert(r.resource_count == 2);
     98     /* assert(id.generation == 2); */
     99     assert(*(int*)get_resource(&r, &id) == 32);
    100     assert(*(int*)get_resource(&r, &first_id) == 42);
    101 
    102     destroy_resource_manager(&r);
    103 }
    104 
    105 static void test_entity_system()
    106 {
    107     printf("test_entity_system\n");
    108     u32 count;
    109     struct entity *ent, *ents;
    110     entity_id ent_id;
    111     entity_id *ids;
    112 
    113     init_node_manager();
    114     init_entity_system();
    115 
    116     init_id(&ent_id);
    117 
    118     ents = get_all_entities(&count, &ids);
    119     assert(count == 0);
    120 
    121     ent = new_entity(&ent_id);
    122     ents = get_all_entities(&count, &ids);
    123 
    124     assert(ent != NULL);
    125     assert(count == 1);
    126     assert(&ents[0] == ent);
    127 
    128     assert(ideq(&ids[0], &ent_id));
    129 
    130     destroy_entity_system();
    131 }
    132 
    133 void test_geometry()
    134 {
    135     printf("test_geometry\n");
    136     init_geometry_manager();
    137     init_model_manager();
    138 
    139     struct model *model;
    140     struct model_id player_model_id =
    141 	    get_model_by_name("pirate_officer", &model);
    142 
    143     struct geometry *geom = get_geometry(&model->geom_id);
    144     assert(geom);
    145 
    146     debug("pirate verts %d\n", geom->num_verts);
    147     /* assert(geom->num_verts == 2676); */
    148 
    149     struct model_id rock_model;
    150     init_id(&rock_model.id);
    151     struct model *pmodel  = new_model(&rock_model); assert(pmodel);
    152     struct geometry *pgeom = get_geometry(&pmodel->geom_id); assert(geom);
    153     proc_sphere(pgeom);
    154 
    155     struct model_id player_model_id2 = get_model_by_name("pirate_officer", NULL);
    156 
    157     assert(ideq(&player_model_id.id, &player_model_id2.id));
    158 
    159     pmodel = get_model(&player_model_id2);
    160     pgeom = get_geometry(&pmodel->geom_id);
    161 
    162     /* assert(geom->num_verts == 2676); */
    163 }
    164 
    165 void test_dynamic_model_manager()
    166 {
    167     printf("test_dynamic_model_manager\n");
    168     init_model_manager();
    169 }
    170 
    171 int main(int argc, char *argv[])
    172 {
    173     test_geometry();
    174     test_int_resource_manager();
    175     test_compact();
    176     test_dynamic_model_manager();
    177     test_entity_system();
    178 
    179     return 0;
    180 }