Skip to content

Commit 62a5b1f

Browse files
add new test cases to showcase DDD usage
1 parent 7b1e52b commit 62a5b1f

File tree

2 files changed

+372
-0
lines changed

2 files changed

+372
-0
lines changed

code/tests/cases/test_ddd.c

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/*
2+
* -----------------------------------------------------------------------------
3+
* Project: Fossil Logic
4+
*
5+
* This file is part of the Fossil Logic project, which aims to develop high-
6+
* performance, cross-platform applications and libraries. The code contained
7+
* herein is subject to the terms and conditions defined in the project license.
8+
*
9+
* Author: Michael Gene Brockus (Dreamer)
10+
* Date: 07/01/2024
11+
*
12+
* Copyright (C) 2024 Fossil Logic. All rights reserved.
13+
* -----------------------------------------------------------------------------
14+
*/
15+
#include <fossil/test/framework.h>
16+
#include <string.h>
17+
18+
// Define the necessary types and functions for the test cases
19+
typedef struct {
20+
int id;
21+
char name[50];
22+
int processed;
23+
} Entity;
24+
25+
typedef struct {
26+
int x;
27+
int y;
28+
} ValueObject;
29+
30+
typedef struct {
31+
int id;
32+
int child_count;
33+
Entity children[10];
34+
} AggregateRoot;
35+
36+
typedef struct {
37+
Entity entities[10];
38+
int count;
39+
} Repository;
40+
41+
typedef struct {
42+
// Service-specific fields
43+
} Service;
44+
45+
Entity create_entity(int id, const char *name) {
46+
Entity entity;
47+
entity.id = id;
48+
strcpy(entity.name, name);
49+
entity.processed = 0;
50+
return entity;
51+
}
52+
53+
ValueObject create_value_object(int x, int y) {
54+
ValueObject vo;
55+
vo.x = x;
56+
vo.y = y;
57+
return vo;
58+
}
59+
60+
int value_object_equals(ValueObject vo1, ValueObject vo2) {
61+
return (vo1.x == vo2.x && vo1.y == vo2.y);
62+
}
63+
64+
AggregateRoot create_aggregate_root(int id) {
65+
AggregateRoot ar;
66+
ar.id = id;
67+
ar.child_count = 0;
68+
return ar;
69+
}
70+
71+
void add_child_entity(AggregateRoot *ar, Entity entity) {
72+
if (ar->child_count < 10) {
73+
ar->children[ar->child_count++] = entity;
74+
}
75+
}
76+
77+
Repository create_repository() {
78+
Repository repo;
79+
repo.count = 0;
80+
return repo;
81+
}
82+
83+
void repository_add(Repository *repo, Entity entity) {
84+
if (repo->count < 10) {
85+
repo->entities[repo->count++] = entity;
86+
}
87+
}
88+
89+
int repository_count(Repository *repo) {
90+
return repo->count;
91+
}
92+
93+
Entity repository_get(Repository *repo, int id) {
94+
for (int i = 0; i < repo->count; ++i) {
95+
if (repo->entities[i].id == id) {
96+
return repo->entities[i];
97+
}
98+
}
99+
Entity empty_entity = {0};
100+
return empty_entity;
101+
}
102+
103+
Service create_service() {
104+
Service service;
105+
// Initialize service-specific fields
106+
return service;
107+
}
108+
109+
void service_process(Service *service, Entity *entity) {
110+
entity->processed = 1;
111+
}
112+
113+
// * * * * * * * * * * * * * * * * * * * * * * * *
114+
// * Fossil Logic Test Utilites
115+
// * * * * * * * * * * * * * * * * * * * * * * * *
116+
// Setup steps for things like test fixtures and
117+
// mock objects are set here.
118+
// * * * * * * * * * * * * * * * * * * * * * * * *
119+
120+
// Define the test suite and add test cases
121+
FOSSIL_TEST_SUITE(c_ddd_suite);
122+
123+
// Setup function for the test suite
124+
FOSSIL_SETUP(c_ddd_suite) {
125+
// Setup code here
126+
}
127+
128+
// Teardown function for the test suite
129+
FOSSIL_TEARDOWN(c_ddd_suite) {
130+
// Teardown code here
131+
}
132+
133+
// * * * * * * * * * * * * * * * * * * * * * * * *
134+
// * Fossil Logic Test Cases
135+
// * * * * * * * * * * * * * * * * * * * * * * * *
136+
// The test cases below are provided as samples, showcasing
137+
// Domain-Driven Design (DDD) usage in the Fossil Logic project.
138+
// * * * * * * * * * * * * * * * * * * * * * * * *
139+
140+
FOSSIL_TEST_CASE(c_ddd_entity_creation) {
141+
// Example of creating an entity
142+
Entity entity = create_entity(42, "Sample Entity");
143+
144+
// Test cases
145+
FOSSIL_TEST_ASSUME(entity.id == 42, "Entity ID should be 42");
146+
FOSSIL_TEST_ASSUME(strcmp(entity.name, "Sample Entity") == 0, "Entity name should be 'Sample Entity'");
147+
} // end case
148+
149+
FOSSIL_TEST_CASE(c_ddd_value_object_equality) {
150+
// Example of value object equality
151+
ValueObject vo1 = create_value_object(10, 20);
152+
ValueObject vo2 = create_value_object(10, 20);
153+
154+
// Test cases
155+
FOSSIL_TEST_ASSUME(value_object_equals(vo1, vo2), "Value objects should be equal");
156+
} // end case
157+
158+
FOSSIL_TEST_CASE(c_ddd_aggregate_root_behavior) {
159+
// Example of aggregate root behavior
160+
AggregateRoot ar = create_aggregate_root(1);
161+
add_child_entity(&ar, create_entity(2, "Child Entity"));
162+
163+
// Test cases
164+
FOSSIL_TEST_ASSUME(ar.child_count == 1, "Aggregate root should have one child entity");
165+
FOSSIL_TEST_ASSUME(ar.children[0].id == 2, "Child entity ID should be 2");
166+
} // end case
167+
168+
FOSSIL_TEST_CASE(c_ddd_repository_usage) {
169+
// Example of repository usage
170+
Repository repo = create_repository();
171+
Entity entity = create_entity(1, "Repo Entity");
172+
repository_add(&repo, entity);
173+
174+
// Test cases
175+
FOSSIL_TEST_ASSUME(repository_count(&repo) == 1, "Repository should contain one entity");
176+
FOSSIL_TEST_ASSUME(repository_get(&repo, 1).id == 1, "Retrieved entity ID should be 1");
177+
} // end case
178+
179+
FOSSIL_TEST_CASE(c_ddd_service_layer) {
180+
// Example of service layer usage
181+
Service service = create_service();
182+
Entity entity = create_entity(1, "Service Entity");
183+
service_process(&service, &entity);
184+
185+
// Test cases
186+
FOSSIL_TEST_ASSUME(entity.processed == true, "Entity should be processed by the service");
187+
} // end case
188+
189+
// * * * * * * * * * * * * * * * * * * * * * * * *
190+
// * Fossil Logic Test Pool
191+
// * * * * * * * * * * * * * * * * * * * * * * * *
192+
FOSSIL_TEST_GROUP(c_ddd_test_cases) {
193+
FOSSIL_TEST_ADD(c_ddd_suite, c_ddd_entity_creation);
194+
FOSSIL_TEST_ADD(c_ddd_suite, c_ddd_value_object_equality);
195+
FOSSIL_TEST_ADD(c_ddd_suite, c_ddd_aggregate_root_behavior);
196+
FOSSIL_TEST_ADD(c_ddd_suite, c_ddd_repository_usage);
197+
FOSSIL_TEST_ADD(c_ddd_suite, c_ddd_service_layer);
198+
199+
FOSSIL_TEST_REGISTER(c_ddd_suite);
200+
} // end of group

code/tests/cases/test_ddd.cpp

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
* -----------------------------------------------------------------------------
3+
* Project: Fossil Logic
4+
*
5+
* This file is part of the Fossil Logic project, which aims to develop high-
6+
* performance, cross-platform applications and libraries. The code contained
7+
* herein is subject to the terms and conditions defined in the project license.
8+
*
9+
* Author: Michael Gene Brockus (Dreamer)
10+
* Date: 07/01/2024
11+
*
12+
* Copyright (C) 2024 Fossil Logic. All rights reserved.
13+
* -----------------------------------------------------------------------------
14+
*/
15+
#include <fossil/test/framework.h>
16+
#include <vector>
17+
#include <string>
18+
19+
// Define the necessary types and functions for the test cases
20+
class Entity {
21+
public:
22+
int id;
23+
std::string name;
24+
bool processed;
25+
26+
Entity(int id, const std::string &name) : id(id), name(name), processed(false) {}
27+
};
28+
29+
class ValueObject {
30+
public:
31+
int x, y;
32+
33+
ValueObject(int x, int y) : x(x), y(y) {}
34+
35+
bool operator==(const ValueObject &other) const {
36+
return x == other.x && y == other.y;
37+
}
38+
};
39+
40+
class AggregateRoot {
41+
public:
42+
int id;
43+
std::vector<Entity> children;
44+
45+
AggregateRoot(int id) : id(id) {}
46+
47+
void addChild(const Entity &entity) {
48+
if (children.size() < 10) {
49+
children.push_back(entity);
50+
}
51+
}
52+
};
53+
54+
class Repository {
55+
public:
56+
std::vector<Entity> entities;
57+
58+
void add(const Entity &entity) {
59+
if (entities.size() < 10) {
60+
entities.push_back(entity);
61+
}
62+
}
63+
64+
size_t count() const {
65+
return entities.size();
66+
}
67+
68+
Entity get(int id) const {
69+
for (const auto &entity : entities) {
70+
if (entity.id == id) {
71+
return entity;
72+
}
73+
}
74+
return Entity(0, "");
75+
}
76+
};
77+
78+
class Service {
79+
public:
80+
void process(Entity &entity) {
81+
entity.processed = true;
82+
}
83+
};
84+
85+
// * * * * * * * * * * * * * * * * * * * * * * * *
86+
// * Fossil Logic Test Utilites
87+
// * * * * * * * * * * * * * * * * * * * * * * * *
88+
// Setup steps for things like test fixtures and
89+
// mock objects are set here.
90+
// * * * * * * * * * * * * * * * * * * * * * * * *
91+
92+
// Define the test suite and add test cases
93+
FOSSIL_TEST_SUITE(cpp_ddd_suite);
94+
95+
// Setup function for the test suite
96+
FOSSIL_SETUP(cpp_ddd_suite) {
97+
// Setup code here
98+
}
99+
100+
// Teardown function for the test suite
101+
FOSSIL_TEARDOWN(cpp_ddd_suite) {
102+
// Teardown code here
103+
}
104+
105+
// * * * * * * * * * * * * * * * * * * * * * * * *
106+
// * Fossil Logic Test Cases
107+
// * * * * * * * * * * * * * * * * * * * * * * * *
108+
// The test cases below are provided as samples, showcasing
109+
// Domain-Driven Design (DDD) usage in the Fossil Logic project.
110+
// * * * * * * * * * * * * * * * * * * * * * * * *
111+
112+
FOSSIL_TEST_CASE(cpp_ddd_entity_creation) {
113+
// Example of creating an entity
114+
Entity entity(42, "Sample Entity");
115+
116+
// Test cases
117+
FOSSIL_TEST_ASSUME(entity.id == 42, "Entity ID should be 42");
118+
FOSSIL_TEST_ASSUME(entity.name == "Sample Entity", "Entity name should be 'Sample Entity'");
119+
} // end case
120+
121+
FOSSIL_TEST_CASE(cpp_ddd_value_object_equality) {
122+
// Example of value object equality
123+
ValueObject vo1(10, 20);
124+
ValueObject vo2(10, 20);
125+
126+
// Test cases
127+
FOSSIL_TEST_ASSUME(vo1 == vo2, "Value objects should be equal");
128+
} // end case
129+
130+
FOSSIL_TEST_CASE(cpp_ddd_aggregate_root_behavior) {
131+
// Example of aggregate root behavior
132+
AggregateRoot ar(1);
133+
ar.addChild(Entity(2, "Child Entity"));
134+
135+
// Test cases
136+
FOSSIL_TEST_ASSUME(ar.children.size() == 1, "Aggregate root should have one child entity");
137+
FOSSIL_TEST_ASSUME(ar.children[0].id == 2, "Child entity ID should be 2");
138+
} // end case
139+
140+
FOSSIL_TEST_CASE(cpp_ddd_repository_usage) {
141+
// Example of repository usage
142+
Repository repo;
143+
Entity entity(1, "Repo Entity");
144+
repo.add(entity);
145+
146+
// Test cases
147+
FOSSIL_TEST_ASSUME(repo.count() == 1, "Repository should contain one entity");
148+
FOSSIL_TEST_ASSUME(repo.get(1).id == 1, "Retrieved entity ID should be 1");
149+
} // end case
150+
151+
FOSSIL_TEST_CASE(cpp_ddd_service_layer) {
152+
// Example of service layer usage
153+
Service service;
154+
Entity entity(1, "Service Entity");
155+
service.process(entity);
156+
157+
// Test cases
158+
FOSSIL_TEST_ASSUME(entity.processed == true, "Entity should be processed by the service");
159+
} // end case
160+
161+
// * * * * * * * * * * * * * * * * * * * * * * * *
162+
// * Fossil Logic Test Pool
163+
// * * * * * * * * * * * * * * * * * * * * * * * *
164+
FOSSIL_TEST_GROUP(cpp_ddd_test_cases) {
165+
FOSSIL_TEST_ADD(cpp_ddd_suite, cpp_ddd_entity_creation);
166+
FOSSIL_TEST_ADD(cpp_ddd_suite, cpp_ddd_value_object_equality);
167+
FOSSIL_TEST_ADD(cpp_ddd_suite, cpp_ddd_aggregate_root_behavior);
168+
FOSSIL_TEST_ADD(cpp_ddd_suite, cpp_ddd_repository_usage);
169+
FOSSIL_TEST_ADD(cpp_ddd_suite, cpp_ddd_service_layer);
170+
171+
FOSSIL_TEST_REGISTER(cpp_ddd_suite);
172+
} // end of group

0 commit comments

Comments
 (0)