/* test-nongui.c
 * This file is part of Amsam photo cropping program
 * Copyright 2021, 2023, 2024, 2025, 2026 Nandakumar Edamana
 * Distributed under GNU General Public License, version 3
 */ 

#include <assert.h>
#include <stdarg.h>
#include <stdbool.h>

#include "test-nongui.h"


void test_nongui_memleak_draw(int count)
{
	printf("test memory leak: %d x draw in ImageDocument\n", count);
	ImageDocument *doc = (ImageDocument *) malloc(sizeof(ImageDocument));
	if (doc == NULL) {
		perror(NULL);
		exit(EXIT_FAILURE);
	}

	image_document_construct(doc, 16, 16, default_drawparams(), true);
	for (int i = 0; i < count; i += 1) {
		doc->drawparams.active_tool = TOOL_FLOODFILL;
		doc->drawparams.fgcolor = (i % 2)? LINEAR_PIXEL_BLACK: LINEAR_PIXEL_RED;
		image_document_floodfill(doc, 0, 0);
	}

	if (doc) {
		image_document_destruct(doc);
		free(doc);
	}
}

void test_nongui_memleak_imgdoc(int count)
{
	printf("test memory leak: %d x new ImageDocument\n", count);
	for (int i = 0; i < count; i += 1) {
		ImageDocument *doc = (ImageDocument *) malloc(sizeof(ImageDocument));
		if (doc == NULL) {
			perror(NULL);
			exit(EXIT_FAILURE);
		}

		image_document_construct(doc, 2480, 3508, default_drawparams(), true);
		if (doc) {
			image_document_destruct(doc);
			free(doc);
		}
	}
}

void test_nongui_layerops_bg_fg()
{
	printf("test ImageDocument (bg-fg): default layer status\n");

	ImageDocument *doc = (ImageDocument *) malloc(sizeof(ImageDocument));
	if (doc == NULL) {
		perror(NULL);
		exit(EXIT_FAILURE);
	}

	image_document_construct(doc, 16, 16, default_drawparams(), true);
	assert(layer_owning_vector_get_count(doc->layers) == 2);
	assert(intvector_get_count(doc->layerstack) == 2);
	assert(doc->in_bg_fg_mode);
	assert(image_document_get_bglayer_must(doc));
	assert(image_document_get_fglayer_must(doc));
	assert(image_document_get_curlayer(doc));

	assert(image_document_get_curlayer(doc) == image_document_get_fglayer_must(doc));
	printf("test ImageDocument (bg-fg): delete FG layer\n");

	assert(image_document_can_delete_layer(doc));
	image_document_delete_layer(doc);
	assert(layer_owning_vector_get_count(doc->layers) == 2);
	assert(intvector_get_count(doc->layerstack) == 1);
	assert(!doc->in_bg_fg_mode);
	assert(image_document_get_bglayer_must(doc));
	assert(image_document_get_fglayer_must(doc));
	assert(image_document_get_curlayer(doc));
	assert(image_document_get_curlayer(doc) == image_document_get_bglayer_must(doc));
	assert(!image_document_can_delete_layer(doc));
	if (doc) {
		image_document_destruct(doc);
		free(doc);
	}
}

void test_nongui_layerops_non_bg_fg()
{
	printf("test ImageDocument (non-bg-fg): default layer status\n");

	ImageDocument *doc = (ImageDocument *) malloc(sizeof(ImageDocument));
	if (doc == NULL) {
		perror(NULL);
		exit(EXIT_FAILURE);
	}

	image_document_construct(doc, 16, 16, default_drawparams(), false);
	assert(!doc->in_bg_fg_mode);

	assert(!image_document_can_delete_layer(doc));
	printf("test ImageDocument (non-bg-fg): create layer\n");

	assert(image_document_can_create_layer(doc));
	image_document_create_layer(doc, "Layer 1", LINEAR_PIXEL_TRANSP);
	assert(layer_owning_vector_get_count(doc->layers) == 1);
	assert(intvector_get_count(doc->layerstack) == 1);
	assert(!doc->in_bg_fg_mode);

	assert(!image_document_can_delete_layer(doc));
	assert(image_document_can_create_layer(doc));
	image_document_create_layer(doc, "Layer 2", LINEAR_PIXEL_TRANSP);
	assert(layer_owning_vector_get_count(doc->layers) == 2);
	assert(intvector_get_count(doc->layerstack) == 2);
	assert(!doc->in_bg_fg_mode);
	assert(image_document_can_delete_layer(doc));
	if (doc) {
		image_document_destruct(doc);
		free(doc);
	}
}

int testmain_nongui(int count)
{
	test_nongui_memleak_imgdoc(count);
	test_nongui_memleak_draw(count);
	test_nongui_layerops_bg_fg();

	test_nongui_layerops_non_bg_fg();

	return 0;
}
