aboutsummaryrefslogtreecommitdiffstats
path: root/rtgrpblib_create_raster.c
blob: cbc69f21d92ed3b3925a3cd5a0860703923efba8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


RASTER *
rtgrpblib_create_raster(size_t width, size_t height)
{
	RASTER *raster;
	size_t size;

	if (!width || !height) {
		errno = EINVAL;
		return NULL;
	}

	if (width > (SIZE_MAX - offsetof(RASTER, cells)) / sizeof(*raster->cells) / height)
		goto enomem;
	size = width * height;
	raster = calloc(1, offsetof(RASTER, cells) + size * sizeof(*raster->cells));
	if (!raster) {
	enomem:
		errno = ENOMEM;
		return NULL;
	}

	raster->width = width;
	raster->height = height;
	raster->size = size;
	raster->draftness = DEFAULT_DRAFTNESS;
	return raster;
}


#else

int
main(void)
{
	RASTER *raster;
	size_t i, n;

	errno = 0;
	ASSERT(!rtgrpblib_create_raster(0, 1));
	ASSERT(errno == EINVAL);

	errno = 0;
	ASSERT(!rtgrpblib_create_raster(1, 0));
	ASSERT(errno == EINVAL);

	errno = 0;
	ASSERT(!rtgrpblib_create_raster(0, 0));
	ASSERT(errno == EINVAL);

	errno = 0;
	ASSERT(!rtgrpblib_create_raster(SIZE_MAX - offsetof(RASTER, cells) + 1, 1));
	ASSERT(errno == ENOMEM);

	errno = 0;
	ASSERT(!rtgrpblib_create_raster(1, SIZE_MAX - offsetof(RASTER, cells) + 1));
	ASSERT(errno == ENOMEM);

	raster = rtgrpblib_create_raster(10, 20);
	ASSERT(raster);
	ASSERT(raster->width == 10);
	ASSERT(raster->height == 20);
	ASSERT(raster->size == raster->width * raster->height);
	ASSERT(raster->draftness == DEFAULT_DRAFTNESS);
	n = raster->width * raster->height;
	for (i = 0; i < n; i++) {
		ASSERT(!raster->cells[i].cell_coverage);
		ASSERT(!raster->cells[i].opposite_coverage);
	}

	free(raster);
	return 0;
}

#endif