Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/btree2: add kdtree test #4797

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions lib/btree2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ MODULE_TOPDIR = ../..

include $(MODULE_TOPDIR)/include/Make/Vars.make

MOD_OBJS := $(filter-out try.o,$(AUTO_OBJS))
MOD_OBJS := $(filter-out test.o,$(AUTO_OBJS))

LIB = BTREE2

Expand All @@ -11,12 +11,25 @@ include $(MODULE_TOPDIR)/include/Make/Doxygen.make

HEADERS := $(ARCH_INCDIR)/kdtree.h

default: headers
$(MAKE) lib

headers: $(HEADERS)

default: lib headers
$(MAKE) lib
@echo "==============TEST============="
ifeq ($(strip $(CROSS_COMPILING)),)
$(MAKE) test
endif

$(ARCH_INCDIR)/kdtree.h: kdtree.h
$(INSTALL_DATA) $< $@

test: $(OBJDIR)/test$(EXE)
$(call run_grass,$(OBJDIR)/test$(EXE))

# Test functions
$(OBJDIR)/test$(EXE): $(OBJDIR)/test.o $(GISDEP) $(BTREE2DEP)
$(call linker)

$(OBJDIR)/test$(EXE): LIBES = $(GISLIB) $(BTREE2LIB)

DOXNAME = btree2
66 changes: 66 additions & 0 deletions lib/btree2/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/****************************************************************************
*
* MODULE: kdtree test
* AUTHOR(S): 林永 ynkan
* PURPOSE: test the kdtree.c method for logical operations such as
* kdtree_create=>kdtree_insert=>kdtree_dnn=>kdtree_remove=>kdtree_destroy
* loop executionbalanced tree
* See https://github.com/OSGeo/grass/issues/4779
* COPYRIGHT: (C) 2024 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*
*****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "kdtree.h"

#define MAX_POINTS 5

void generate_random_points(double points[][2], int num_points)
{
for (int i = 0; i < num_points; i++) {
points[i][0] = (rand() % 2000) / 100.0;
points[i][1] = (rand() % 2000) / 100.0;
}
}

int main(void)
{

srand(time(NULL));

while (1) {
int num = MAX_POINTS;
double points[MAX_POINTS][2];
generate_random_points(points, num);

/* double target[2] = {(rand() % 2000) / 100.0, (rand() % 2000) /
* 100.0};*/

struct kdtree *kdt = kdtree_create(2, NULL);

for (int i = 0; i < num; i++) {
int result = kdtree_insert(kdt, points[i], i, 0);
printf("kdtree insert[uid:%d](%.2f, %.2f) :[%d][%s]\r\n", i,
points[i][0], points[i][1], result,
result ? "success" : "failure");
}

for (int i = 0; i < num; i++) {
int result = kdtree_remove(kdt, points[i], i);
printf("kdtree remove[uid:%d](%.2f, %.2f) :[%d][%s]\r\n", i,
points[i][0], points[i][1], result,
result ? "success" : "failure");
}

kdtree_destroy(kdt);
}

return 0;
}
Loading