Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Chris Müller
crystal
Commits
f806ffb7
Commit
f806ffb7
authored
Oct 30, 2012
by
Chris Müller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test: add testcases for pointer arrays
parent
f9456069
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
57 additions
and
33 deletions
+57
-33
test/array.c
test/array.c
+57
-33
No files found.
test/array.c
View file @
f806ffb7
#include "unittest.h"
#include "structures/array.h"
#include <assert.h>
#include <stdio.h>
static
void
test_array_allocation
(
const_pointer
data
)
{
char
text
[]
=
{
'A'
,
'B'
,
'C'
,
'D'
,
'E'
};
struct
CryArray
*
array
=
cry_array_new
(
2
,
4
);
assert
(
cry_array_size
(
array
)
==
0
);
assert
(
cry_array_alloc
(
array
)
==
4
);
cry_array_append
(
array
,
text
,
2
);
assert
(
cry_array_size
(
array
)
==
2
);
assert
(
cry_array_alloc
(
array
)
==
4
);
cry_array_append
(
array
,
text
+
2
,
3
);
assert
(
cry_array_size
(
array
)
==
5
);
assert
(
cry_array_alloc
(
array
)
==
8
);
cry_array_free
(
array
);
}
#include <string.h>
static
void
test_array_operations
(
const_pointer
data
)
{
char
text
[]
=
{
'A'
,
'B'
,
'C'
,
'D'
,
'E'
};
struct
CryArray
*
array
=
cry_array_new
(
2
,
4
);
struct
CryArray
*
array
=
cry_array_new
(
4
);
assert
(
cry_array_size
(
array
)
==
0
);
assert
(
cry_array_
alloc
(
array
)
==
4
);
assert
(
cry_array_
capacity
(
array
)
==
4
);
cry_array_append
(
array
,
text
,
5
);
cry_array_append
(
array
,
"
\0
"
,
1
);
assert
(
cry_array_size
(
array
)
==
6
);
assert
(
cry_array_
alloc
(
array
)
==
8
);
assert
(
cry_array_
capacity
(
array
)
>
4
);
assert
(
strcmp
(
cry_array_get
(
array
,
0
),
"ABCDE"
)
==
0
);
assert
(
cry_array_remove
(
array
,
0
,
2
)
==
CRY_OKAY
);
assert
(
cry_array_size
(
array
)
==
4
);
assert
(
cry_array_
alloc
(
array
)
==
8
);
assert
(
cry_array_
capacity
(
array
)
>
4
);
assert
(
cry_array_get
(
array
,
0
)
!=
0
);
...
...
@@ -56,19 +32,67 @@ test_array_operations(const_pointer data)
assert
(
cry_array_insert
(
array
,
1
,
"A"
,
1
)
==
CRY_OKAY
);
assert
(
cry_array_size
(
array
)
==
5
);
assert
(
cry_array_
alloc
(
array
)
==
8
);
assert
(
cry_array_
capacity
(
array
)
>
4
);
assert
(
cry_array_get
(
array
,
0
)
!=
0
);
assert
(
strcmp
(
cry_array_get
(
array
,
0
),
"CADE"
)
==
0
);
cry_array_free
(
array
);
assert
(
cry_array_trim
(
array
)
==
cry_array_size
(
array
));
cry_array_free
(
array
);
}
static
void
test_ptr_array_operations
(
const_pointer
data
)
{
struct
CryPtrArray
*
array
=
cry_ptr_array_new
(
4
);
assert
(
cry_ptr_array_size
(
array
)
==
0
);
assert
(
cry_ptr_array_capacity
(
array
)
==
4
);
cry_ptr_array_append
(
array
,
"1"
);
cry_ptr_array_append
(
array
,
"2"
);
cry_ptr_array_append
(
array
,
"3"
);
cry_ptr_array_append
(
array
,
"4"
);
cry_ptr_array_append
(
array
,
"5"
);
assert
(
cry_ptr_array_size
(
array
)
==
5
);
assert
(
cry_ptr_array_size
(
array
)
>
4
);
assert
(
strcmp
(
cry_cast
(
const
char
*
,
cry_ptr_array_get
(
array
,
0
)),
"1"
)
==
0
);
assert
(
strcmp
(
cry_cast
(
const
char
*
,
cry_ptr_array_get
(
array
,
1
)),
"2"
)
==
0
);
assert
(
strcmp
(
cry_cast
(
const
char
*
,
cry_ptr_array_get
(
array
,
2
)),
"3"
)
==
0
);
assert
(
strcmp
(
cry_cast
(
const
char
*
,
cry_ptr_array_get
(
array
,
3
)),
"4"
)
==
0
);
assert
(
strcmp
(
cry_cast
(
const
char
*
,
cry_ptr_array_get
(
array
,
4
)),
"5"
)
==
0
);
cry_ptr_array_remove
(
array
,
0
);
cry_ptr_array_remove
(
array
,
1
);
assert
(
cry_ptr_array_size
(
array
)
==
3
);
assert
(
strcmp
(
cry_cast
(
const
char
*
,
cry_ptr_array_get
(
array
,
0
)),
"2"
)
==
0
);
assert
(
strcmp
(
cry_cast
(
const
char
*
,
cry_ptr_array_get
(
array
,
1
)),
"4"
)
==
0
);
assert
(
strcmp
(
cry_cast
(
const
char
*
,
cry_ptr_array_get
(
array
,
2
)),
"5"
)
==
0
);
assert
(
cry_ptr_array_insert
(
array
,
0
,
"0"
)
==
CRY_OKAY
);
assert
(
strcmp
(
cry_cast
(
const
char
*
,
cry_ptr_array_get
(
array
,
0
)),
"0"
)
==
0
);
assert
(
strcmp
(
cry_cast
(
const
char
*
,
cry_ptr_array_get
(
array
,
1
)),
"2"
)
==
0
);
assert
(
strcmp
(
cry_cast
(
const
char
*
,
cry_ptr_array_get
(
array
,
2
)),
"4"
)
==
0
);
assert
(
strcmp
(
cry_cast
(
const
char
*
,
cry_ptr_array_get
(
array
,
3
)),
"5"
)
==
0
);
assert
(
cry_ptr_array_size
(
array
)
==
4
);
assert
(
cry_ptr_array_trim
(
array
)
==
cry_ptr_array_size
(
array
));
cry_ptr_array_free
(
array
);
}
void
cry_test_arrays
(
void
)
{
cry_unittest_run
(
"structures.array
.allocation
"
,
test_array_
alloc
ation
,
0
,
10
);
cry_unittest_run
(
"structures.array
.operations
"
,
test_array_operations
,
0
,
10
);
cry_unittest_run
(
"structures.array"
,
test_array_
oper
ation
s
,
0
,
10
);
cry_unittest_run
(
"structures.
ptr_
array"
,
test_
ptr_
array_operations
,
0
,
10
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment