summaryrefslogtreecommitdiff
path: root/dviware/dvisvgm/tests/SplittedCharInputBufferTest.cpp
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /dviware/dvisvgm/tests/SplittedCharInputBufferTest.cpp
Initial commit
Diffstat (limited to 'dviware/dvisvgm/tests/SplittedCharInputBufferTest.cpp')
-rw-r--r--dviware/dvisvgm/tests/SplittedCharInputBufferTest.cpp179
1 files changed, 179 insertions, 0 deletions
diff --git a/dviware/dvisvgm/tests/SplittedCharInputBufferTest.cpp b/dviware/dvisvgm/tests/SplittedCharInputBufferTest.cpp
new file mode 100644
index 0000000000..c29a33c15a
--- /dev/null
+++ b/dviware/dvisvgm/tests/SplittedCharInputBufferTest.cpp
@@ -0,0 +1,179 @@
+/*************************************************************************
+** SplittedCharInputBufferTest.cpp **
+** **
+** This file is part of dvisvgm -- a fast DVI to SVG converter **
+** Copyright (C) 2005-2019 Martin Gieseking <martin.gieseking@uos.de> **
+** **
+** 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 3 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, see <http://www.gnu.org/licenses/>. **
+*************************************************************************/
+
+#include <gtest/gtest.h>
+#include <cstring>
+#include <sstream>
+#include "InputBuffer.hpp"
+#include "InputReader.hpp"
+
+using std::istringstream;
+
+TEST(SplittedCharInputBufferTest, get) {
+ const char *buf1 = "abcdef";
+ const char *buf2 = "ghijklmnopqrstuvwxyz";
+ SplittedCharInputBuffer buffer(buf1, strlen(buf1), buf2, strlen(buf2));
+ BufferInputReader in(buffer);
+ bool ok=true;
+ for (int i=0; !in.eof() && ok; i++) {
+ EXPECT_LT(i, 26);
+ EXPECT_EQ(in.get(), 'a'+i);
+ ok = (i < 26);
+ }
+}
+
+
+TEST(SplittedCharInputBufferTest, peek) {
+ const char *buf1 = "abcdef";
+ const char *buf2 = "ghijklmnopqrstuvwxyz";
+ SplittedCharInputBuffer buffer(buf1, strlen(buf1), buf2, strlen(buf2));
+ BufferInputReader in(buffer);
+ EXPECT_EQ(in.peek(), 'a');
+ for (int i=0; i < 26; i++)
+ EXPECT_EQ(in.peek(i), 'a'+i);
+ EXPECT_EQ(in.peek(26), -1);
+}
+
+
+TEST(SplittedCharInputBufferTest, check) {
+ const char *buf1 = "abcde";
+ const char *buf2 = "fghijklmnopqrstuvwxyz";
+ SplittedCharInputBuffer buffer(buf1, strlen(buf1), buf2, strlen(buf2));
+ BufferInputReader in(buffer);
+ EXPECT_TRUE(in.check("abc", false));
+ EXPECT_TRUE(in.check("abc", true));
+ EXPECT_TRUE(in.check("def", true));
+ EXPECT_TRUE(in.check("ghi", true));
+ EXPECT_TRUE(in.check("jkl", true));
+ EXPECT_TRUE(in.check("mnopqrst", false));
+ EXPECT_TRUE(in.check("mnopqrst", true));
+ EXPECT_TRUE(in.check("uvwxyz", true));
+ EXPECT_EQ(in.get(), -1);
+}
+
+
+TEST(SplittedCharInputBufferTest, skip) {
+ const char *buf1 = "abcde";
+ const char *buf2 = "fghijklmnopqrstuvwxyz";
+ SplittedCharInputBuffer buffer(buf1, strlen(buf1), buf2, strlen(buf2));
+ BufferInputReader in(buffer);
+ in.skip(3);
+ EXPECT_EQ(in.peek(), 'd');
+ in.skipUntil("ijk");
+ EXPECT_EQ(in.peek(), 'l');
+ in.skipUntil("z");
+ EXPECT_TRUE(in.eof());
+}
+
+
+TEST(SplittedCharInputBufferTest, parseInt) {
+ const char *buf1 = "1234,-";
+ const char *buf2 = "5,+6,10.-";
+ SplittedCharInputBuffer buffer(buf1, strlen(buf1), buf2, strlen(buf2));
+ BufferInputReader in(buffer);
+ int n;
+ EXPECT_TRUE(in.parseInt(n));
+ EXPECT_EQ(n, 1234);
+ EXPECT_EQ(in.get(), ',');
+
+ EXPECT_TRUE(in.parseInt(n));
+ EXPECT_EQ(n, -5);
+ EXPECT_EQ(in.get(), ',');
+
+ EXPECT_TRUE(in.parseInt(n));
+ EXPECT_EQ(n, 6);
+ EXPECT_EQ(in.get(), ',');
+
+ EXPECT_TRUE(in.parseInt(n));
+ EXPECT_EQ(n, 10);
+ EXPECT_EQ(in.get(), '.');
+
+ EXPECT_FALSE(in.parseInt(n));
+ EXPECT_EQ(in.get(), '-');
+}
+
+
+TEST(SplittedCharInputBufferTest, parseUInt_base) {
+ const char *buf1 = "1234,-5,10,1a";
+ const char *buf2 = "bc,1234a";
+ SplittedCharInputBuffer buffer(buf1, strlen(buf1), buf2, strlen(buf2));
+ BufferInputReader in(buffer);
+ unsigned n;
+ EXPECT_TRUE(in.parseUInt(10, n));
+ EXPECT_EQ(n, 1234u);
+ EXPECT_EQ(in.get(), ',');
+
+ EXPECT_FALSE(in.parseUInt(10, n));
+ in.get();
+ EXPECT_TRUE(in.parseUInt(10, n));
+ EXPECT_EQ(n, 5u);
+ EXPECT_EQ(in.get(), ',');
+
+ EXPECT_TRUE(in.parseUInt(16, n));
+ EXPECT_EQ(n, 16u);
+ EXPECT_EQ(in.get(), ',');
+
+ EXPECT_TRUE(in.parseUInt(16, n));
+ EXPECT_EQ(n, 0x1ABCu);
+ EXPECT_EQ(in.get(), ',');
+
+ EXPECT_TRUE(in.parseUInt(8, n));
+ EXPECT_EQ(n, 01234u);
+ EXPECT_EQ(in.get(), 'a');
+}
+
+
+TEST(SplittedCharInputBufferTest, parseDouble) {
+ const char *buf1 = "1234,-5,6.12,-3";
+ const char *buf2 = ".1415,-.1,12e2,10.-";
+ SplittedCharInputBuffer buffer(buf1, strlen(buf1), buf2, strlen(buf2));
+ BufferInputReader in(buffer);
+ double d;
+ EXPECT_EQ(in.parseDouble(d), 'i');
+ EXPECT_EQ(d, 1234.0);
+ EXPECT_EQ(in.get(), ',');
+
+ EXPECT_EQ(in.parseDouble(d), 'i');
+ EXPECT_EQ(d, -5.0);
+ EXPECT_EQ(in.get(), ',');
+
+ EXPECT_EQ(in.parseDouble(d), 'f');
+ EXPECT_EQ(d, 6.12);
+ EXPECT_EQ(in.get(), ',');
+
+ EXPECT_EQ(in.parseDouble(d), 'f');
+ EXPECT_EQ(d, -3.1415);
+ EXPECT_EQ(in.get(), ',');
+
+ EXPECT_EQ(in.parseDouble(d), 'f');
+ EXPECT_EQ(d, -0.1);
+ EXPECT_EQ(in.get(), ',');
+
+ EXPECT_EQ(in.parseDouble(d), 'f');
+ EXPECT_EQ(d, 1200);
+ EXPECT_EQ(in.get(), ',');
+
+ EXPECT_EQ(in.parseDouble(d), 'f');
+ EXPECT_EQ(d, 10.0);
+ EXPECT_EQ(in.peek(), '-');
+
+ EXPECT_FALSE(in.parseDouble(d));
+ EXPECT_EQ(in.get(), '-');
+}