Skip to content

Commit 2d9aad1

Browse files
committed
added tests
1 parent fb9490e commit 2d9aad1

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

core/include/sorts/MergeSort.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace MergeSort {
1212

1313
void GetMergeCount(Rectangle* array, int left, int middle, int right, int* stepCount);
1414

15-
int GetMergeSortCount(Rectangle* array);
15+
int GetStepCount(Rectangle* array);
1616

1717
void Merge(Rectangle* array, int left, int middle, int right, int* currentStep, SortSequence sort, int currentSize);
1818

core/include/utils/Rect.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ typedef struct {
1010
#define rect_base_color rgb_color { .r = 50, .g = 50, .b = 50 }
1111

1212
// OTHER COLORS TO BETTER DISTINGUISH THE SORTING PROCESS
13+
#define rect_red_color rgb_color { .r = 200, .g = 60, .b = 40 }
1314
#define rect_green_color rgb_color { .r = 0, .g = 150, .b = 75 }
1415
#define rect_blue_color rgb_color { .r = 0, .g = 120, .b = 160 }
1516
#define rect_orange_color rgb_color { .r = 200, .g = 120, .b = 0 }

core/src/sorts/MergeSort.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
#define min(a, b) (((a) < (b)) ? (a) : (b))
1111

12-
#define rect_red_color rgb_color { .r = 200, .g = 60, .b = 40 }
13-
1412
namespace MergeSort {
1513

1614
rgb_color GetRectangleColor(bool isSelected, bool inGrouping) {
@@ -72,7 +70,7 @@ namespace MergeSort {
7270
(*stepCount)++;
7371
}
7472

75-
int GetMergeSortCount(Rectangle* items) {
73+
int GetStepCount(Rectangle* items) {
7674
int* stepCount = (int*) calloc(1, sizeof(int));
7775

7876
// COPY ARRAY TO AVOID CHANGING UNDERLYING DATA
@@ -171,7 +169,7 @@ namespace MergeSort {
171169
Rectangle* array = CopyArray(items);
172170

173171
// RUN THE SORT TO CALCULATE THE TOTAL NUMBER OF STEPS
174-
int stepCount = GetMergeSortCount(items);
172+
int stepCount = GetStepCount(items);
175173

176174
SortSequence sort = create_sort_sequence(stepCount);
177175
sort.steps = (SortStep*) malloc(LIST_SIZE * stepCount * sizeof(SortStep));

test/sorts/MergeSort.cc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <gtest/gtest.h>
2+
#include <SDL3/SDL.h>
3+
4+
#include <TestUtils.hpp>
5+
6+
#include <MergeSort.hpp>
7+
#include <List.hpp>
8+
#include <Rect.hpp>
9+
10+
using namespace MergeSort;
11+
12+
TEST(MergeSort_Test, GetStepCount_GreaterThanOne) {
13+
// ARRANGE
14+
SDL_Renderer* renderer = (SDL_Renderer*) malloc(sizeof(Rectangle));
15+
Rectangle* items = (Rectangle*) malloc(LIST_SIZE * sizeof(Rectangle));
16+
17+
CreateList(renderer, items, WINDOW_WIDTH, WINDOW_HEIGHT);
18+
ShuffleList(items);
19+
20+
// ACT
21+
int stepCount = GetStepCount(items);
22+
23+
// ASSERT
24+
EXPECT_TRUE(stepCount > 1);
25+
}
26+
27+
TEST(MergeSort_Test, GetSequence_CorrectOrder) {
28+
// ARRANGE
29+
SDL_Renderer* renderer = (SDL_Renderer*) malloc(sizeof(Rectangle));
30+
Rectangle* items = (Rectangle*) malloc(LIST_SIZE * sizeof(Rectangle));
31+
32+
CreateList(renderer, items, WINDOW_WIDTH, WINDOW_HEIGHT);
33+
ShuffleList(items);
34+
35+
// ACT
36+
SortSequence sequence = GetSequence(items);
37+
38+
// ASSERT
39+
EXPECT_TRUE(sequence.stepCount > 0);
40+
41+
int lastStep = sequence.stepCount - 1;
42+
int offset;
43+
for (int i = 0; i < LIST_SIZE; i++) {
44+
offset = (lastStep * LIST_SIZE) + i;
45+
EXPECT_EQ(sequence.steps[offset].value, (i + 1));
46+
}
47+
}

0 commit comments

Comments
 (0)