summaryrefslogtreecommitdiff
path: root/AK/Tests
diff options
context:
space:
mode:
authorRobin Burchell <robin+git@viroteck.net>2019-07-16 10:08:39 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-07-16 11:03:38 +0200
commit41d2c674d764314c2930bcf4bfcf2992bf6b5cb8 (patch)
treec26da13d12db7942425192dbee354993cc4c27b9 /AK/Tests
parentdf3e295ba6ee69f0b178c581f1d91f8c322c0c4d (diff)
downloadserenity-41d2c674d764314c2930bcf4bfcf2992bf6b5cb8.zip
AK: Add a new TestSuite.h from my own work, adapted to match the existing one a bit
This gives a few new features: * benchmarks * the ability to run individual testcases easily * timing of tests
Diffstat (limited to 'AK/Tests')
-rw-r--r--AK/Tests/Makefile20
-rw-r--r--AK/Tests/TestHashMap.cpp55
-rw-r--r--AK/Tests/TestHelpers.h68
-rw-r--r--AK/Tests/TestJSON.cpp10
-rw-r--r--AK/Tests/TestQueue.cpp20
-rw-r--r--AK/Tests/TestString.cpp53
-rw-r--r--AK/Tests/TestVector.cpp41
7 files changed, 138 insertions, 129 deletions
diff --git a/AK/Tests/Makefile b/AK/Tests/Makefile
index ecfc22750e..04acb7a270 100644
--- a/AK/Tests/Makefile
+++ b/AK/Tests/Makefile
@@ -4,20 +4,20 @@ all: $(PROGRAMS)
CXXFLAGS = -std=c++17 -Wall -Wextra
-TestString: TestString.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp TestHelpers.h
- $(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestString.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp
+TestString: TestString.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../TestSuite.h ../LogStream.cpp
+ $(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestString.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../LogStream.cpp
-TestQueue: TestQueue.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp TestHelpers.h
- $(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestQueue.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp
+TestQueue: TestQueue.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../TestSuite.h ../LogStream.cpp
+ $(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestQueue.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../LogStream.cpp
-TestVector: TestVector.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp TestHelpers.h
- $(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestVector.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp
+TestVector: TestVector.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../TestSuite.h ../LogStream.cpp
+ $(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestVector.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../LogStream.cpp
-TestHashMap: TestHashMap.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp TestHelpers.h
- $(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestHashMap.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp
+TestHashMap: TestHashMap.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../TestSuite.h ../LogStream.cpp
+ $(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestHashMap.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../LogStream.cpp
-TestJSON: TestJSON.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp TestHelpers.h ../JsonObject.cpp ../JsonValue.cpp ../JsonArray.cpp ../JsonParser.cpp
- $(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestJSON.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../JsonObject.cpp ../JsonValue.cpp ../JsonArray.cpp ../JsonParser.cpp
+TestJSON: TestJSON.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../TestSuite.h ../LogStream.cpp ../JsonObject.cpp ../JsonValue.cpp ../JsonArray.cpp ../JsonParser.cpp
+ $(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestJSON.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../LogStream.cpp ../JsonObject.cpp ../JsonValue.cpp ../JsonArray.cpp ../JsonParser.cpp
clean:
rm -f $(PROGRAMS)
diff --git a/AK/Tests/TestHashMap.cpp b/AK/Tests/TestHashMap.cpp
index 55455b1d16..06ada64459 100644
--- a/AK/Tests/TestHashMap.cpp
+++ b/AK/Tests/TestHashMap.cpp
@@ -1,27 +1,46 @@
-#include "TestHelpers.h"
+#include <AK/TestSuite.h>
#include <AK/AKString.h>
#include <AK/HashMap.h>
-typedef HashMap<int, int> IntIntMap;
-
-int main()
+TEST_CASE(construct)
{
+ typedef HashMap<int, int> IntIntMap;
EXPECT(IntIntMap().is_empty());
- EXPECT(IntIntMap().size() == 0);
+ EXPECT_EQ(IntIntMap().size(), 0);
+}
+TEST_CASE(populate)
+{
HashMap<int, String> number_to_string;
number_to_string.set(1, "One");
number_to_string.set(2, "Two");
number_to_string.set(3, "Three");
-
+
EXPECT_EQ(number_to_string.is_empty(), false);
EXPECT_EQ(number_to_string.size(), 3);
+}
+
+TEST_CASE(range_loop)
+{
+ HashMap<int, String> number_to_string;
+ number_to_string.set(1, "One");
+ number_to_string.set(2, "Two");
+ number_to_string.set(3, "Three");
int loop_counter = 0;
for (auto& it : number_to_string) {
- EXPECT(!it.value.is_null());
+ EXPECT_EQ(it.value.is_null(), false);
++loop_counter;
}
+ EXPECT_EQ(loop_counter, 3);
+}
+
+TEST_CASE(map_remove)
+{
+ HashMap<int, String> number_to_string;
+ number_to_string.set(1, "One");
+ number_to_string.set(2, "Two");
+ number_to_string.set(3, "Three");
number_to_string.remove(1);
EXPECT_EQ(number_to_string.size(), 2);
@@ -30,16 +49,16 @@ int main()
number_to_string.remove(3);
EXPECT_EQ(number_to_string.size(), 1);
EXPECT(number_to_string.find(3) == number_to_string.end());
+ EXPECT(number_to_string.find(2) != number_to_string.end());
+}
- EXPECT_EQ(loop_counter, 3);
-
- {
- HashMap<String, int, CaseInsensitiveStringTraits> casemap;
- EXPECT_EQ(String("nickserv").to_lowercase(), String("NickServ").to_lowercase());
- casemap.set("nickserv", 3);
- casemap.set("NickServ", 3);
- EXPECT_EQ(casemap.size(), 1);
- }
-
- return 0;
+TEST_CASE(case_insensitive)
+{
+ HashMap<String, int, CaseInsensitiveStringTraits> casemap;
+ EXPECT_EQ(String("nickserv").to_lowercase(), String("NickServ").to_lowercase());
+ casemap.set("nickserv", 3);
+ casemap.set("NickServ", 3);
+ EXPECT_EQ(casemap.size(), 1);
}
+
+TEST_MAIN(HashMap)
diff --git a/AK/Tests/TestHelpers.h b/AK/Tests/TestHelpers.h
deleted file mode 100644
index 311eb6ad1f..0000000000
--- a/AK/Tests/TestHelpers.h
+++ /dev/null
@@ -1,68 +0,0 @@
-#pragma once
-
-#include <stdio.h>
-#include <AK/AKString.h>
-
-#define LOG_FAIL(cond) \
- fprintf(stderr, "\033[31;1mFAIL\033[0m: " #cond "\n")
-
-#define LOG_PASS(cond) \
- fprintf(stderr, "\033[32;1mPASS\033[0m: " #cond "\n")
-
-#define LOG_FAIL_EQ(cond, expected_value, actual_value) \
- fprintf(stderr, "\033[31;1mFAIL\033[0m: " #cond " should be " #expected_value ", got "); \
- stringify_for_test(actual_value); \
- fprintf(stderr, "\n")
-
-#define LOG_PASS_EQ(cond, expected_value) \
- fprintf(stderr, "\033[32;1mPASS\033[0m: " #cond " should be " #expected_value " and it is\n")
-
-#define EXPECT_EQ(expr, expected_value) \
- do { \
- auto result = (expr); \
- if (!(result == expected_value)) { \
- LOG_FAIL_EQ(expr, expected_value, result); \
- } else { \
- LOG_PASS_EQ(expr, expected_value); \
- } \
- } while(0)
-
-#define EXPECT(cond) \
- do { \
- if (!(cond)) { \
- LOG_FAIL(cond); \
- } else { \
- LOG_PASS(cond); \
- } \
- } while(0)
-
-inline void stringify_for_test(int value)
-{
- fprintf(stderr, "%d", value);
-}
-
-inline void stringify_for_test(unsigned value)
-{
- fprintf(stderr, "%u", value);
-}
-
-inline void stringify_for_test(const char* value)
-{
- fprintf(stderr, "%s", value);
-}
-
-inline void stringify_for_test(char value)
-{
- fprintf(stderr, "%c", value);
-}
-
-inline void stringify_for_test(const AK::String& string)
-{
- stringify_for_test(string.characters());
-}
-
-inline void stringify_for_test(const AK::StringImpl& string)
-{
- stringify_for_test(string.characters());
-}
-
diff --git a/AK/Tests/TestJSON.cpp b/AK/Tests/TestJSON.cpp
index 72fb007de5..875ba6b99a 100644
--- a/AK/Tests/TestJSON.cpp
+++ b/AK/Tests/TestJSON.cpp
@@ -1,4 +1,4 @@
-#include "TestHelpers.h"
+#include <AK/TestSuite.h>
#include <AK/AKString.h>
#include <AK/HashMap.h>
#include <AK/JsonArray.h>
@@ -6,9 +6,7 @@
#include <AK/JsonValue.h>
#include <AK/StringBuilder.h>
-typedef HashMap<int, int> IntIntMap;
-
-int main()
+TEST_CASE(load_form)
{
FILE* fp = fopen("../../Base/home/anon/test.frm", "r");
ASSERT(fp);
@@ -42,6 +40,6 @@ int main()
//dbgprintf("Set property %s.%s to '%s'\n", widget_class.characters(), property_name.characters(), property_value.serialized().characters());
});
});
-
- return 0;
}
+
+TEST_MAIN(JSON)
diff --git a/AK/Tests/TestQueue.cpp b/AK/Tests/TestQueue.cpp
index a604074215..23d30f5de2 100644
--- a/AK/Tests/TestQueue.cpp
+++ b/AK/Tests/TestQueue.cpp
@@ -1,12 +1,15 @@
-#include "TestHelpers.h"
+#include <AK/TestSuite.h>
#include <AK/AKString.h>
#include <AK/Queue.h>
-int main()
+TEST_CASE(construct)
{
EXPECT(Queue<int>().is_empty());
EXPECT(Queue<int>().size() == 0);
+}
+TEST_CASE(populate_int)
+{
Queue<int> ints;
ints.enqueue(1);
ints.enqueue(2);
@@ -18,7 +21,10 @@ int main()
EXPECT_EQ(ints.size(), 1);
EXPECT_EQ(ints.dequeue(), 3);
EXPECT_EQ(ints.size(), 0);
+}
+TEST_CASE(populate_string)
+{
Queue<String> strings;
strings.enqueue("ABC");
strings.enqueue("DEF");
@@ -26,6 +32,12 @@ int main()
EXPECT_EQ(strings.dequeue(), "ABC");
EXPECT_EQ(strings.dequeue(), "DEF");
EXPECT(strings.is_empty());
+}
+
+TEST_CASE(order)
+{
+ Queue<String> strings;
+ EXPECT(strings.is_empty());
for (int i = 0; i < 10000; ++i) {
strings.enqueue(String::number(i));
@@ -38,6 +50,6 @@ int main()
}
EXPECT(strings.is_empty());
-
- return 0;
}
+
+TEST_MAIN(Queue)
diff --git a/AK/Tests/TestString.cpp b/AK/Tests/TestString.cpp
index 808d0904be..a8a926d1ec 100644
--- a/AK/Tests/TestString.cpp
+++ b/AK/Tests/TestString.cpp
@@ -1,7 +1,7 @@
-#include "TestHelpers.h"
+#include <AK/TestSuite.h>
#include <AK/AKString.h>
-int main()
+TEST_CASE(construct_empty)
{
EXPECT(String().is_null());
EXPECT(String().is_empty());
@@ -9,22 +9,29 @@ int main()
EXPECT(!String("").is_null());
EXPECT(String("").is_empty());
- EXPECT(String("").characters());
+ EXPECT(String("").characters() != nullptr);
EXPECT(String("").impl() == String::empty().impl());
+}
+TEST_CASE(construct_contents)
+{
String test_string = "ABCDEF";
EXPECT(!test_string.is_empty());
EXPECT(!test_string.is_null());
EXPECT_EQ(test_string.length(), 6);
EXPECT_EQ(test_string.length(), (int)strlen(test_string.characters()));
- EXPECT(test_string.characters());
+ EXPECT(test_string.characters() != nullptr);
EXPECT(!strcmp(test_string.characters(), "ABCDEF"));
EXPECT(test_string == "ABCDEF");
EXPECT(test_string != "ABCDE");
EXPECT(test_string != "ABCDEFG");
+}
+TEST_CASE(compare)
+{
+ String test_string = "ABCDEF";
EXPECT("a" < String("b"));
EXPECT(!("a" > String("b")));
EXPECT("b" > String("a"));
@@ -33,36 +40,70 @@ int main()
EXPECT(!("a" >= String("b")));
EXPECT("a" <= String("a"));
EXPECT(!("b" <= String("a")));
+}
+TEST_CASE(index_access)
+{
+ String test_string = "ABCDEF";
EXPECT_EQ(test_string[0], 'A');
EXPECT_EQ(test_string[1], 'B');
+}
+TEST_CASE(starts_with)
+{
+ String test_string = "ABCDEF";
EXPECT(test_string.starts_with("AB"));
EXPECT(test_string.starts_with("ABCDEF"));
EXPECT(!test_string.starts_with("DEF"));
+}
+TEST_CASE(ends_with)
+{
+ String test_string = "ABCDEF";
EXPECT(test_string.ends_with("EF"));
EXPECT(test_string.ends_with("ABCDEF"));
EXPECT(!test_string.ends_with("ABC"));
+}
+TEST_CASE(copy_string)
+{
+ String test_string = "ABCDEF";
auto test_string_copy = test_string;
EXPECT_EQ(test_string, test_string_copy);
EXPECT_EQ(test_string.characters(), test_string_copy.characters());
+}
+TEST_CASE(move_string)
+{
+ String test_string = "ABCDEF";
+ auto test_string_copy = test_string;
auto test_string_move = move(test_string_copy);
EXPECT_EQ(test_string, test_string_move);
EXPECT(test_string_copy.is_null());
+}
+TEST_CASE(repeated)
+{
EXPECT_EQ(String::repeated('x', 0), "");
EXPECT_EQ(String::repeated('x', 1), "x");
EXPECT_EQ(String::repeated('x', 2), "xx");
+}
+TEST_CASE(to_int)
+{
bool ok;
EXPECT(String("123").to_int(ok) == 123 && ok);
EXPECT(String("-123").to_int(ok) == -123 && ok);
+}
+TEST_CASE(to_lowercase)
+{
EXPECT(String("ABC").to_lowercase() == "abc");
- EXPECT(String("AbC").to_uppercase() == "ABC");
+}
- return 0;
+TEST_CASE(to_uppercase)
+{
+ EXPECT(String("AbC").to_uppercase() == "ABC");
}
+
+TEST_MAIN(String)
diff --git a/AK/Tests/TestVector.cpp b/AK/Tests/TestVector.cpp
index d8f8fa405c..ad281d3b0c 100644
--- a/AK/Tests/TestVector.cpp
+++ b/AK/Tests/TestVector.cpp
@@ -1,12 +1,15 @@
-#include "TestHelpers.h"
+#include <AK/TestSuite.h>
#include <AK/AKString.h>
#include <AK/Vector.h>
-int main()
+TEST_CASE(construct)
{
EXPECT(Vector<int>().is_empty());
EXPECT(Vector<int>().size() == 0);
+}
+TEST_CASE(ints)
+{
Vector<int> ints;
ints.append(1);
ints.append(2);
@@ -21,7 +24,10 @@ int main()
ints.clear();
EXPECT_EQ(ints.size(), 0);
+}
+TEST_CASE(strings)
+{
Vector<String> strings;
strings.append("ABC");
strings.append("DEF");
@@ -40,22 +46,23 @@ int main()
++loop_counter;
}
EXPECT_EQ(loop_counter, 2);
+}
- {
- Vector<String> strings;
- strings.append("abc");
- strings.append("def");
- strings.append("ghi");
-
- strings.insert_before_matching("f-g", [](auto& entry) {
- return "f-g" < entry;
- });
+TEST_CASE(strings_insert_ordered)
+{
+ Vector<String> strings;
+ strings.append("abc");
+ strings.append("def");
+ strings.append("ghi");
- EXPECT_EQ(strings[0], "abc");
- EXPECT_EQ(strings[1], "def");
- EXPECT_EQ(strings[2], "f-g");
- EXPECT_EQ(strings[3], "ghi");
- }
+ strings.insert_before_matching("f-g", [](auto& entry) {
+ return "f-g" < entry;
+ });
- return 0;
+ EXPECT_EQ(strings[0], "abc");
+ EXPECT_EQ(strings[1], "def");
+ EXPECT_EQ(strings[2], "f-g");
+ EXPECT_EQ(strings[3], "ghi");
}
+
+TEST_MAIN(Vector)