diff --git a/src/tests/.gitignore b/src/tests/.gitignore index 94ab5cd82..17a026577 100644 --- a/src/tests/.gitignore +++ b/src/tests/.gitignore @@ -3,4 +3,4 @@ t.benchmark.t tdb.t date.t duration.t - +text.t diff --git a/src/tests/Makefile b/src/tests/Makefile index b98b58bd4..8df7ab92f 100644 --- a/src/tests/Makefile +++ b/src/tests/Makefile @@ -1,4 +1,4 @@ -PROJECT = t.t tdb.t date.t duration.t t.benchmark.t +PROJECT = t.t tdb.t date.t duration.t t.benchmark.t text.t CFLAGS = -I. -I.. -Wall -pedantic -ggdb3 -fno-rtti LFLAGS = -L/usr/local/lib OBJECTS = ../TDB.o ../T.o ../parse.o ../text.o ../Date.o ../util.o ../Config.o @@ -32,3 +32,6 @@ duration.t: duration.t.o $(OBJECTS) test.o t.benchmark.t: t.benchmark.t.o $(OBJECTS) test.o g++ t.benchmark.t.o $(OBJECTS) test.o $(LFLAGS) -o t.benchmark.t +text.t: text.t.o $(OBJECTS) test.o + g++ text.t.o $(OBJECTS) test.o $(LFLAGS) -o text.t + diff --git a/src/tests/text.t.cpp b/src/tests/text.t.cpp new file mode 100644 index 000000000..65c3d5e11 --- /dev/null +++ b/src/tests/text.t.cpp @@ -0,0 +1,216 @@ +//////////////////////////////////////////////////////////////////////////////// +// task - a command line task list manager. +// +// Copyright 2006 - 2009, Paul Beckingham. +// All rights reserved. +// +// This program is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// +#include +#include "task.h" +#include "test.h" + +//////////////////////////////////////////////////////////////////////////////// +int main (int argc, char** argv) +{ + UnitTest t (78); + + // void wrapText (std::vector & lines, const std::string& text, const int width) + std::string text = "This is a test of the line wrapping code."; + std::vector lines; + wrapText (lines, text, 10); + t.is (lines.size (), (size_t) 5, "wrapText 'This is a test of the line wrapping code.' -> total 5 lines"); + t.is (lines[0], "This is a", "wrapText line 0 -> 'This is a'"); + t.is (lines[1], "test of", "wrapText line 1 -> 'test of'"); + t.is (lines[2], "the line", "wrapText line 2 -> 'the line'"); + t.is (lines[3], "wrapping", "wrapText line 3 -> 'wrapping'"); + t.is (lines[4], "code.", "wrapText line 4 -> 'code.'"); + +#ifdef NOPE + // void wrapText (std::vector & lines, const std::string& text, const int width) + text = "This ☺ is a test of utf8 line extraction."; + lines.clear (); + wrapText (lines, text, 7); + t.is (lines.size (), (size_t) 7, "wrapText 'This ☺ is a test of utf8 line extraction.' -> total 7 lines"); + t.is (lines[0], "This ☺", "wrapText line 0 -> 'This ☺'"); + t.is (lines[1], "is a", "wrapText line 1 -> 'is a'"); + t.is (lines[2], "test of", "wrapText line 2 -> 'test of'"); + t.is (lines[3], "utf8", "wrapText line 3 -> 'utf8'"); + t.is (lines[4], "line", "wrapText line 4 -> 'line'"); + t.is (lines[5], "extrac-", "wrapText line 5 -> 'extrac-'"); + t.is (lines[6], "tion.", "wrapText line 6 -> 'tion.'"); + + // void extractLine (std::string& text, std::string& line, int length) + text = "This ☺ is a test of utf8 line extraction."; +#endif + std::string line; +#ifdef NOPE + extractLine (text, line, 7); + t.is (line, "line 1", "extractLine 7 'This ☺ is a test of utf8 line extraction.' -> 'This ☺'"); +#endif + + // void extractLine (std::string& text, std::string& line, int length) + text = "line 1\nlengthy second line that exceeds width"; + extractLine (text, line, 10); + t.is (line, "line 1", "extractLine 10 'line 1\\nlengthy second line that exceeds width' -> 'line 1'"); + + extractLine (text, line, 10); + t.is (line, "lengthy", "extractLine 10 'lengthy second line that exceeds width' -> 'lengthy'"); + + extractLine (text, line, 10); + t.is (line, "second", "extractLine 10 'second line that exceeds width' -> 'second'"); + + extractLine (text, line, 10); + t.is (line, "line that", "extractLine 10 'line that exceeds width' -> 'line that'"); + + extractLine (text, line, 10); + t.is (line, "exceeds", "extractLine 10 'exceeds width' -> 'exceeds'"); + + extractLine (text, line, 10); + t.is (line, "width", "extractLine 10 'width' -> 'width'"); + + extractLine (text, line, 10); + t.is (line, "", "extractLine 10 '' -> ''"); + + // void split (std::vector& results, const std::string& input, const char delimiter) + std::vector items; + std::string unsplit = ""; + split (items, unsplit, '-'); + t.is (items.size (), (size_t) 0, "split '' '-' -> 0 items"); + + unsplit = "a"; + split (items, unsplit, '-'); + t.is (items.size (), (size_t) 1, "split 'a' '-' -> 1 item"); + t.is (items[0], "a", "split 'a' '-' -> 'a'"); + + unsplit = "-"; + split (items, unsplit, '-'); + t.is (items.size (), (size_t) 2, "split '-' '-' -> '' ''"); + t.is (items[0], "", "split '-' '-' -> [0] ''"); + t.is (items[1], "", "split '-' '-' -> [1] ''"); + + unsplit = "-a-bc-def"; + split (items, unsplit, '-'); + t.is (items.size (), (size_t) 4, "split '-a-bc-def' '-' -> '' 'a' 'bc' 'def'"); + t.is (items[0], "", "split '-a-bc-def' '-' -> [0] ''"); + t.is (items[1], "a", "split '-a-bc-def' '-' -> [1] 'a'"); + t.is (items[2], "bc", "split '-a-bc-def' '-' -> [2] 'bc'"); + t.is (items[3], "def", "split '-a-bc-def' '-' -> [3] 'def'"); + + // void split (std::vector& results, const std::string& input, const std::string& delimiter) + unsplit = ""; + split (items, unsplit, "--"); + t.is (items.size (), (size_t) 0, "split '' '--' -> 0 items"); + + unsplit = "a"; + split (items, unsplit, "--"); + t.is (items.size (), (size_t) 1, "split 'a' '--' -> 1 item"); + t.is (items[0], "a", "split 'a' '-' -> 'a'"); + + unsplit = "--"; + split (items, unsplit, "--"); + t.is (items.size (), (size_t) 2, "split '-' '--' -> '' ''"); + t.is (items[0], "", "split '-' '-' -> [0] ''"); + t.is (items[1], "", "split '-' '-' -> [1] ''"); + + unsplit = "--a--bc--def"; + split (items, unsplit, "--"); + t.is (items.size (), (size_t) 4, "split '-a-bc-def' '--' -> '' 'a' 'bc' 'def'"); + t.is (items[0], "", "split '-a-bc-def' '--' -> [0] ''"); + t.is (items[1], "a", "split '-a-bc-def' '--' -> [1] 'a'"); + t.is (items[2], "bc", "split '-a-bc-def' '--' -> [2] 'bc'"); + t.is (items[3], "def", "split '-a-bc-def' '--' -> [3] 'def'"); + + // void join (std::string& result, const std::string& separator, const std::vector& items) + std::vector unjoined; + std::string joined; + + join (joined, "", unjoined); + t.is (joined.length (), (size_t) 0, "join -> length 0"); + t.is (joined, "", "join -> ''"); + + unjoined.push_back (""); + unjoined.push_back ("a"); + unjoined.push_back ("bc"); + unjoined.push_back ("def"); + join (joined, "", unjoined); + t.is (joined.length (), (size_t) 6, "join '' 'a' 'bc' 'def' -> length 6"); + t.is (joined, "abcdef", "join '' 'a' 'bc' 'def' -> 'abcdef'"); + + join (joined, "-", unjoined); + t.is (joined.length (), (size_t) 9, "join '' - 'a' - 'bc' - 'def' -> length 9"); + t.is (joined, "-a-bc-def", "join '' - 'a' - 'bc' - 'def' -> '-a-bc-def'"); + + // std::string trimLeft (const std::string& in, const std::string& t /*= " "*/) + t.is (trimLeft (""), "", "trimLeft '' -> ''"); + t.is (trimLeft ("", " \t"), "", "trimLeft '' -> ''"); + t.is (trimLeft ("xxx"), "xxx", "trimLeft 'xxx' -> 'xxx'"); + t.is (trimLeft ("xxx", " \t"), "xxx", "trimLeft 'xxx' -> 'xxx'"); + t.is (trimLeft (" \t xxx \t "), "\t xxx \t ", "trimLeft ' \\t xxx \\t ' -> '\\t xxx \\t '"); + t.is (trimLeft (" \t xxx \t ", " \t"), "xxx \t ", "trimLeft ' \\t xxx \\t ' -> 'xxx \\t '"); + + // std::string trimRight (const std::string& in, const std::string& t /*= " "*/) + t.is (trimRight (""), "", "trimRight '' -> ''"); + t.is (trimRight ("", " \t"), "", "trimRight '' -> ''"); + t.is (trimRight ("xxx"), "xxx", "trimRight 'xxx' -> 'xxx'"); + t.is (trimRight ("xxx", " \t"), "xxx", "trimRight 'xxx' -> 'xxx'"); + t.is (trimRight (" \t xxx \t "), " \t xxx \t", "trimRight ' \\t xxx \\t ' -> ' \\t xxx \\t'"); + t.is (trimRight (" \t xxx \t ", " \t"), " \t xxx", "trimRight ' \\t xxx \\t ' -> ' \\t xxx'"); + + // std::string trim (const std::string& in, const std::string& t /*= " "*/) + t.is (trim (""), "", "trim '' -> ''"); + t.is (trim ("", " \t"), "", "trim '' -> ''"); + t.is (trim ("xxx"), "xxx", "trim 'xxx' -> 'xxx'"); + t.is (trim ("xxx", " \t"), "xxx", "trim 'xxx' -> 'xxx'"); + t.is (trim (" \t xxx \t "), "\t xxx \t", "trim ' \\t xxx \\t ' -> '\\t xxx \\t'"); + t.is (trim (" \t xxx \t ", " \t"), "xxx", "trim ' \\t xxx \\t ' -> 'xxx'"); + + // std::string commify (const std::string& data) + t.is (commify (""), "", "commify '' -> ''"); + t.is (commify ("1"), "1", "commify '1' -> '1'"); + t.is (commify ("12"), "12", "commify '12' -> '12'"); + t.is (commify ("123"), "123", "commify '123' -> '123'"); + t.is (commify ("1234"), "1,234", "commify '1234' -> '1,234'"); + t.is (commify ("12345"), "12,345", "commify '12345' -> '12,345'"); + t.is (commify ("123456"), "123,456", "commify '123456' -> '123,456'"); + t.is (commify ("1234567"), "1,234,567", "commify '1234567' -> '1,234,567'"); + t.is (commify ("12345678"), "12,345,678", "commify '12345678' -> '12,345,678'"); + t.is (commify ("123456789"), "123,456,789", "commify '123456789' -> '123,456,789'"); + t.is (commify ("1234567890"), "1,234,567,890", "commify '1234567890' -> '1,234,567,890'"); + + t.is (commify ("pre"), "pre", "commify 'pre' -> 'pre'"); + t.is (commify ("pre1234"), "pre1,234", "commify 'pre1234' -> 'pre1,234'"); + t.is (commify ("1234post"), "1,234post", "commify '1234post' -> '1,234post'"); + t.is (commify ("pre1234post"), "pre1,234post", "commify 'pre1234post' -> 'pre1,234post'"); + + // std::string lowerCase (const std::string& input) + t.is (lowerCase (""), "", "lowerCase '' -> ''"); + t.is (lowerCase ("pre01_:POST"), "pre01_:post", "lowerCase 'pre01_:POST' -> 'pre01_:post'"); + + // std::string upperCase (const std::string& input) + t.is (upperCase (""), "", "upperCase '' -> ''"); + t.is (upperCase ("pre01_:POST"), "PRE01_:POST", "upperCase 'pre01_:POST' -> 'PRE01_:POST'"); + + return 0; +} + +//////////////////////////////////////////////////////////////////////////////// +