-
Notifications
You must be signed in to change notification settings - Fork 0
CuTest Testes unitários
Para os nossos testes unitários optamos por utilizar a framework CuTest. Os testes são divididos em: testes relacionados à comparação de filas e testes relacionados à codificação de huffman.
A CuTest Framework possibilita a criação de de test suites (conjunto de testes unitários), assim nosso projeto é separado - primordialmente - em duas grandes test suites, as que testam as funcionalidades das filas e as que testam a codificação de huffman. Aqui na test suite de filas, podemos ver que testamos as ações de criação, enfileiramento e desenfileiramento de ambas as implementações de fila (com heap e sem heap).
#include <stdbool.h>
#include <stdio.h>
#include "../src/lib/fila.h"
#include "./framework/CuTest.h"
#include "./global_tests.h"
void test_new_queue_sh(CuTest *tc)
{
priorityQueue *fila = newPriorityQueue();
CuAssertPtrNotNullMsg(tc,"Error, não foi possível cirar a fila com linked list.",fila);
}
void test_enqueue_sh(CuTest *tc)
{
priorityQueue *fila = newPriorityQueue();
enqueue_sh(&fila,10,1,NULL);
CuAssertIntEquals_Msg(tc,"Error, valor inesperado para valor na fila com linked list.",10,fila->head->value);
}
void test_enqueue_sh_multiple(CuTest *tc)
{
priorityQueue *fila = newPriorityQueue();
enqueue_sh(&fila,10,10,NULL);
enqueue_sh(&fila,24,20,NULL);
enqueue_sh(&fila,2,2,NULL);
enqueue_sh(&fila,3,3,NULL);
int resp = fila->head->value;
CuAssertIntEquals_Msg(tc,"Error, valor inesperado para valor na fila com linked list.",24,resp);
}
void test_dequeue_sh(CuTest *tc)
{
priorityQueue *fila = newPriorityQueue();
enqueue_sh(&fila,10,1,NULL);
int resp = dequeue_sh(fila)->value;
CuAssertIntEquals_Msg(tc,"Error, valor inesperado para valor na fila com linked list.",10,resp);
}
void test_new_heap(CuTest *tc)
{
heapQueue *heap = newHeapQueue();
CuAssertPtrNotNullMsg(tc,"Error, não foi possível cirar a fila com heap.",heap);
}
void test_enqueue_heap(CuTest *tc)
{
heapQueue *heap = newHeapQueue();
enqueue_heap(heap,10,NULL);
CuAssertIntEquals_Msg(tc,"Error, heap queue valor inesperado.",10,item_of(heap,1));
}
void test_enqueue_heap_multiple(CuTest *tc)
{
heapQueue *heap = newHeapQueue();
enqueue_heap(heap,1,NULL);
enqueue_heap(heap,3,NULL);
enqueue_heap(heap,2,NULL);
CuAssertIntEquals_Msg(tc,"Error, heap queue produziu um resultado inesperado.",3,heap->data[1]);
}
void test_dequeue_heap_multiple(CuTest *tc)
{
heapQueue *heap = newHeapQueue();
enqueue_heap(heap,1,NULL);
enqueue_heap(heap,3,NULL);
enqueue_heap(heap,5,NULL);
enqueue_heap(heap,2,NULL);
enqueue_heap(heap,4,NULL);
int resp = dequeue_heap(heap);
CuAssertIntEquals_Msg(tc,"Error, heap queue produziu um resultado inesperado.",5,resp);
CuAssertIntEquals_Msg(tc,"Error, heap queue produziu um resultado inesperado.",4,dequeue_heap(heap));
CuAssertIntEquals_Msg(tc,"Error, heap queue produziu um resultado inesperado.",3,dequeue_heap(heap));
CuAssertIntEquals_Msg(tc,"Error, heap queue produziu um resultado inesperado.",2,dequeue_heap(heap));
CuAssertIntEquals_Msg(tc,"Error, heap queue produziu um resultado inesperado.",1,dequeue_heap(heap));
}
CuSuite *Test_Fila_Sh()
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite,test_new_queue_sh);
SUITE_ADD_TEST(suite,test_enqueue_sh);
SUITE_ADD_TEST(suite,test_dequeue_sh);
SUITE_ADD_TEST(suite,test_enqueue_sh_multiple);
SUITE_ADD_TEST(suite,test_new_heap);
SUITE_ADD_TEST(suite,test_enqueue_heap);
SUITE_ADD_TEST(suite,test_enqueue_heap_multiple);
SUITE_ADD_TEST(suite,test_dequeue_heap_multiple);
return suite;
}
void test_fila_run()
{
CuString *output = CuStringNew();
CuSuite *suite = CuSuiteNew();
CuSuiteAddSuite(suite,Test_Fila_Sh());
CuSuiteRun(suite);
CuSuiteSummary(suite,output);
CuSuiteDetails(suite,output);
printf("%s\n",output->buffer);
}
Para testar a codificação de huffman, criamos uma test suite (conjunto de testes a serem rodados juntos) e inserimos nossos testes, nesse caso testamos: a ação de criar uma fila de prioridade, a funcionalidade de trocar (swap) dois elementos, o enfileiramento na fila, o desenfileiramento da fila e a nossa hashtable. A test suite é chamada quando passamos a flag --debug
ao programa.
#include <stdbool.h>
#include <stdio.h>
#include "../src/huffman/priorityQ.h"
#include "./framework/CuTest.h"
#include "./global_tests.h"
#include "../src/huffman/helpers.h"
void test_new_queue(CuTest *tc)
{
huffheapQueue *heap = newHuffQueue();
CuAssertPtrNotNullMsg(tc,"Error, não foi possível cirar a fila com heap.",heap);
}
void test_swap(CuTest *tc)
{
hufftree_node *a = newHeapNode('R',1);
hufftree_node *b = newHeapNode('C',1);
swap(&a,&b);
CuAssertIntEquals(tc,'R',b->value);
}
void test_enqueue(CuTest *tc)
{
huffheapQueue *heap = newHuffQueue();
huff_enqueue(heap,'C',3);
huff_enqueue(heap,'E',5);
huff_enqueue(heap,'D',4);
huff_enqueue(heap,'G',7);
huff_enqueue(heap,'A',1);
huff_enqueue(heap,'B',2);
huff_enqueue(heap,'F',6);
huff_enqueue(heap,'H',8);
CuAssertIntEquals_Msg(tc,"Error, heap queue valor inesperado.",'A',huff_item_of(heap,1)->value);
}
void test_dequeue_multiple(CuTest *tc)
{
huffheapQueue *heap = newHuffQueue();
huff_enqueue(heap,'C',3);
huff_enqueue(heap,'E',5);
huff_enqueue(heap,'D',4);
huff_enqueue(heap,'G',7);
huff_enqueue(heap,'A',1);
huff_enqueue(heap,'B',2);
huff_enqueue(heap,'F',6);
huff_enqueue(heap,'H',8);
CuAssertIntEquals_Msg(tc,"Error, heap queue valor inesperado.",'A',huff_dequeue(heap)->value);
CuAssertIntEquals_Msg(tc,"Error, heap queue valor inesperado.",'B',huff_dequeue(heap)->value);
CuAssertIntEquals_Msg(tc,"Error, heap queue valor inesperado.",'C',huff_dequeue(heap)->value);
CuAssertIntEquals_Msg(tc,"Error, heap queue valor inesperado.",'D',huff_dequeue(heap)->value);
CuAssertIntEquals_Msg(tc,"Error, heap queue valor inesperado.",'E',huff_dequeue(heap)->value);
CuAssertIntEquals_Msg(tc,"Error, heap queue valor inesperado.",'F',huff_dequeue(heap)->value);
CuAssertIntEquals_Msg(tc,"Error, heap queue valor inesperado.",'G',huff_dequeue(heap)->value);
CuAssertIntEquals_Msg(tc,"Error, heap queue valor inesperado.",'H',huff_dequeue(heap)->value);
}
void test_hashtable(CuTest *tc)
{
hashtable *tb = newhashtable();
put_hashtable(tb,'R',0,'0',0);
put_hashtable(tb,'E',0,'0',0);
put_hashtable(tb,'I',0,'0',0);
CuAssertIntEquals_Msg(tc,"Error, hashtable valor inesperado.",0,get_hashtable(tb,'E')->level);
}
CuSuite *Test_Huffman()
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite,test_new_queue);
SUITE_ADD_TEST(suite,test_swap);
SUITE_ADD_TEST(suite,test_enqueue);
SUITE_ADD_TEST(suite,test_hashtable);
SUITE_ADD_TEST(suite,test_dequeue_multiple);
return suite;
}
void test_huffman_run()
{
CuString *output = CuStringNew();
CuSuite *suite = CuSuiteNew();
CuSuiteAddSuite(suite,Test_Huffman());
CuSuiteRun(suite);
CuSuiteSummary(suite,output);
CuSuiteDetails(suite,output);
printf("%s\n",output->buffer);
}