diff TestPriorityQueue.c @ 0:9ecc993a29ea

initial heap implementation
author hausers
date Thu, 09 Feb 2012 11:48:03 +0100
parents
children 500d0e2fabfb
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/TestPriorityQueue.c	Thu Feb 09 11:48:03 2012 +0100
     1.3 @@ -0,0 +1,159 @@
     1.4 +#include <stdbool.h>
     1.5 +#include <stdio.h>
     1.6 +#include <stdlib.h>
     1.7 +
     1.8 +
     1.9 +#include "CUnit/Basic.h"
    1.10 +#include "PriorityQueue.h"
    1.11 +
    1.12 +#ifndef DEBUG
    1.13 +#define DEBUG
    1.14 +#endif
    1.15 +
    1.16 +static PrioQueueStruc *Q; 
    1.17 +static char *first,*second,*third;
    1.18 +
    1.19 +int init_suite (void) {
    1.20 +	if (NULL == (Q= makeVMSPrioQ())) return -1;
    1.21 +	else return 0;
    1.22 +}
    1.23 +
    1.24 +int clean_suite (void) {
    1.25 +	return 0;
    1.26 +}
    1.27 +
    1.28 +void testInsertAElement (void) {
    1.29 +	
    1.30 +	first= malloc(3*sizeof(char));
    1.31 +	first= "foo";
    1.32 +
    1.33 +	printf("SIZE = %d | ",Q->size);
    1.34 +	printf("MAXSIZE = %d | ",Q->maxSize);
    1.35 +	CU_ASSERT(true == insertPrioQ(first,0,Q));
    1.36 +	printPrioQ(Q);
    1.37 +	printf("SIZE = %d | ",Q->size);
    1.38 +	printf("MAXSIZE = %d | ",Q->maxSize);
    1.39 +}
    1.40 +
    1.41 +void testInsertOneElement (void) {
    1.42 +	
    1.43 +	first= malloc(3*sizeof(char));
    1.44 +	first= "ten";
    1.45 +
    1.46 +	CU_ASSERT(true == insertPrioQ(first,10,Q));
    1.47 +	printPrioQ(Q);
    1.48 +}
    1.49 +
    1.50 +void testPopOneElement (void) {
    1.51 +	popPrioQ(Q);
    1.52 +}
    1.53 +
    1.54 +void testInsertSorted (void) {
    1.55 +	second= malloc(4*sizeof(char));
    1.56 +	second= "five";
    1.57 +	
    1.58 +	CU_ASSERT(true == insertPrioQ(second,5,Q));
    1.59 +	printPrioQ(Q);
    1.60 +}
    1.61 +
    1.62 +void testInsertAndHeapify (void) {
    1.63 +	third= malloc(6*sizeof(char));
    1.64 +	third= "twenty";
    1.65 +
    1.66 +	CU_ASSERT(true == insertPrioQ(third,20,Q));
    1.67 +	printPrioQ(Q);
    1.68 +}
    1.69 +
    1.70 +void testGetFirstElement (void) {
    1.71 +	void* elem;
    1.72 +
    1.73 +	elem= getFirstPrioQ(Q);
    1.74 +	CU_ASSERT(elem == third);
    1.75 +	printPrioQ(Q);
    1.76 +}
    1.77 +
    1.78 +void testSwap (void) {
    1.79 +	swap(Q,0,2);
    1.80 +	printPrioQ(Q);
    1.81 +	swap(Q,2,0);
    1.82 +	printPrioQ(Q);
    1.83 +}
    1.84 +
    1.85 +void testPop (void) {
    1.86 +	void *elem;
    1.87 +	
    1.88 +	elem= popPrioQ(Q);
    1.89 +	CU_ASSERT(elem == third);
    1.90 +	printPrioQ(Q);
    1.91 +	printf("size = %d",Q->size);
    1.92 +	elem= popPrioQ(Q);
    1.93 +	CU_ASSERT(elem == first);
    1.94 +	printPrioQ(Q);
    1.95 +	printf("size = %d",Q->size);
    1.96 +	elem= popPrioQ(Q);
    1.97 +	CU_ASSERT(elem == second);
    1.98 +	printPrioQ(Q);
    1.99 +	printf("size = %d",Q->size);
   1.100 +}
   1.101 +
   1.102 +void testPopEmptyQueue (void) {
   1.103 +	void *elem;
   1.104 +	CU_ASSERT (NULL == (elem= popPrioQ(Q)));
   1.105 +}
   1.106 +
   1.107 +void testEnlargeQueue (void) {
   1.108 +	int i;
   1.109 +	char *elem;
   1.110 +	for (i= 0; i<1163; i++) {
   1.111 +		elem= malloc(5*sizeof(char));
   1.112 +		sprintf(elem,"%d",i);
   1.113 +		CU_ASSERT(true == insertPrioQ(elem,i,Q));
   1.114 +	}
   1.115 +}
   1.116 +
   1.117 +
   1.118 +void testPopEnlargedQueue (void) {
   1.119 +	int i;
   1.120 +	char *elem; 
   1.121 +	for (i= 1162; i>= 0; i--) {
   1.122 +		elem= popPrioQ(Q);
   1.123 +		CU_ASSERT(i == atoi(elem));
   1.124 +	}
   1.125 +	CU_ASSERT(NULL == popPrioQ(Q));
   1.126 +}
   1.127 +
   1.128 +int main () {
   1.129 +	CU_pSuite pSuite= NULL;
   1.130 +
   1.131 +	if (CUE_SUCCESS != CU_initialize_registry()) return CU_get_error();
   1.132 +
   1.133 +	pSuite= CU_add_suite("Simple Test Suite",init_suite, clean_suite);
   1.134 +	if (NULL == pSuite) {
   1.135 +		CU_cleanup_registry();
   1.136 +		return CU_get_error();
   1.137 +	}
   1.138 +
   1.139 +	if (NULL == CU_add_test(pSuite,"test of insert first element",testInsertOneElement) ||
   1.140 +		NULL == CU_add_test(pSuite,"test of insert sorted",testInsertSorted) ||
   1.141 +		NULL == CU_add_test(pSuite,"test of insert and heapify",testInsertAndHeapify) ||
   1.142 +		NULL == CU_add_test(pSuite,"test of swap",testSwap) ||
   1.143 +		NULL == CU_add_test(pSuite,"test of get top element",testGetFirstElement) ||
   1.144 +		NULL == CU_add_test(pSuite,"test of pop elements",testPop) || 
   1.145 +		NULL == CU_add_test(pSuite,"test of pop an empty queue",testPopEmptyQueue) || 
   1.146 +	    NULL == CU_add_test(pSuite,"test of again a element",testInsertAElement) ||
   1.147 +	    NULL == CU_add_test(pSuite,"test of remove the element",testPopOneElement) ||
   1.148 +		NULL == CU_add_test(pSuite,"test of enlarge queue",testEnlargeQueue) || 
   1.149 +		NULL == CU_add_test(pSuite,"test of pop enlarged queue",testPopEnlargedQueue)) {
   1.150 +
   1.151 +
   1.152 +
   1.153 +		CU_cleanup_registry();
   1.154 +		return CU_get_error();
   1.155 +	}
   1.156 +	
   1.157 +
   1.158 +	CU_basic_set_mode(CU_BRM_VERBOSE);
   1.159 +	CU_basic_run_tests();
   1.160 +	CU_cleanup_registry();
   1.161 +	return CU_get_error();
   1.162 +}