Skip to content

Instantly share code, notes, and snippets.

@bhelx

bhelx/libtre_test.c

Created Dec 19, 2011
Embed
What would you like to do?
test of libtre fuzzy regex matcher
Compiled Regex
Created Default params
cost_ins -> 1
cost_del -> 1
cost_subst -> 1
max_cost -> 2147483647
max_ins -> 2147483647
max_del -> 2147483647
max_subst -> 2147483647
max_err -> 2147483647
Created match
Made match
===========
Match For: ns jsdn Helo assja
Start idx -> 8
End idx -> 12
cost -> 1
num_ins -> 0
num_del -> 1
num_subt -> 0
Created match
Made match
===========
Match For: ns jsdn Helllo assja
Start idx -> 8
End idx -> 14
cost -> 1
num_ins -> 1
num_del -> 0
num_subt -> 0
Created match
Made match
===========
Match For: ns jsdn Hexlo assja
Start idx -> 8
End idx -> 13
cost -> 1
num_ins -> 0
num_del -> 0
num_subt -> 1
/**
* Quick super hacky test of libtre
*
* to compile:
* gcc libtre_test.c -ltre -o libtre-test
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tre/tre.h>
void printMatch(regamatch_t *match);
void printParams(regaparams_t *params);
int main() {
regex_t *preg = (regex_t *) malloc(sizeof(regex_t));
int regex_compiled = tre_regcomp(preg, "Hello", 0);
printf((regex_compiled != 0) ? "Something fucked up\n" : "Compiled Regex\n");
regaparams_t params;
tre_regaparams_default(&params);
printParams(&params);
// test del cost
const char *str = "ns jsdn Helo assja"; // Missing an 'l' in this Hello
regamatch_t *match = (regamatch_t *) malloc(sizeof(regamatch_t));
match->nmatch = 1;
match->pmatch = (regmatch_t *) malloc(sizeof(regmatch_t));
printf("Created match \n");
int matched = tre_reganexec(preg, str, strlen(str), match, params, 0);
printf((matched != 0) ? "Something fucked up\n" : "Made match\n");
printf("===========\nMatch For: %s\n", str);
printMatch(match);
// test extra char cost
str = "ns jsdn Helllo assja"; // Extra 'l' in this Hello
match = (regamatch_t *) malloc(sizeof(regamatch_t));
match->nmatch = 1;
match->pmatch = (regmatch_t *) malloc(sizeof(regmatch_t));
printf("Created match \n");
matched = tre_reganexec(preg, str, strlen(str), match, params, 0);
printf((matched != 0) ? "Something fucked up\n" : "Made match\n");
printf("===========\nMatch For: %s\n", str);
printMatch(match);
// test extra char cost
str = "ns jsdn Hexlo assja"; // One 'l' is substituted for an 'x'
match = (regamatch_t *) malloc(sizeof(regamatch_t));
match->nmatch = 1;
match->pmatch = (regmatch_t *) malloc(sizeof(regmatch_t));
printf("Created match \n");
matched = tre_reganexec(preg, str, strlen(str), match, params, 0);
printf((matched != 0) ? "Something fucked up\n" : "Made match\n");
printf("===========\nMatch For: %s\n", str);
printMatch(match);
return 0;
}
void printMatch(regamatch_t *match) {
printf("Start idx -> %d\n", match->pmatch->rm_so);
printf("End idx -> %d\n", match->pmatch->rm_eo);
printf("cost -> %d\n", match->cost);
printf("num_ins -> %d\n", match->num_ins);
printf("num_del -> %d\n", match->num_del);
printf("num_subt -> %d\n", match->num_subst);
}
void printParams(regaparams_t *params) {
printf("Created Default params\n");
printf("cost_ins -> %d\n", params->cost_ins);
printf("cost_del -> %d\n", params->cost_del);
printf("cost_subst -> %d\n", params->cost_subst);
printf("max_cost -> %d\n", params->max_cost);
printf("max_ins -> %d\n", params->max_ins);
printf("max_del -> %d\n", params->max_del);
printf("max_subst -> %d\n", params->max_subst);
printf("max_err -> %d\n", params->max_err);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.