|
| 1 | +/* |
| 2 | + * Copyright (c) Simverge Software LLC - All Rights Reserved |
| 3 | + */ |
| 4 | + |
| 5 | +#include <stdlib.h> |
| 6 | +#include <stdio.h> |
| 7 | +#include <assert.h> |
| 8 | + |
| 9 | +#include "greatest.h" |
| 10 | + |
| 11 | +#include <pdal/capi/Pipeline.h> |
| 12 | +#include <pdal/capi/PointView.h> |
| 13 | +#include <pdal/capi/PointViewCollection.h> |
| 14 | + |
| 15 | +/// A simple binary tree implementation for int values |
| 16 | +struct node |
| 17 | +{ |
| 18 | + int key; |
| 19 | + struct node *left; |
| 20 | + struct node *right; |
| 21 | +}; |
| 22 | + |
| 23 | +void dispose(struct node *n) |
| 24 | +{ |
| 25 | + if (n == NULL) |
| 26 | + { |
| 27 | + dispose(n->left); |
| 28 | + dispose(n->right); |
| 29 | + free(n); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +bool insert(int key, struct node **n) |
| 34 | +{ |
| 35 | + bool inserted = false; |
| 36 | + |
| 37 | + if (*n == NULL) |
| 38 | + { |
| 39 | + *n = (struct node *) malloc(sizeof(struct node)); |
| 40 | + (*n)->key = key; |
| 41 | + (*n)->left = NULL; |
| 42 | + (*n)->right = NULL; |
| 43 | + inserted = true; |
| 44 | + } |
| 45 | + else if (key < (*n)->key) |
| 46 | + { |
| 47 | + inserted = insert(key, &(*n)->left); |
| 48 | + } |
| 49 | + else if (key > (*n)->key) |
| 50 | + { |
| 51 | + inserted = insert(key, &(*n)->right); |
| 52 | + } |
| 53 | + |
| 54 | + return inserted; |
| 55 | +} |
| 56 | + |
| 57 | +SUITE(PointViewTest); |
| 58 | + |
| 59 | +static const int INVALID_POINT_VIEW_ID = 0; |
| 60 | +static PDALPipelinePtr gPipeline = NULL; |
| 61 | +static PDALPointViewCollectionPtr gPointViewCollection = NULL; |
| 62 | + |
| 63 | +static void setupPointViewTest(void *arg) |
| 64 | +{ |
| 65 | + FILE *file = fopen("@CMAKE_BINARY_DIR@//data/stats.json", "rb"); |
| 66 | + char *json = NULL; |
| 67 | + |
| 68 | + if (file) |
| 69 | + { |
| 70 | + fseek(file, 0, SEEK_END); |
| 71 | + long length = ftell(file); |
| 72 | + fseek(file, 0, SEEK_SET); |
| 73 | + char *json = malloc(length + 1); |
| 74 | + |
| 75 | + if (json) |
| 76 | + { |
| 77 | + fread(json, 1, length, file); |
| 78 | + json[length] = '\0'; |
| 79 | + |
| 80 | + gPipeline = PDALCreatePipeline(json); |
| 81 | + |
| 82 | + if (gPipeline && PDALExecutePipeline(gPipeline)) |
| 83 | + { |
| 84 | + gPointViewCollection = PDALGetPointViews(gPipeline); |
| 85 | + } |
| 86 | + |
| 87 | + free(json); |
| 88 | + } |
| 89 | + |
| 90 | + fclose(file); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +static void teardownPointViewTest(void *arg) |
| 95 | +{ |
| 96 | + PDALDisposePointViewCollection(gPointViewCollection); |
| 97 | + PDALDisposePipeline(gPipeline); |
| 98 | +} |
| 99 | + |
| 100 | +TEST testPDALGetPointViewId(void) |
| 101 | +{ |
| 102 | + ASSERT_EQ(INVALID_POINT_VIEW_ID, PDALGetPointViewId(NULL)); |
| 103 | + |
| 104 | + PDALResetPointViewCollection(gPointViewCollection); |
| 105 | + bool hasNext = PDALHasNextPointView(gPointViewCollection); |
| 106 | + ASSERT_FALSE(!hasNext); |
| 107 | + |
| 108 | + struct node *tree = NULL; |
| 109 | + |
| 110 | + while (hasNext) |
| 111 | + { |
| 112 | + PDALPointViewPtr view = PDALGetNextPointView(gPointViewCollection); |
| 113 | + ASSERT_FALSE(view == NULL); |
| 114 | + |
| 115 | + // Make sure all IDs are valid |
| 116 | + int id = PDALGetPointViewId(view); |
| 117 | + ASSERT_FALSE(id == INVALID_POINT_VIEW_ID); |
| 118 | + |
| 119 | + // Make sure that there are no duplicate IDs |
| 120 | + bool inserted = insert(id, &tree); |
| 121 | + ASSERT_FALSE(!inserted); |
| 122 | + |
| 123 | + hasNext = PDALHasNextPointView(gPointViewCollection); |
| 124 | + } |
| 125 | + |
| 126 | + dispose(tree); |
| 127 | + |
| 128 | + PASS(); |
| 129 | +} |
| 130 | + |
| 131 | +TEST testPDALGetPointViewSize(void) |
| 132 | +{ |
| 133 | + ASSERT_EQ(0, PDALGetPointViewSize(NULL)); |
| 134 | + |
| 135 | + PASS(); |
| 136 | +} |
| 137 | + |
| 138 | +TEST testPDALIsPointViewEmpty(void) |
| 139 | +{ |
| 140 | + ASSERT_FALSE(!PDALIsPointViewEmpty(NULL)); |
| 141 | + |
| 142 | + PASS(); |
| 143 | +} |
| 144 | + |
| 145 | +TEST testPDALClonePointView(void) |
| 146 | +{ |
| 147 | + PDALResetPointViewCollection(gPointViewCollection); |
| 148 | + |
| 149 | + |
| 150 | + PASS(); |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | +GREATEST_SUITE(PointViewTest) |
| 155 | +{ |
| 156 | + SET_SETUP(setupPointViewTest, NULL); |
| 157 | + SET_TEARDOWN(teardownPointViewTest, NULL); |
| 158 | + |
| 159 | + RUN_TEST(testPDALGetPointViewId); |
| 160 | + RUN_TEST(testPDALGetPointViewSize); |
| 161 | + RUN_TEST(testPDALIsPointViewEmpty); |
| 162 | + RUN_TEST(testPDALClonePointView); |
| 163 | + |
| 164 | + SET_SETUP(NULL, NULL); |
| 165 | + SET_TEARDOWN(NULL, NULL); |
| 166 | +} |
0 commit comments