test_solparser5.c (4406B)
1 /* Copyright (C) 2018-2026 |Meso|Star> (contact@meso-star.com) 2 * Copyright (C) 2016-2018 CNRS 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 16 17 #include "solparser.h" 18 #include "solparser_sun.h" 19 #include "test_solstice_utils.h" 20 21 static const char* input[] = { 22 "- sun:\n", 23 " dni: 1\n", 24 " spectrum: [{wavelength: 1, data: 1}]\n", 25 "- geometry: &cuboid\n", 26 " - cuboid: { size: [1, 2, 3] }\n", 27 " material: { matte: { reflectivity: 1 } }\n", 28 "- template: &template\n", 29 " name: lvl1\n", 30 " primary: 1\n", 31 " geometry: *cuboid\n", 32 " anchors:\n", 33 " - name: anchor0\n", 34 " position: [1, 2, 3]\n", 35 " children:\n", 36 " - name: lvl2\n", 37 " x_pivot:\n", 38 " ref_point: [1, 2, 3]\n", 39 " target: { anchor: self.lvl1.anchor0 }\n", 40 "- entity:\n", 41 " name: entity0\n", 42 " children: [ *template ]\n", 43 "- entity:\n", 44 " name: entity1\n", 45 " children: [ *template ]\n", 46 NULL 47 }; 48 49 static void 50 check_entity(struct solparser* parser, const struct solparser_entity* entity) 51 { 52 const struct solparser_anchor* anchor; 53 const struct solparser_x_pivot* x_pivot; 54 struct solparser_entity_id entity_id; 55 double tmp[3]; 56 57 CHK(entity->type == SOLPARSER_ENTITY_EMPTY); 58 59 CHK(solparser_entity_get_children_count(entity) == 1); 60 entity_id = solparser_entity_get_child(entity, 0); 61 entity = solparser_get_entity(parser, entity_id); 62 CHK(strcmp(str_cget(&entity->name), "lvl1") == 0); 63 CHK(entity->type == SOLPARSER_ENTITY_GEOMETRY); 64 65 CHK(solparser_entity_get_children_count(entity) == 1); 66 entity_id = solparser_entity_get_child(entity, 0); 67 entity = solparser_get_entity(parser, entity_id); 68 CHK(strcmp(str_cget(&entity->name), "lvl2") == 0); 69 CHK(entity->type == SOLPARSER_ENTITY_X_PIVOT); 70 71 x_pivot = solparser_get_x_pivot(parser, entity->data.x_pivot); 72 CHK(d3_eq(x_pivot->ref_point, d3(tmp, 1, 2, 3)) == 1); 73 CHK(x_pivot->target.type == SOLPARSER_TARGET_ANCHOR); 74 75 anchor = solparser_get_anchor(parser, x_pivot->target.data.anchor); 76 CHK(strcmp(str_cget(&anchor->name), "anchor0") == 0); 77 CHK(d3_eq(anchor->position, d3(tmp, 1, 2, 3)) == 1); 78 } 79 80 int 81 main(int argc, char** argv) 82 { 83 struct mem_allocator allocator; 84 struct solparser* parser; 85 const struct solparser_entity* entity; 86 struct solparser_entity_id entity_id; 87 struct solparser_entity_iterator it, it_end; 88 size_t i; 89 int entity0 = 0; 90 int entity1 = 0; 91 FILE* stream; 92 (void)argc, (void)argv; 93 94 CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); 95 solparser_create(&allocator, &parser); 96 97 stream = tmpfile(); 98 CHK(stream != NULL); 99 i = 0; 100 while(input[i]) { 101 const size_t len = strlen(input[i]); 102 CHK(fwrite(input[i], 1, len, stream) == len); 103 ++i; 104 } 105 rewind(stream); 106 107 CHK(solparser_setup(parser, NULL, stream) == RES_OK); 108 CHK(solparser_load(parser) == RES_OK); 109 110 solparser_entity_iterator_begin(parser, &it); 111 solparser_entity_iterator_end(parser, &it_end); 112 CHK(solparser_entity_iterator_eq(&it, &it_end) == 0); 113 114 while(!solparser_entity_iterator_eq(&it, &it_end)) { 115 entity_id = solparser_entity_iterator_get(&it); 116 entity = solparser_get_entity(parser, entity_id); 117 if(!strcmp(str_cget(&entity->name), "entity0")) { 118 CHK(entity0 == 0); 119 entity0 = 1; 120 check_entity(parser, entity); 121 } else if(!strcmp(str_cget(&entity->name), "entity1")) { 122 CHK(entity1 == 0); 123 entity1 = 1; 124 check_entity(parser, entity); 125 } else { 126 FATAL("Unexpected entity name.\n"); 127 } 128 solparser_entity_iterator_next(&it); 129 } 130 CHK(entity0 == 1); 131 CHK(entity1 == 1); 132 133 solparser_ref_put(parser); 134 fclose(stream); 135 136 check_memory_allocator(&allocator); 137 mem_shutdown_proxy_allocator(&allocator); 138 CHK(mem_allocated_size() == 0); 139 return 0; 140 } 141