filter: Update unit test

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2014-05-03 19:31:29 -04:00
parent 90bd93b10f
commit 70067a89cb
3 changed files with 78 additions and 141 deletions

View File

@ -14,7 +14,7 @@ for arg in sys.argv:
src = SConscript("src/Sconscript")
tests = [ "version" , "file", "database", "index" ]
tests = [ "version" , "file", "database", "index", "filter" ]
# "filter", "idle", "tag_db",
# "queue" ]
#scripts = [ "playlist", "library", "deck", "audio", "gui" ]

View File

@ -1,72 +0,0 @@
#!/bin/bash
# Copyright 2014 (c) Anna Schumaker
. $(dirname $0)/_functions
function test_add
{
test_equal "./src/filter.run -a $1" "$2"
}
function test_lowercase
{
test_equal "./src/filter.run -l $1" "$2"
}
function test_text
{
test_add "$1" "$2"
test_lowercase "$1" "$2"
}
function test_search
{
num=$(cat -b $DATA_DIR/filter.txt | tail -n 1 | awk '{print $1}')
let num=$num-1
test_equal "./src/filter.run -s $num $1" "$2"
}
new_test "Filter Add and Lowercase Test"
test_text " " ""
test_text " test
text" "test text"
test_text "test text" "test text"
test_text "Test Text" "test text"
test_text "Test? Text!" "test text"
test_text "Test?123 Text!456" "test123 text456"
test_text "Test?123 Text!456" "test123 text456"
test_text "Test(text);123-456" "test text 123 456"
test_text "Test((text));;123--456" "test text 123 456"
echo
new_test "Filter Search Test"
file=$DATA_DIR/filter.txt
mkdir -p $DATA_DIR
echo "0" > $file
echo "It's dangerous to go alone! Take this..." >> $file
echo "DODONGO DISLIKES SMOKE." >> $file
echo "I am Error." >> $file
echo "Error knows a secret." >> $file
echo "Hey, you pay, then you can open the chests!" >> $file
echo "And the Master Sword sleeps again... FOREVER!" >> $file
echo "Link checked the chest. Wow! This is a nice chest!" >> $file
echo "Hey! Listen! Hey! Listen! Watch out!" >> $file
echo "You killed the Deku Tree? How could you?!" >> $file
echo "You've met with a terrible fate, haven't you?" >> $file
echo "Believe in your strengths... Believe..." >> $file
echo "Tingle! Tingle! Kooloo-Limpah!" >> $file
echo "Well excuse me, Princess!" >> $file
test_search "error" "2 3"
test_search "the" "4 5 6 8"
test_search "the ch" "4 6"
test_search "the CH" "4 6"
test_search "the ch y" "4"
test_search "unknown terms" ""

View File

@ -3,96 +3,105 @@
* Test the filtering code
*/
#include <file.h>
#include <filter.h>
#include <print.h>
#include "test.h"
#include <string>
#include <stdlib.h>
#include <unistd.h>
enum action_t { ADD, LOWERCASE, SEARCH };
void add_text(const std::string &text)
static void do_test_lowercase(const std::string &text, const std::string &lc)
{
std::string lc = filter :: add(text, 0);
print("%s\n", lc.c_str());
test_equal(filter :: lowercase(text), lc);
}
void to_lowercase(const std::string &text)
static void test_lowercase()
{
std::string lc = filter :: lowercase(text);
print("%s\n", lc.c_str());
do_test_lowercase(" ", "");
do_test_lowercase(" test \
text", "test text");
do_test_lowercase("test text", "test text");
do_test_lowercase("Test Text", "test text");
do_test_lowercase("Test? Text!", "test text");
do_test_lowercase("Test?123 Text!456", "test123 text456");
do_test_lowercase("Test?123 Text!456", "test123 text456");
do_test_lowercase("Test(text);123-456", "test text 123 456");
do_test_lowercase("Test((text));;123--456", "test text 123 456");
}
void read_file(unsigned int n)
static void do_test_add(const std::string &text, const std::string &lc)
{
File f("filter.txt");
if (f.open(OPEN_READ)) {
for (unsigned int i = 0; i < n; i++) {
std::string text = f.getline();
filter :: add(text, i);
}
f.close();
}
static unsigned int i = 0;
test_equal(filter :: add(text, i++), lc);
}
void do_search(const std::string &text)
static void test_add()
{
do_test_add("It's dangerous to go alone! Take this...",
"its dangerous to go alone take this");
do_test_add("DODONGO DISLIKES SMOKE.",
"dodongo dislikes smoke");
do_test_add("I am Error.",
"i am error");
do_test_add("Error knows a secret.",
"error knows a secret");
do_test_add("Hey, you pay, then you can open the chests!",
"hey you pay then you can open the chests");
do_test_add("And the Master Sword sleeps again... FOREVER!",
"and the master sword sleeps again forever");
do_test_add("Link checked the chest. Wow! This is a nice chest!",
"link checked the chest wow this is a nice chest");
do_test_add("Hey! Listen! Hey! Listen! Watch out!",
"hey listen hey listen watch out");
do_test_add("You killed the Deku Tree? How could you?!",
"you killed the deku tree how could you");
do_test_add("You've met with a terrible fate, haven't you?",
"youve met with a terrible fate havent you");
do_test_add("Believe in your strengths... Believe...",
"believe in your strengths believe");
do_test_add("Tingle! Tingle! Kooloo-Limpah!",
"tingle tingle kooloo limpah");
do_test_add("Well excuse me, Princess!",
"well excuse me princess");
}
static void do_test_search(const std::string &text, unsigned int len,
unsigned int *ids)
{
std::set<unsigned int> res;
std::set<unsigned int>::iterator it;
filter :: search(text, res);
test_equal(res.size(), (size_t)len);
test :: begin();
it = res.begin();
if (it == res.end())
return;
for (unsigned int i = 0; i < len; i++) {
check_equal(*it, ids[i]);
it++;
}
test :: success();
}
print("%u", *it);
for (it++; it != res.end(); it++)
print(" %u", *it);
print("\n");
static void test_search()
{
unsigned int res1[] = {2, 3};
do_test_search("error", 2, res1);
unsigned int res2[] = {4, 5, 6, 8};
do_test_search("the", 4, res2);
unsigned int res3[] = {4, 6};
do_test_search("the ch", 2, res3);
do_test_search("the CH", 2, res3);
unsigned int res4[] = {4};
do_test_search("the ch y", 1, res4);
unsigned int res5[] = {};
do_test_search("unknown terms", 0, res5);
}
int main(int argc, char **argv)
{
char c;
unsigned int n;
action_t action = ADD;
while ((c = getopt(argc, argv, "als:")) != -1) {
switch (c) {
case 'a':
action = ADD;
break;
case 'l':
action = LOWERCASE;
break;
case 's':
action = SEARCH;
n = atoi(optarg);
break;
}
}
std::string text;
for (int i = optind; i < argc; i++) {
text += " ";
text += argv[i];
}
switch (action) {
case ADD:
add_text(text);
break;
case LOWERCASE:
to_lowercase(text);
break;
case SEARCH:
read_file(n);
do_search(text);
break;
}
run_test("Filter Lowercase Test", test_lowercase);
run_test("Filter Add Test", test_add);
run_test("Filter Search Test", test_search);
return 0;
}