From 9b15bbecb3ff16cecabcef4ebec861c6ed4cfc61 Mon Sep 17 00:00:00 2001 From: angi Date: Wed, 11 Jul 2007 19:58:21 +0000 Subject: [PATCH] --- fastlib/u/angela/npoint/3pt.matcher | 6 +-- fastlib/u/angela/npoint/build.py | 10 ++++- fastlib/u/angela/npoint/globals.h | 3 +- fastlib/u/angela/npoint/main.cc | 27 ++++++++++- fastlib/u/angela/npoint/matcher.cc | 57 +++++++++++------------- fastlib/u/angela/npoint/metrics.h | 4 +- fastlib/u/angela/npoint/multi_matcher.cc | 4 +- fastlib/u/angela/npoint/multi_matcher.h | 6 +-- fastlib/u/angela/npoint/naive.cc | 18 +------- fastlib/u/angela/npoint/schema.text | 5 +++ fastlib/u/angela/npoint/tests.cc | 36 +++++++++++++++ fastlib/u/angela/npoint/tests.h | 19 ++++++++ 12 files changed, 135 insertions(+), 60 deletions(-) create mode 100644 fastlib/u/angela/npoint/tests.cc create mode 100644 fastlib/u/angela/npoint/tests.h diff --git a/fastlib/u/angela/npoint/3pt.matcher b/fastlib/u/angela/npoint/3pt.matcher index 3bf5fbd073..ca369ba491 100644 --- a/fastlib/u/angela/npoint/3pt.matcher +++ b/fastlib/u/angela/npoint/3pt.matcher @@ -1,6 +1,6 @@ 0, 0, 0 0, 0, 0 0, 0, 0 -0, 1, 3 -1, 0, 2 -3, 2, 0 +0, 10, 30 +10, 0, 20 +30, 20, 0 diff --git a/fastlib/u/angela/npoint/build.py b/fastlib/u/angela/npoint/build.py index 145fb2a0b4..71974412ed 100644 --- a/fastlib/u/angela/npoint/build.py +++ b/fastlib/u/angela/npoint/build.py @@ -33,9 +33,17 @@ librule( deplibs = ["fastlib:fastlib",":datapack",":metrics",":matcher"] ) +librule( + name = "unit_test", + sources = ["tests.cc"], + headers = ["tests.h"], + deplibs = + ["fastlib:fastlib",":datapack",":metrics",":matcher",":naive",":multi_matcher"] +) + binrule( name = "npoint", sources = ["main.cc"], headers = ["globals.h"], - deplibs = ["fastlib:fastlib",":datapack",":metrics",":matcher",":naive"] + deplibs = ["fastlib:fastlib",":datapack",":metrics",":matcher",":naive",":unit_test"] ) diff --git a/fastlib/u/angela/npoint/globals.h b/fastlib/u/angela/npoint/globals.h index 3ad764dfc4..a15b17ad06 100644 --- a/fastlib/u/angela/npoint/globals.h +++ b/fastlib/u/angela/npoint/globals.h @@ -13,9 +13,10 @@ #define RECURSE 0 #define LEAF_SIZE 20 -#define MAX_N 25 +#define MAX_N 10 extern FILE *output; extern int count_all_permutations; +extern char *unit_test; #endif diff --git a/fastlib/u/angela/npoint/main.cc b/fastlib/u/angela/npoint/main.cc index ae9dedadd7..0d5746a79d 100644 --- a/fastlib/u/angela/npoint/main.cc +++ b/fastlib/u/angela/npoint/main.cc @@ -14,6 +14,7 @@ * --n=[size of n-tuple (default 2)] * --nweights=[number of weights (default 0)] * --metric=[csv file containing the desired metric] + * --test=[test name] * etc. */ @@ -24,8 +25,10 @@ #include "matcher.h" #include "datapack.h" #include "naive.h" +#include "tests.h" /* Initialize any global variables here. */ +/* Note: Probably useless. */ FILE *output = stderr; int count_all_permutations = 0; @@ -37,6 +40,29 @@ int main(int argc, char *argv[]) { fx_init(argc,argv); + /* First we check if we want to run a test */ + const char *test = fx_param_str(NULL,"test","none"); + String tmp; + + tmp.Copy(test); + if ( tmp.CompareTo("none") ) { + if ( !tmp.CompareTo("permutations") ) { + const int n = fx_param_int(NULL,"n",2); + + if ( !PASSED(test_permutation_generator(n)) ) { + fprintf(output,"\n\nPermutation generator failed for n = %d.\n\n",n); + } + else { + fprintf(output,"\n\nPermutation generator worked for n = %d.\n\n",n); + } + } + + fx_done(); + exit(1); + } + tmp.Destruct(); + + /* After the tests are done get the parameters and run the actual program */ const char *data_file = fx_param_str_req(NULL,"data"); const char *matcher_file = fx_param_str_req(NULL,"matcher"); const char *metric_file = fx_param_str(NULL,"metric","default"); @@ -50,7 +76,6 @@ int main(int argc, char *argv[]) Matcher matcher; Metric metric; Vector count; - String tmp; int i; tmp.Copy(output_file); diff --git a/fastlib/u/angela/npoint/matcher.cc b/fastlib/u/angela/npoint/matcher.cc index d4a078945d..30a4eb4f91 100644 --- a/fastlib/u/angela/npoint/matcher.cc +++ b/fastlib/u/angela/npoint/matcher.cc @@ -215,6 +215,7 @@ success_t Matcher::AnyMatch(const DataPack data, const Vector index, const Metri } else { int ready = 0; + index_t i; success_t match = SUCCESS_PASS; Vector tau; tau.Init(n); @@ -223,15 +224,11 @@ success_t Matcher::AnyMatch(const DataPack data, const Vector index, const Metri fprintf(output,"Fatal error: could not generate the first permutation\n"); exit(1); } -/* - for (i=0;i (n-1) ) { + if (tau[top] > n-1) { + ok_so_far = 0; + tau[top] = -1; top -= 1; - if (top < 0) { - return SUCCESS_FAIL; - } + } + if (top < 0) { + return SUCCESS_FAIL; } - - for (i=0;i 0); + Vector tau; + double count = 0.0; + + tau.Init(n); + if ( !PASSED(generate_first_permutation(tau)) ) { + return SUCCESS_FAIL; + } + count += 1.0; + while ( PASSED(generate_next_permutation(tau)) ) { + count += 1.0; + } + + if ( count != math::Factorial(n) ) { + return SUCCESS_FAIL; + } + + return SUCCESS_PASS; +} diff --git a/fastlib/u/angela/npoint/tests.h b/fastlib/u/angela/npoint/tests.h new file mode 100644 index 0000000000..c0067486aa --- /dev/null +++ b/fastlib/u/angela/npoint/tests.h @@ -0,0 +1,19 @@ +/** + * @author: Angela Grigoroaia + * @file: tests.h + * + * @description: Simple unit tests for npoint. + */ + +#ifndef TESTS_H +#define TEST_H + +/** + * This can be used to test if we are properly generating all the possible + * permutations of size n. It counts all the permutations that we generate and + * checks if the final value is n!. + */ +success_t test_permutation_generator(int n); + + +#endif