Skip to content

Commit 8252018

Browse files
authored
Merge pull request #118 from saxbophone/josh/102-add-assertions
Add Assertions
2 parents d0e6d60 + fa6a026 commit 8252018

File tree

11 files changed

+178
-17
lines changed

11 files changed

+178
-17
lines changed

saxbospiral/initialise.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* You should have received a copy of the GNU Affero General Public License
2121
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2222
*/
23+
#include <assert.h>
2324
#include <stdint.h>
2425
#include <stdlib.h>
2526

@@ -54,8 +55,14 @@ sxbp_spiral_t sxbp_blank_spiral() {
5455
* this converts the 0s and 1s in the data into UP, LEFT, DOWN, RIGHT
5556
* instructions which are then used to build the pattern.
5657
* returns a status_t struct with error information (if needed)
58+
*
59+
* Asserts:
60+
* - That the spiral struct pointed to has its pointer attributes set to NULL
5761
*/
5862
sxbp_status_t sxbp_init_spiral(sxbp_buffer_t buffer, sxbp_spiral_t* spiral) {
63+
// preconditional assertions
64+
assert(spiral->lines == NULL);
65+
assert(spiral->co_ord_cache.co_ords.items == NULL);
5966
// result status object
6067
sxbp_status_t result;
6168
// number of lines is number of bits of the data, + 1 for the first UP line

saxbospiral/initialise.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ sxbp_spiral_t sxbp_blank_spiral();
4949
* this converts the 0s and 1s in the data into UP, LEFT, DOWN, RIGHT
5050
* instructions which are then used to build the pattern.
5151
* returns a status_t struct with error information (if needed)
52+
*
53+
* Asserts:
54+
* - That the spiral struct pointed to has its pointer attributes set to NULL
5255
*/
5356
sxbp_status_t sxbp_init_spiral(sxbp_buffer_t buffer, sxbp_spiral_t* spiral);
5457

saxbospiral/plot.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* You should have received a copy of the GNU Affero General Public License
2222
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2323
*/
24+
#include <assert.h>
2425
#include <stdlib.h>
2526

2627
#include "saxbospiral.h"
@@ -31,8 +32,18 @@
3132
extern "C"{
3233
#endif
3334

34-
// returns the sum of all line lengths within the given indexes
35+
/*
36+
* returns the sum of all line lengths within the given indexes
37+
*
38+
* Asserts:
39+
* - That start and end indexes are less than or equal to the spiral size
40+
* - That spiral.lines is not NULL
41+
*/
3542
size_t sxbp_sum_lines(sxbp_spiral_t spiral, size_t start, size_t end) {
43+
// preconditional assertions
44+
assert(start <= spiral.size);
45+
assert(end <= spiral.size);
46+
assert(spiral.lines != NULL);
3647
size_t size = 0;
3748
for(size_t i = start; i < end; i++) {
3849
size += spiral.lines[i].length;
@@ -49,11 +60,21 @@ size_t sxbp_sum_lines(sxbp_spiral_t spiral, size_t start, size_t end) {
4960
* each line segment is only one unit long, meaning multiple ones are needed for
5061
* lines longer than one unit.
5162
* returns a status struct with error information (if any)
63+
*
64+
* Asserts:
65+
* - That the output struct's items pointer is NULL
66+
* - That start and end indexes are less than or equal to the spiral size
67+
* - That spiral.lines is not NULL
5268
*/
5369
sxbp_status_t sxbp_spiral_points(
5470
sxbp_spiral_t spiral, sxbp_co_ord_array_t* output,
5571
sxbp_co_ord_t start_point, size_t start, size_t end
5672
) {
73+
// preconditional assertions
74+
assert(output->items == NULL);
75+
assert(start <= spiral.size);
76+
assert(end <= spiral.size);
77+
assert(spiral.lines != NULL);
5778
// prepare result status
5879
sxbp_status_t result = {{0, 0, 0}, 0};
5980
// the amount of space needed is the sum of all line lengths + 1 for end
@@ -91,15 +112,22 @@ sxbp_status_t sxbp_spiral_points(
91112
}
92113

93114
/*
94-
* given a pointer to a spiral struct an limit, which is the index of the last
115+
* given a pointer to a spiral struct and limit, which is the index of the last
95116
* line to use, calculate and store the co-ordinates of all line segments that
96117
* would make up the spiral if the current lengths and directions were used.
97118
* each line segment is only one unit long, meaning multiple ones are needed for
98119
* lines longer than one unit. The co-ords are stored in the spiral's
99120
* co_ord_cache member and are re-used if they are still valid
100121
* returns a status struct with error information (if any)
122+
*
123+
* Asserts:
124+
* - That spiral->lines is not NULL
125+
* - That limit is less than or equal to spiral->size
101126
*/
102127
sxbp_status_t sxbp_cache_spiral_points(sxbp_spiral_t* spiral, size_t limit) {
128+
// preconditional assertions
129+
assert(spiral->lines != NULL);
130+
assert(limit <= spiral->size);
103131
// prepare result status
104132
sxbp_status_t result = {{0, 0, 0}, 0};
105133
// the amount of space needed is the sum of all line lengths + 1 for end

saxbospiral/plot.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@
3333
extern "C"{
3434
#endif
3535

36-
// returns the sum of all line lengths within the given indexes
36+
/*
37+
* returns the sum of all line lengths within the given indexes
38+
*
39+
* Asserts:
40+
* - That start and end indexes are less than or equal to the spiral size
41+
* - That spiral.lines is not NULL
42+
*/
3743
size_t sxbp_sum_lines(sxbp_spiral_t spiral, size_t start, size_t end);
3844

3945
/*
@@ -45,20 +51,29 @@ size_t sxbp_sum_lines(sxbp_spiral_t spiral, size_t start, size_t end);
4551
* each line segment is only one unit long, meaning multiple ones are needed for
4652
* lines longer than one unit.
4753
* returns a status struct with error information (if any)
54+
*
55+
* Asserts:
56+
* - That the output struct's items pointer is NULL
57+
* - That start and end indexes are less than or equal to the spiral size
58+
* - That spiral.lines is not NULL
4859
*/
4960
sxbp_status_t sxbp_spiral_points(
5061
sxbp_spiral_t spiral, sxbp_co_ord_array_t* output,
5162
sxbp_co_ord_t start_point, size_t start, size_t end
5263
);
5364

5465
/*
55-
* given a pointer to a spiral struct an limit, which is the index of the last
66+
* given a pointer to a spiral struct and limit, which is the index of the last
5667
* line to use, calculate and store the co-ordinates of all line segments that
5768
* would make up the spiral if the current lengths and directions were used.
5869
* each line segment is only one unit long, meaning multiple ones are needed for
59-
* lines longer than one unit. The co-ords are stored in the
60-
* spiral's co_ord_cache member and are re-used if they are still valid
70+
* lines longer than one unit. The co-ords are stored in the spiral's
71+
* co_ord_cache member and are re-used if they are still valid
6172
* returns a status struct with error information (if any)
73+
*
74+
* Asserts:
75+
* - That spiral->lines is not NULL
76+
* - That limit is less than or equal to spiral->size
6277
*/
6378
sxbp_status_t sxbp_cache_spiral_points(sxbp_spiral_t* spiral, size_t limit);
6479

saxbospiral/render.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* You should have received a copy of the GNU Affero General Public License
2121
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2222
*/
23+
#include <assert.h>
2324
#include <stdbool.h>
2425
#include <stdint.h>
2526
#include <stdlib.h>
@@ -39,8 +40,13 @@ extern "C"{
3940
* corners of the square needed to contain the points.
4041
* NOTE: This should NEVER be called with a pointer to anything other than a
4142
* 2-item struct of type co_ord_t
43+
*
44+
* Asserts:
45+
* - That spiral.co_ord_cache.co_ords.items is not NULL
4246
*/
4347
static void get_bounds(sxbp_spiral_t spiral, sxbp_co_ord_t* bounds) {
48+
// preconditional assertions
49+
assert(spiral.co_ord_cache.co_ords.items != NULL);
4450
sxbp_tuple_item_t min_x = 0;
4551
sxbp_tuple_item_t min_y = 0;
4652
sxbp_tuple_item_t max_x = 0;
@@ -70,8 +76,15 @@ static void get_bounds(sxbp_spiral_t spiral, sxbp_co_ord_t* bounds) {
7076
* given a spiral struct and a pointer to a blank bitmap_t struct, writes data
7177
* representing a monochromatic image of the rendered spiral to the bitmap
7278
* returns a status struct with error information (if any)
79+
*
80+
* Asserts:
81+
* - That image->pixels is NULL
82+
* - That spiral.lines is not NULL
7383
*/
7484
sxbp_status_t sxbp_render_spiral(sxbp_spiral_t spiral, sxbp_bitmap_t* image) {
85+
// preconditional assertions
86+
assert(image->pixels == NULL);
87+
assert(spiral.lines != NULL);
7588
// create result status struct
7689
sxbp_status_t result = {{0, 0, 0}, 0};
7790
// plot co-ords of spiral into it's cache
@@ -108,6 +121,8 @@ sxbp_status_t sxbp_render_spiral(sxbp_spiral_t spiral, sxbp_bitmap_t* image) {
108121
for(size_t j = i; j > 0; j--) {
109122
free(image->pixels[j]);
110123
}
124+
// now we need to free() the top-level array
125+
free(image->pixels);
111126
result.location = SXBP_DEBUG;
112127
result.diagnostic = SXBP_MALLOC_REFUSED;
113128
return result;

saxbospiral/render.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ typedef struct sxbp_bitmap_t {
4343
* given a spiral struct and a pointer to a blank bitmap_t struct, writes data
4444
* representing a monochromatic image of the rendered spiral to the bitmap
4545
* returns a status struct with error information (if any)
46+
*
47+
* Asserts:
48+
* - That image->pixels is NULL
49+
* - That spiral.lines is not NULL
4650
*/
4751
sxbp_status_t sxbp_render_spiral(sxbp_spiral_t spiral, sxbp_bitmap_t* image);
4852

saxbospiral/serialise.c

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* You should have received a copy of the GNU Affero General Public License
2222
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2323
*/
24+
#include <assert.h>
2425
#include <stdint.h>
2526
#include <stdio.h>
2627
#include <stdlib.h>
@@ -38,28 +39,49 @@ extern "C"{
3839
const size_t SXBP_FILE_HEADER_SIZE = 37;
3940
const size_t SXBP_LINE_T_PACK_SIZE = 4;
4041

41-
// loads a 64-bit unsigned integer from buffer starting at given index
42+
/*
43+
* loads a 64-bit unsigned integer from buffer starting at given index
44+
*
45+
* Asserts:
46+
* - That buffer->bytes is not NULL
47+
*/
4248
static uint64_t load_uint64_t(sxbp_buffer_t* buffer, size_t start_index) {
49+
// preconditional assertions
50+
assert(buffer->bytes != NULL);
4351
uint64_t value = 0;
4452
for(size_t i = 0; i < 8; i++) {
4553
value |= (buffer->bytes[start_index + i]) << (8 * (7 - i));
4654
}
4755
return value;
4856
}
4957

50-
// loads a 32-bit unsigned integer from buffer starting at given index
58+
/*
59+
* loads a 32-bit unsigned integer from buffer starting at given index
60+
*
61+
* Asserts:
62+
* - That buffer->bytes is not NULL
63+
*/
5164
static uint32_t load_uint32_t(sxbp_buffer_t* buffer, size_t start_index) {
65+
// preconditional assertions
66+
assert(buffer->bytes != NULL);
5267
uint32_t value = 0;
5368
for(size_t i = 0; i < 4; i++) {
5469
value |= (buffer->bytes[start_index + i]) << (8 * (3 - i));
5570
}
5671
return value;
5772
}
5873

59-
// dumps a 64-bit unsigned integer of value to buffer at given index
74+
/*
75+
* dumps a 64-bit unsigned integer of value to buffer at given index
76+
*
77+
* Asserts:
78+
* - That buffer->bytes is not NULL
79+
*/
6080
static void dump_uint64_t(
6181
uint64_t value, sxbp_buffer_t* buffer, size_t start_index
6282
) {
83+
// preconditional assertions
84+
assert(buffer->bytes != NULL);
6385
for(uint8_t i = 0; i < 8; i++) {
6486
uint8_t shift = (8 * (7 - i));
6587
buffer->bytes[start_index + i] = (uint8_t)(
@@ -68,10 +90,17 @@ static void dump_uint64_t(
6890
}
6991
}
7092

71-
// dumps a 32-bit unsigned integer of value to buffer at given index
93+
/*
94+
* dumps a 32-bit unsigned integer of value to buffer at given index
95+
*
96+
* Asserts:
97+
* - That buffer->bytes is not NULL
98+
*/
7299
static void dump_uint32_t(
73100
uint32_t value, sxbp_buffer_t* buffer, size_t start_index
74101
) {
102+
// preconditional assertions
103+
assert(buffer->bytes != NULL);
75104
for(uint8_t i = 0; i < 4; i++) {
76105
uint8_t shift = (8 * (3 - i));
77106
buffer->bytes[start_index + i] = (uint8_t)(
@@ -86,10 +115,17 @@ static void dump_uint32_t(
86115
* returns a serialise_result_t struct, which will contain information about
87116
* whether the operation was successful or not and information about what went
88117
* wrong if it was not successful
118+
*
119+
* Asserts:
120+
* - That buffer.bytes is not NULL
121+
* - That spiral->lines is NULL
89122
*/
90123
sxbp_serialise_result_t sxbp_load_spiral(
91124
sxbp_buffer_t buffer, sxbp_spiral_t* spiral
92125
) {
126+
// preconditional assertions
127+
assert(buffer.bytes != NULL);
128+
assert(spiral->lines == NULL);
93129
sxbp_serialise_result_t result; // build struct for returning success / failure
94130
// first, if header is too small for header + 1 line, then return early
95131
if(buffer.size < SXBP_FILE_HEADER_SIZE + SXBP_LINE_T_PACK_SIZE) {
@@ -177,10 +213,17 @@ sxbp_serialise_result_t sxbp_load_spiral(
177213
* returns a serialise_result_t struct, which will contain information about
178214
* whether the operation was successful or not and information about what went
179215
* wrong if it was not successful
216+
*
217+
* Asserts:
218+
* - That spiral.lines is not NULL
219+
* - That buffer->bytes is NULL
180220
*/
181221
sxbp_serialise_result_t sxbp_dump_spiral(
182222
sxbp_spiral_t spiral, sxbp_buffer_t* buffer
183223
) {
224+
// preconditional assertions
225+
assert(buffer->bytes == NULL);
226+
assert(spiral.lines != NULL);
184227
sxbp_serialise_result_t result; // build struct for returning success / failure
185228
// populate buffer struct, base size on header + spiral size
186229
buffer->size = (SXBP_FILE_HEADER_SIZE + (SXBP_LINE_T_PACK_SIZE * spiral.size));

saxbospiral/serialise.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ extern const size_t SXBP_LINE_T_PACK_SIZE;
6161
* returns a serialise_result_t struct, which will contain information about
6262
* whether the operation was successful or not and information about what went
6363
* wrong if it was not successful
64+
*
65+
* Asserts:
66+
* - That buffer.bytes is not NULL
67+
* - That spiral->lines is NULL
6468
*/
6569
sxbp_serialise_result_t sxbp_load_spiral(
6670
sxbp_buffer_t buffer, sxbp_spiral_t* spiral
@@ -72,6 +76,10 @@ sxbp_serialise_result_t sxbp_load_spiral(
7276
* returns a serialise_result_t struct, which will contain information about
7377
* whether the operation was successful or not and information about what went
7478
* wrong if it was not successful
79+
*
80+
* Asserts:
81+
* - That spiral.lines is not NULL
82+
* - That buffer->bytes is NULL
7583
*/
7684
sxbp_serialise_result_t sxbp_dump_spiral(
7785
sxbp_spiral_t spiral, sxbp_buffer_t* buffer

0 commit comments

Comments
 (0)