diff --git a/tools/slice_array.h b/tools/slice_array.h index 8de9c4d55..4dde8143b 100644 --- a/tools/slice_array.h +++ b/tools/slice_array.h @@ -33,9 +33,20 @@ struct SliceArray const T* ptr; size_t length; + /// Initialize to zero length + SliceArray() : ptr(0), length(0) {} + + /// Initialize from pointer + length SliceArray(const T* _ptr, size_t _length) : ptr(_ptr), length(_length) {} + /// Initialize from null-terminated string + SliceArray(const char* str) + { + ptr = str; + length = strlen(str); + } + bool operator==(SliceArray &t) { return @@ -52,8 +63,7 @@ struct SliceArray } /** This allocates a copy of the data. Only use this for debugging - and error messages. - */ + and error messages. */ std::string toString() { return std::string(ptr,length); } }; diff --git a/tools/tests/.gitignore b/tools/tests/.gitignore new file mode 100644 index 000000000..814490404 --- /dev/null +++ b/tools/tests/.gitignore @@ -0,0 +1 @@ +*_test diff --git a/tools/tests/Makefile b/tools/tests/Makefile new file mode 100644 index 000000000..8791154bf --- /dev/null +++ b/tools/tests/Makefile @@ -0,0 +1,12 @@ +GCC=g++ + +all: strops_test slice_test + +slice_test: slice_test.cpp ../slice_array.h + $(GCC) $< -o $@ + +strops_test: strops_test.cpp ../stringops.h ../stringops.cpp + $(GCC) $< -o $@ ../stringops.cpp + +clean: + rm *_test diff --git a/tools/tests/output/slice_test.out b/tools/tests/output/slice_test.out new file mode 100644 index 000000000..7b054082b --- /dev/null +++ b/tools/tests/output/slice_test.out @@ -0,0 +1,6 @@ +hello, len=5 +001 +hell, len=4 +010 +01 +4 3 diff --git a/tools/tests/output/strops_test.out b/tools/tests/output/strops_test.out new file mode 100644 index 000000000..e69de29bb diff --git a/tools/tests/slice_test.cpp b/tools/tests/slice_test.cpp new file mode 100644 index 000000000..ff60d2c96 --- /dev/null +++ b/tools/tests/slice_test.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +#include + +#include "../slice_array.h" + +int main() +{ + SString s, t; + s = SString("hello"); + cout << s.toString() << ", len=" << s.length << endl; + cout << (s=="hel") << (s=="hell") << (s=="hello") << endl; + t = s; + + s = SString("othello"+2, 4); + cout << s.toString() << ", len=" << s.length << endl; + cout << (s=="hel") << (s=="hell") << (s=="hello") << endl; + + cout << (s==t) << (SString("hello")==t) << endl; + + const int arr[4] = {1,2,3,4}; + + IntArray ia(arr,4); + + cout << ia.length << " " << ia.ptr[2] << endl; + + return 0; +} diff --git a/tools/tests/strops_test.cpp b/tools/tests/strops_test.cpp new file mode 100644 index 000000000..8991f5623 --- /dev/null +++ b/tools/tests/strops_test.cpp @@ -0,0 +1,18 @@ +#include + +#include "../stringops.h" + +int main() +{ + assert(begins("abc", "a")); + assert(begins("abc", "ab")); + assert(begins("abc", "abc")); + assert(begins("abcd", "abc")); + + assert(!begins("abc", "b")); + assert(!begins("abc", "bc")); + assert(!begins("abc", "bcd")); + assert(!begins("abc", "abcd")); + + return 0; +} diff --git a/tools/tests/test.sh b/tools/tests/test.sh new file mode 100755 index 000000000..b1ca6f1a6 --- /dev/null +++ b/tools/tests/test.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +make || exit + +mkdir -p output + +PROGS=*_test + +for a in $PROGS; do + if [ -f "output/$a.out" ]; then + echo "Running $a:" + $a | diff output/$a.out - + else + echo "Creating $a.out" + $a > "output/$a.out" + git add "output/$a.out" + fi +done