Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Advanced Systems Programming
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
j2-mcdaid
Advanced Systems Programming
Commits
2f6a9abc
Commit
2f6a9abc
authored
6 months ago
by
j2-mcdaid
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
34020ba2
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Worksheets/Worksheet2/Task 2/unitTest.cpp
+86
-0
86 additions, 0 deletions
Worksheets/Worksheet2/Task 2/unitTest.cpp
with
86 additions
and
0 deletions
Worksheets/Worksheet2/Task 2/unitTest.cpp
0 → 100644
+
86
−
0
View file @
2f6a9abc
#include
"allocator.hpp"
#include
"simpletest/simpletest.h"
#include
<iostream>
using
namespace
std
;
char
const
*
groups
[]
=
{
"Bump"
,
};
// Test 1 - Basic Allocation and Deallocation
DEFINE_TEST_G
(
BumpBasicTest
,
Bump
)
{
std
::
cout
<<
"Test 1 - basic allocation and deallocation: "
<<
std
::
endl
;
bAllocator
allocator
(
64
);
// 64 byte
int
*
x
=
allocator
.
alloc
<
int
>
(
4
);
// 16 byte
TEST_MESSAGE
(
x
!=
nullptr
,
"Allocation Failed for 4 ints"
);
allocator
.
dealloc
();
allocator
.
dealloc
();
}
// Test 2 - Full Allocation
DEFINE_TEST_G
(
BumpFullAllocationTest
,
Bump
)
{
std
::
cout
<<
"Test 2 - full allocation: "
<<
std
::
endl
;
bAllocator
allocator
(
16
);
// 16 byte
int
*
a
=
allocator
.
alloc
<
int
>
(
3
);
// 12 byte
TEST_MESSAGE
(
a
!=
nullptr
,
"Failed to allocate 3 ints."
);
char
*
b
=
allocator
.
alloc
<
char
>
(
4
);
// 4 byte
TEST_MESSAGE
(
b
!=
nullptr
,
"Failed to allocate 4 int"
);
}
// Test 3 - Mixed type allocation
DEFINE_TEST_G
(
BumpMixedTypesTest
,
Bump
)
{
std
::
cout
<<
"Test 3 - mixed type allocation: "
<<
std
::
endl
;
bAllocator
allocator
(
32
);
//32 byte
int
*
x
=
allocator
.
alloc
<
int
>
(
2
);
// 8 byte
char
*
y
=
allocator
.
alloc
<
char
>
(
8
);
// 8 byte
double
*
z
=
allocator
.
alloc
<
double
>
(
1
);
// 8 byte
TEST_MESSAGE
(
x
!=
nullptr
,
"Failed to allocate 2 ints."
);
TEST_MESSAGE
(
y
!=
nullptr
,
"Failed to allocate 10 chars."
);
TEST_MESSAGE
(
z
!=
nullptr
,
"Failed to allocate double."
);
}
// Test 4 - Reset when Deallocation is complete
DEFINE_TEST_G
(
BumpResetOnDeallocationTest
,
Bump
)
{
std
::
cout
<<
"Test 4 - reset when deallocated: "
<<
std
::
endl
;
bAllocator
allocator
(
32
);
// 32 byte
int
*
x
=
allocator
.
alloc
<
int
>
(
3
);
// 12 byte
char
*
y
=
allocator
.
alloc
<
char
>
(
4
);
// 4 byte
TEST_MESSAGE
(
x
!=
nullptr
,
"Failed to allocate 4 ints."
);
TEST_MESSAGE
(
y
!=
nullptr
,
"Failed to alllocate 6 chars."
);
allocator
.
dealloc
();
allocator
.
dealloc
();
// frees up heap
int
*
z
=
allocator
.
alloc
<
int
>
(
6
);
// 24 byte
TEST_MESSAGE
(
z
!=
nullptr
,
"Allocator failed to reset."
);
}
// Test 5 - Over capacity of allocation
DEFINE_TEST_G
(
BumpOverCapacityTest
,
Bump
)
{
std
::
cout
<<
"Test 5 - Over capacity of allocation: "
<<
std
::
endl
;
bAllocator
allocator
(
8
);
// 8 byte
int
*
x
=
allocator
.
alloc
<
int
>
(
1
);
// 4 byte
int
*
y
=
allocator
.
alloc
<
int
>
(
1
);
// 4 byte
int
*
z
=
allocator
.
alloc
<
int
>
(
1
);
// 4 byte - should fail
TEST_MESSAGE
(
x
!=
nullptr
,
"First allocation failure."
);
TEST_MESSAGE
(
y
!=
nullptr
,
"Second allocation failure."
);
TEST_MESSAGE
(
z
==
nullptr
,
"Third allocation failure."
);
}
int
main
()
{
bool
pass
=
true
;
for
(
auto
group
:
groups
)
{
pass
&=
TestFixture
::
ExecuteTestGroup
(
group
,
TestFixture
::
Verbose
);
}
return
pass
?
0
:
1
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment