Skip to content

Commit

Permalink
added tests + commenting debug
Browse files Browse the repository at this point in the history
  • Loading branch information
jarthod committed Sep 19, 2015
1 parent c197a4e commit f624ffe
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
bisect
benchmark
benchmark
test/*.out
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ bisect: bisect.c
clean:
rm -f bisect

re: clean all
re: clean all

test: all
test/run.sh
22 changes: 15 additions & 7 deletions bisect.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const char* bisect_start(const char* data, const size_t data_size, const char *p
const int line_size = find_line_size(line_start, data_size - mid);
// compare line with pattern
const int compare = match(line_start, line_size, pattern);
printf("bisect_start(%p, %ld, %s) mid: %ld, line: %.50s (%d)\n", data, data_size, pattern, mid, line_start, compare);
// printf("bisect_start(%p, %ld, %s) mid: %ld, line: %.50s (%d)\n", data, data_size, pattern, mid, line_start, compare);

if (compare > 0) { // line is smaller than pattern, search in next lines
return bisect_start(line_start + line_size + 1, data_size - mid - line_size -1, pattern);
Expand All @@ -84,7 +84,7 @@ const char* bisect_end(const char* data, const size_t data_size, const char *pat
const int line_size = find_line_size(line_start, data_size - mid);
// compare line with pattern
const int compare = match(line_start, line_size, pattern);
printf("bisect_end(%p, %ld, %s) mid: %ld, line: %.50s (%d)\n", data, data_size, pattern, mid, line_start, compare);
// printf("bisect_end(%p, %ld, %s) mid: %ld, line: %.50s (%d)\n", data, data_size, pattern, mid, line_start, compare);

if (compare < 0) { // line is higher than pattern, search in previous lines
return bisect_end(data, mid, pattern);
Expand All @@ -105,7 +105,7 @@ int main(int argc, char **argv) {
const char *filename = argv[1];
const char *pattern = argv[2];
size_t file_size = get_file_size(filename);
printf("bisecting %s (%.3g MB)\n", filename, file_size / 1024. / 1024.);
// printf("bisecting %s (%.3g MB)\n", filename, file_size / 1024. / 1024.);
int fd = open(filename, O_RDONLY, 0);
if (fd == -1) {
fprintf(stderr, "Can't open file\n");
Expand All @@ -117,10 +117,18 @@ int main(int argc, char **argv) {
return 1;
}
const char *start = bisect_start(data, file_size, pattern);
printf("Starts at %ld: %.50s\n", start - data, start);
const char *end = bisect_end(start, file_size - (start - data), pattern);
printf("Ends at %ld: %.50s\n", end - data, end);
write(1, start, end - start);
if (start) {
// printf("Starts at %ld: %.50s\n", start - data, start);
const char *end = bisect_end(start, file_size - (start - data), pattern);
if (end) {
// printf("Ends at %ld: %.50s\n", end - data, end);
write(1, start, end - start);
} else {
write(1, "\n", 1);
}
} else {
write(1, "\n", 1);
}
munmap(data, file_size);
close(fd);
return 0;
Expand Down
10 changes: 10 additions & 0 deletions test/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
01\n03\n05\n09\n10\n11\n12\n13\n16\n19\n20\n25,,01\n03\n05\n09\n10\n11\n12\n13\n16\n19\n20\n25
01\n03\n05\n09\n10\n11\n12\n13\n16\n19\n20\n25,1,10\n11\n12\n13\n16\n19
01\n03\n05\n09\n10\n11\n12\n13\n16\n19\n20\n25,0,01\n03\n05\n09
01\n03\n05\n09\n10\n11\n12\n13\n16\n19\n20\n25,2,20\n25
01\n03\n05\n09\n10\n11\n12\n13\n16\n19\n20\n25,1.,10\n11\n12\n13\n16\n19
01\n03\n05\n09\n10\n11\n12\n13\n16\n19\n20\n25,12,12
01\n03\n05\n09\n10\n12\n12\n12\n16\n19\n20\n25,12,12\n12\n12
1\n,3,
1\n,0,
q01\nw03\ne05\nr09\nt10\ny11\nu12\ni13\no16\np19\na20\ns25,.1,t10\ny11\nu12\ni13\no16\np19
19 changes: 19 additions & 0 deletions test/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

i=1
while IFS=, read -r input pattern expect
do
echo -n "test $i: "
echo $input | sed 's/\\n/\n/g' > test/input
echo $expect | sed 's/\\n/\n/g' > test/expected
./bisect test/input $pattern > test/got
res=`diff -u test/expected test/got`
if [ $? == 0 ]; then
echo -e "\e[92m[ OK ]\e[0m"
else
echo -e "\e[91m[ FAIL ]\e[0m pattern: '$pattern' \ninput:\n$input\ndiff:\n$res"
fi
i=$((i+1))
done < test/data.txt

rm -f test/input test/expected test/got

0 comments on commit f624ffe

Please sign in to comment.