aboutsummaryrefslogblamecommitdiffstats
path: root/rtgrpblib_create_raster.c
blob: cbc69f21d92ed3b3925a3cd5a0860703923efba8 (plain) (tree)
1
2
3
4
5
6
7
8
9








                                                         
                    







                                                                                           

                                                                                    







                                

                                              








                      




































                                                                                    


      
/* 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