AK/Tests: Add a simple EXPECT_EQ macro and use it for the String test.

This commit is contained in:
Andreas Kling 2019-06-14 17:52:51 +02:00
parent 3557f277f6
commit a12751695e
Notes: sideshowbarker 2024-07-19 13:36:27 +09:00
3 changed files with 60 additions and 11 deletions

View file

@ -2,7 +2,7 @@ all: TestString
CXXFLAGS = -std=c++17 -Wall -Wextra
TestString: TestString.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp
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
clean:

View file

@ -1,6 +1,7 @@
#pragma once
#include <stdio.h>
#include <AK/AKString.h>
#define LOG_FAIL(cond) \
fprintf(stderr, "\033[31;1mFAIL\033[0m: " #cond "\n")
@ -8,6 +9,24 @@
#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)) { \
@ -17,3 +36,33 @@
} \
} 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());
}

View file

@ -16,8 +16,8 @@ int main()
String test_string = "ABCDEF";
EXPECT(!test_string.is_empty());
EXPECT(!test_string.is_null());
EXPECT(test_string.length() == 6);
EXPECT(test_string.length() == (int)strlen(test_string.characters()));
EXPECT_EQ(test_string.length(), 6);
EXPECT_EQ(test_string.length(), (int)strlen(test_string.characters()));
EXPECT(test_string.characters());
EXPECT(!strcmp(test_string.characters(), "ABCDEF"));
@ -25,8 +25,8 @@ int main()
EXPECT(test_string != "ABCDE");
EXPECT(test_string != "ABCDEFG");
EXPECT(test_string[0] == 'A');
EXPECT(test_string[1] == 'B');
EXPECT_EQ(test_string[0], 'A');
EXPECT_EQ(test_string[1], 'B');
EXPECT(test_string.starts_with("AB"));
EXPECT(test_string.starts_with("ABCDEF"));
@ -37,16 +37,16 @@ int main()
EXPECT(!test_string.ends_with("ABC"));
auto test_string_copy = test_string;
EXPECT(test_string == test_string_copy);
EXPECT(test_string.characters() == test_string_copy.characters());
EXPECT_EQ(test_string, test_string_copy);
EXPECT_EQ(test_string.characters(), test_string_copy.characters());
auto test_string_move = move(test_string_copy);
EXPECT(test_string == test_string_move);
EXPECT_EQ(test_string, test_string_move);
EXPECT(test_string_copy.is_null());
EXPECT(String::repeated('x', 0) == "");
EXPECT(String::repeated('x', 1) == "x");
EXPECT(String::repeated('x', 2) == "xx");
EXPECT_EQ(String::repeated('x', 0), "");
EXPECT_EQ(String::repeated('x', 1), "x");
EXPECT_EQ(String::repeated('x', 2), "xx");
bool ok;
EXPECT(String("123").to_int(ok) == 123 && ok);