Skip to content
Permalink
19d0aacb25
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
70 lines (58 sloc) 2.34 KB
#include <assert.h>
#include <string.h>
#include "keywordset.h"
#include <stdlib.h>
#include <stdio.h>
static void test_new(char *init, char *expect) {
struct keywordset *kws = keywordset_new(init);
char *s = keywordset_get(kws);
if (strcmp(s, expect))
fprintf(stderr, "FAIL: new from '%s' got '%s' expected '%s'\n", init, s, expect); \
free(s);
keywordset_free(kws);
}
static void test_update(struct keywordset *kws, char *update, char *expect) {
char *init = keywordset_get(kws);
keywordset_update(kws, update);
char *s = keywordset_get(kws);
if (strcmp(s, expect))
fprintf(stderr, "FAIL: update '%s' with '%s' got '%s' expected '%s'\n", init, update, s, expect);
free(s);
free(init);
}
static void test_add(struct keywordset *kws, char *add, char *expect) {
char *init = keywordset_get(kws);
keywordset_add(kws, add);
char *s = keywordset_get(kws);
if (strcmp(s, expect))
fprintf(stderr, "FAIL: add '%s' with '%s' got '%s' expected '%s'\n", init, add, s, expect);
free(s);
free(init);
}
int main() {
test_new(NULL, "");
test_new("", "");
test_new("h7 h8 h9 h1 h2 h3", "h1 h2 h3 h7 h8 h9");
test_new(" h7\th8 h9 h1 h2 h3 \n", "h1 h2 h3 h7 h8 h9");
struct keywordset *kws = keywordset_new("a b c");
test_update(kws, "d e f", "d e f");
test_update(kws, "+g +h +i", "d e f g h i");
test_update(kws, "-e -h", "d f g i");
test_update(kws, "-i +x", "d f g x");
test_update(kws, "-x +m +n -n x y +a", "a m y");
test_update(kws, "-x +x", "a m x y");
test_update(kws, "+z -z", "a m x y");
keywordset_purge(kws);
test_update(kws, "", "");
keywordset_purge(kws);
test_update(kws, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
keywordset_free(kws);
kws = keywordset_new("abcdef");
test_update(kws, "+ab", "ab abcdef");
test_update(kws, "+abcdefgh", "ab abcdef abcdefgh");
keywordset_free(kws);
kws = keywordset_new("x x x");
test_add(kws, "a b c", "a b c x");
test_add(kws, "-x +y", "+y -x a b c x");
keywordset_free(kws);
}