Created
March 12, 2018 13:47
-
-
Save solvingj/693e1c691c50b087918b102ff04d585c to your computer and use it in GitHub Desktop.
unit test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include "CppUnitTest.h" | |
using namespace Microsoft::VisualStudio::CppUnitTestFramework; | |
extern "C" { | |
#include "ArrayList.h" | |
} | |
#include <stdarg.h> | |
static bool OddPredicate(const void *item) | |
{ | |
const int *val = (const int*)item; | |
return (*val) % 2 != 0; | |
} | |
namespace test | |
{ | |
TEST_CLASS(ArrayList_Test) | |
{ | |
ArrayList listobj, *list; | |
void DefaultInit(size_t itemSize) | |
{ | |
list = &listobj; | |
ArrayList_Init(list, itemSize); | |
} | |
void DefaultInit(size_t itemSize, int capacity) | |
{ | |
list = &listobj; | |
ArrayList_InitCapacity(list, itemSize, capacity); | |
} | |
void TestContents(int count, ...) | |
{ | |
va_list args; | |
Assert::AreEqual(count, ArrayList_Count(list)); | |
va_start(args, count); | |
for (int i = 0; i < count; ++i) { | |
int val = va_arg(args, int); | |
CheckValue(ArrayList_GetAt(list, i), val); | |
} | |
va_end(args); | |
} | |
int RemoveOdd() | |
{ | |
Predicate pred; | |
Predicate_Init(&pred, OddPredicate); | |
int result = ArrayList_Prune(list, &pred, NULL); | |
return result; | |
} | |
public: | |
TEST_METHOD_INITIALIZE(Setup) | |
{ | |
SetInitialMemoryState(); | |
list = NULL; | |
} | |
TEST_METHOD_CLEANUP(TearDown) | |
{ | |
if (list) | |
ArrayList_Cleanup(list); | |
Assert::IsFalse(HasMemoryLeaks(), L"You have memory leaks"); | |
} | |
UNIT_TEST(EmptyList) | |
{ | |
DefaultInit(sizeof(int)); | |
Assert::AreEqual(0, ArrayList_Count(list)); | |
Assert::IsTrue(ArrayList_IsEmpty(list)); | |
Assert::AreEqual(sizeof(int), ArrayList_ItemSize(list)); | |
Assert::AreEqual(0, RemoveOdd()); | |
Assert::AreEqual(0, ArrayList_Count(list)); | |
Assert::IsTrue(ArrayList_IsEmpty(list)); | |
Assert::AreEqual(sizeof(int), ArrayList_ItemSize(list)); | |
ArrayList_Clear(list, NULL); | |
Assert::AreEqual(0, ArrayList_Count(list)); | |
Assert::IsTrue(ArrayList_IsEmpty(list)); | |
Assert::AreEqual(sizeof(int), ArrayList_ItemSize(list)); | |
} | |
UNIT_TEST(BasicOperation) | |
{ | |
DefaultInit(sizeof(int)); | |
SetValue(ArrayList_PushBack(list), 23); | |
SetValue(ArrayList_PushFront(list), 15); | |
SetValue(ArrayList_InsertAt(list, 1), -1); | |
SetValue(ArrayList_PushBack(list), 5); | |
SetValue(ArrayList_PushFront(list), 10); | |
TestContents(5, 10, 15, -1, 23, 5); | |
ArrayList_ReserveBack(list, 10); | |
ArrayList_ReserveFront(list, 10); | |
ArrayList_ReserveAt(list, 5, 5); | |
Assert::AreEqual(30, ArrayList_Count(list)); | |
for (int i = 0; i < 30; i++) { | |
SetValue(ArrayList_GetAt(list, i), i); | |
} | |
for (int i = 0; i < 30; i++) { | |
CheckValue(ArrayList_GetAt(list, i), i); | |
} | |
ArrayList_DeleteFront(list, 10, NULL); | |
Assert::AreEqual(20, ArrayList_Count(list)); | |
for (int i = 0; i < 20; i++) { | |
CheckValue(ArrayList_GetAt(list, i), i + 10); | |
} | |
ArrayList_Clear(list, NULL); | |
Assert::AreEqual(0, ArrayList_Count(list)); | |
} | |
UNIT_TEST(InitCapacity) | |
{ | |
const int BIG_SIZE = 10000; | |
DefaultInit(sizeof(int), BIG_SIZE); | |
void *first = ArrayList_PushBack(list); | |
for (int i = 1; i < BIG_SIZE; i++) { | |
ArrayList_PushBack(list); | |
Assert::AreEqual(first, ArrayList_GetFront(list), | |
L"The array list was created with a specified" | |
L" capacity but could not add that many items without resizing."); | |
} | |
} | |
UNIT_TEST(Prune) | |
{ | |
DefaultInit(sizeof(int)); | |
ArrayList_ReserveBack(list, 100); | |
for (int i = 0; i < 100; i++) { | |
SetValue(ArrayList_GetAt(list, i), i); | |
} | |
Assert::AreEqual(50, RemoveOdd()); | |
Assert::AreEqual(50, ArrayList_Count(list)); | |
Assert::AreEqual(0, RemoveOdd()); | |
Assert::AreEqual(50, ArrayList_Count(list)); | |
for (int i = 0; i < 10; i++) { | |
SetValue(ArrayList_PushBack(list), 1); | |
} | |
Assert::AreEqual(60, ArrayList_Count(list)); | |
Assert::AreEqual(10, RemoveOdd()); | |
Assert::AreEqual(50, ArrayList_Count(list)); | |
for (int i = 0; i < 50; i++) { | |
CheckValue(ArrayList_GetAt(list, i), 2 * i); | |
} | |
} | |
UNIT_TEST(Enumerate) | |
{ | |
DefaultInit(sizeof(int)); | |
for (int i = 0; i < 100; i++) { | |
SetValue(ArrayList_PushBack(list), i); | |
} | |
Enumerator *en = ArrayList_Enumerate(list, false); | |
Assert::IsNotNull(en); | |
for (int i = 0; i < 100; i++) { | |
Assert::IsTrue(Enumerator_MoveNext(en)); | |
CheckValue(Enumerator_Current(en), i); | |
} | |
Assert::IsFalse(Enumerator_MoveNext(en)); | |
Enumerator_Reset(en); | |
for (int i = 0; i < 100; i++) { | |
Assert::IsTrue(Enumerator_MoveNext(en)); | |
CheckValue(Enumerator_Current(en), i); | |
} | |
Assert::IsFalse(Enumerator_MoveNext(en)); | |
Enumerator_Cleanup(en); | |
en = ArrayList_Enumerate(list, true); | |
Assert::IsNotNull(en); | |
for (int i = 100 - 1; i >= 0; i--) { | |
Assert::IsTrue(Enumerator_MoveNext(en)); | |
CheckValue(Enumerator_Current(en), i); | |
} | |
Enumerator_Reset(en); | |
for (int i = 100 - 1; i >= 0; i--) { | |
Assert::IsTrue(Enumerator_MoveNext(en)); | |
CheckValue(Enumerator_Current(en), i); | |
} | |
Enumerator_Cleanup(en); | |
} | |
UNIT_TEST(LargeItems) | |
{ | |
struct BigItem { double a, b; }; | |
DefaultInit(sizeof(BigItem)); | |
for (int i = 0; i < 100; i++) { | |
BigItem item = { 1.5*i, -3.0*i }; | |
SetValue(ArrayList_PushBack(list), item); | |
} | |
for (int i = 0; i < 100; i++) { | |
BigItem item = { 1.5*i, -3.0*i }; | |
CheckValue(ArrayList_GetAt(list, i), item); | |
} | |
} | |
UNIT_TEST(SmallItems) | |
{ | |
DefaultInit(sizeof(char)); | |
for (char c = 0; c < 100; c++) { | |
SetValue(ArrayList_PushBack(list), c); | |
} | |
for (char c = 0; c < 100; c++) { | |
CheckValue(ArrayList_GetAt(list, c), c); | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment