Commit 626510d1 authored by francisco jose  herrero's avatar francisco jose herrero
Browse files

Final

parents
c
p *Deck_Five
x /5gx $4->data
c
r
c
p *Deck_Five
c
n
p *Deck_Five
x /5gx $4->data
n
b arrayRemove
n
}
n
p *Deck_Five
x /5gx $4->data
q
b arrayRemove
r
n
q
b main.c:46
r
n
r
p (array_t*) $r12
p (array_t*) Deck_Five
p *$2
x /5gx $3->data
n
x /5gx $3->data
r
x /5gx $3->data
n
x /5gx $3->data
n
b arrayDelete
n
n
x /5gx $3->data
n
x /5gx $3->data
n
n
x /5gx $3->data
b
c
x /5gx $3->data
cn
n
x /5gx $3->data
p *Deck_Five
n
p (array_t*) $r12
p *$3
p *$4
n
p *$3
p *$4
n
p *$4
n
q
q
b game.c:70
r
b gamePrint
n
b arrayPrint
n
b info
break info
b -v
b
break help
break remove
info b
rm 5
r 5
d 5
d4
d 4
d 3
b 2
d 2
d 2
d 6
b listPrint
c
n
n
c
n
c
n
c
n
c
n
c
n
c
n
c
n
c
n
c
n
c
n
c
n
c
n
c
n
c
n
c
n
c
n
b listPrint if l->size != 0
info break
r 1
d 1
d7
d 7
c
n
c
n
c
n
c
n
b lib.c:87
info break
d 8
c
n
c
n
n
c
n
c
n
c
n
q
b lib.c:87
r
c
b game.c:39
info b
d 1
c
n
n
make
r
n
make
r
n
make
r
n
n
n
q
b game.c:43
r
b arrayPrint
r
n
r
make
r
b game.c:40
r
n
q
gdb main
q
b game.c:40
r
n
b cardAddStacked
c
n
p (list_t*)
p (list_t*) $rdi
n
p (card_t*) $rsi
p *$3
p $4->number
p *$5
p *$4->stacked
n
p *$3
n
c
r
c
q
q
r
q
q
b listClone
r
n
q
b listClone
r
n
n
n
r
n
b
c
p *ret_elem
p*$1
p*$1->data
p *$1->data
p $1->data
p *$3
p (char*) $1->data
n
p elem
p *$6
p (char*) $7->data
p $7->next
p (char*) $9->data
p ret_elem
p *$11
p *$12->next
n
p *$12->next
p *$11
p *ret_elem
p *$15
ret_l
p ret_l
printList(ret_l)
p $17->first
p *$18
p *ret_elem
n
q
CC=c99
CFLAGS=-Wall -Wextra -pedantic -Wno-unused-variable -Wno-unused-parameter -Wfloat-conversion -O0 -ggdb -no-pie -lm
NASM=nasm
NASMFLAGS=-f elf64 -g -F DWARF
all: main tester game lib_asm.o
main: main.c lib_c.o lib_asm.o
$(CC) $(CFLAGS) $^ -o $@
tester: tester.c lib_c.o lib_asm.o
$(CC) $(CFLAGS) $^ -o $@
game: game.c lib_c.o lib_asm.o
$(CC) $(CFLAGS) $^ -o $@
lib_c.o: lib.c
$(CC) $(CFLAGS) -c $< -o $@
lib_asm.o: lib.asm
$(NASM) $(NASMFLAGS) $< -o $@
clean:
rm -f *.o
rm -f main tester game
rm -f salida.propios.caso*
rm -f gameResult*
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include "lib.h"
void fillCardDeck(void* cardDeck, funcAddLast_t* funcAddLast) {
card_t* card;
char* B = "basto";
char* E = "espada";
char* C = "copa";
char* O = "oro";
for(int32_t i=1; i<=7; i++) {
card = cardNew(B, &i); funcAddLast(cardDeck, card); cardDelete(card);
card = cardNew(E, &i); funcAddLast(cardDeck, card); cardDelete(card);
card = cardNew(C, &i); funcAddLast(cardDeck, card); cardDelete(card);
card = cardNew(O, &i); funcAddLast(cardDeck, card); cardDelete(card);
}
for(int32_t i=10; i<=12; i++) {
card = cardNew(B, &i); funcAddLast(cardDeck, card); cardDelete(card);
card = cardNew(E, &i); funcAddLast(cardDeck, card); cardDelete(card);
card = cardNew(C, &i); funcAddLast(cardDeck, card); cardDelete(card);
card = cardNew(O, &i); funcAddLast(cardDeck, card); cardDelete(card);
}
}
void shuffleCardDeck(void* cardDeck, funcSwap_t* funcSwap, int seed) {
srand(seed);
for(int k=0; k<100; k++) {
uint8_t i = (uint8_t)(rand()%40);
uint8_t j = (uint8_t)(rand()%40);
funcSwap(cardDeck, i, j);
}
}
game_t* buildGameWithArray(int seed) {
void* cardDeck = arrayNew(TypeCard, 40);
fillCardDeck(cardDeck, (funcAddLast_t*)&arrayAddLast);
shuffleCardDeck(cardDeck, (funcSwap_t*)&arraySwap, seed);
game_t* game = gameNew(cardDeck, (funcGet_t*)&arrayGet,
(funcRemove_t*)&arrayRemove,
(funcSize_t*)&arrayGetSize,
(funcPrint_t*)&arrayPrint,
(funcDelete_t*)&arrayDelete);
return game;
}
game_t* buildGameWithList(int seed) {
void* cardDeck = listNew(TypeCard);
fillCardDeck(cardDeck, (funcAddLast_t*)&listAddLast);
shuffleCardDeck(cardDeck, (funcSwap_t*)&listSwap, seed);
game_t* game = gameNew(cardDeck, (funcGet_t*)&listGet,
(funcRemove_t*)&listRemove,
(funcSize_t*)&listGetSize,
(funcPrint_t*)&listPrint,
(funcDelete_t*)&listDelete);
return game;
}
void playGame(game_t* game, FILE* pFile) {
gamePrint(game, pFile); fprintf(pFile, "\n");
fprintf(pFile, "-- %i -- START\n",gameGetCardDeckSize(game));
int go = 1;
int count = 0;
while (go == 1) {
fprintf(pFile, "%i -> ", count);
gamePrint(game, pFile); fprintf(pFile, "\n");
go = gamePlayStep(game);
count = count + 1;
}
gamePrint(game, pFile); fprintf(pFile, "\n");
fprintf(pFile, "-- %i -- END \n",gameGetCardDeckSize(game));
}
#ifndef TESTING
int main (int argc, char **argv){
if (argc < 2) {
printf("Usage: ./game <seed>\n");
printf(" <seed> = integer number\n");
return 0;
}
int seed = atoi(argv[1]);
FILE *pfile;
game_t* game;
pfile = fopen("gameResultArray.txt","w");
game = buildGameWithArray(seed);
playGame(game, pfile);
gameDelete(game);
fclose(pfile);
pfile = fopen("gameResultList.txt","w");
game = buildGameWithList(seed);
playGame(game, pfile);
gameDelete(game);
fclose(pfile);
return 0;
}
#endif
This diff is collapsed.
#include "lib.h"
funcCmp_t* getCompareFunction(type_t t) {
switch (t) {
case TypeInt: return (funcCmp_t*)&intCmp; break;
case TypeString: return (funcCmp_t*)&strCmp; break;
case TypeCard: return (funcCmp_t*)&cardCmp; break;
default: break;
}
return 0;
}
funcClone_t* getCloneFunction(type_t t) {
switch (t) {
case TypeInt: return (funcClone_t*)&intClone; break;
case TypeString: return (funcClone_t*)&strClone; break;
case TypeCard: return (funcClone_t*)&cardClone; break;
default: break;
}
return 0;
}
funcDelete_t* getDeleteFunction(type_t t) {
switch (t) {
case TypeInt: return (funcDelete_t*)&intDelete; break;
case TypeString: return (funcDelete_t*)&strDelete; break;
case TypeCard: return (funcDelete_t*)&cardDelete; break;
default: break;
}
return 0;
}
funcPrint_t* getPrintFunction(type_t t) {
switch (t) {
case TypeInt: return (funcPrint_t*)&intPrint; break;
case TypeString: return (funcPrint_t*)&strPrint; break;
case TypeCard: return (funcPrint_t*)&cardPrint; break;
default: break;
}
return 0;
}
/** Array **/
void arrayPrint(array_t* a, FILE* pFile) { void* (*data) = a->data;
uint8_t size = a->size;
if(size == 0) {
fprintf(pFile, "[]");
} else{
funcPrint_t* func = getPrintFunction(a->type);
for(uint8_t i = 0; i < size; i++){
if (i == 0){
fprintf(pFile, "[");
func(data[i], pFile);
}else {
fprintf(pFile, ",");
func(data[i], pFile);
}
}
fprintf(pFile, "]");
}
}
/** Lista **/
void listAddLast(list_t* l, void* data){
funcClone_t* func = getCloneFunction(l->type);
void* clone_data = func(data);
listElem_t* elem_new = malloc(sizeof(listElem_t));
elem_new->data = clone_data;
elem_new->next = NULL;
elem_new->prev = NULL;
if (l->size == 0){
l->first = elem_new;
l->last = elem_new;
}else{
listElem_t* aux = l->last;
l->last = elem_new;
elem_new->prev = aux;
aux->next = elem_new;
}
l->size = l->size + 1;
}
void listPrint(list_t* l, FILE* pFile) {
uint32_t size = l->size;
if(size == 0) {
fprintf(pFile, "[]");
} else{
funcPrint_t* func = getPrintFunction(l->type);
listElem_t* elem = l->first;
uint32_t i = 0;
while(elem != NULL){
if (i == 0){
fprintf(pFile, "[");
func(elem->data, pFile);
}else {
fprintf(pFile, ",");
func(elem->data, pFile);
}
elem = elem->next;
i++;
}
fprintf(pFile, "]");
}
}
/*
list_t* listClone(list_t* l){
list_t* ret_l = (list_t*) malloc(sizeof(list_t));
ret_l->type = l->type;
ret_l->size = 0;
ret_l->first= NULL;
ret_l->last= NULL;
listElem_t* elem = l->first;
while(elem != NULL){
listAddLast(ret_l, elem->data);
elem = elem->next;
}
return ret_l;
}
*/
/** Game **/
game_t* gameNew(void* cardDeck, funcGet_t* funcGet, funcRemove_t* funcRemove, funcSize_t* funcSize, funcPrint_t* funcPrint, funcDelete_t* funcDelete) {
game_t* game = (game_t*)malloc(sizeof(game_t));
game->cardDeck = cardDeck;
game->funcGet = funcGet;
game->funcRemove = funcRemove;
game->funcSize = funcSize;
game->funcPrint = funcPrint;
game->funcDelete = funcDelete;
return game;
}
int gamePlayStep(game_t* g) {
int applied = 0;
uint8_t i = 0;
while(applied == 0 && i+2 < g->funcSize(g->cardDeck)) {
card_t* a = g->funcGet(g->cardDeck,i);
card_t* b = g->funcGet(g->cardDeck,i+1);
card_t* c = g->funcGet(g->cardDeck,i+2);
if( strCmp(cardGetSuit(a), cardGetSuit(c)) == 0 || intCmp(cardGetNumber(a), cardGetNumber(c)) == 0 ) {
card_t* removed = g->funcRemove(g->cardDeck,i);
cardAddStacked(b,removed);
cardDelete(removed);
applied = 1;
}
i++;
}
return applied;
}
uint8_t gameGetCardDeckSize(game_t* g) {
return g->funcSize(g->cardDeck);
}
void gameDelete(game_t* g) {
g->funcDelete(g->cardDeck);
free(g);
}
void gamePrint(game_t* g, FILE* pFile) {
g->funcPrint(g->cardDeck, pFile);
}
#ifndef _LIB_HH_
#define _LIB_HH_
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdarg.h>
typedef enum e_type {
TypeNone = 0,
TypeInt = 1,
TypeString = 2,
TypeCard = 3
} type_t;
typedef int32_t (funcCmp_t)(void*, void*);
typedef void* (funcClone_t)(void*);
typedef void (funcDelete_t)(void*);
typedef void (funcPrint_t)(void*, FILE* pFile);
typedef void* (funcGet_t)(void*, uint8_t);
typedef void* (funcRemove_t)(void*, uint8_t);
typedef void (funcAddLast_t)(void*, void*);
typedef uint8_t (funcSize_t)(void*);
typedef void (funcSwap_t)(void*, uint8_t, uint8_t);
/** Int **/
int32_t intCmp(int32_t* a, int32_t* b);
int32_t* intClone(int32_t* a);
void intDelete(int32_t* a);
void intPrint(int32_t* a, FILE* pFile);
/* String */
int32_t strCmp(char* a, char* b);
char* strClone(char* a);
void strDelete(char* a);
void strPrint(char* a, FILE* pFile);
uint32_t strLen(char* a);
/** Array **/
typedef struct s_array {
type_t type;
uint8_t size;
uint8_t capacity;
void** data;
} array_t;
array_t* arrayNew(type_t t, uint8_t capacity);
uint8_t arrayGetSize(array_t* a);
void arrayAddLast(array_t* a, void* data);
void* arrayGet(array_t* a, uint8_t i);
void* arrayRemove(array_t* a, uint8_t i);
void arraySwap(array_t* a, uint8_t i, uint8_t j);
void arrayDelete(array_t* a);
void arrayPrint(array_t* a, FILE* pFile);
/* List */
typedef struct s_list {
type_t type;
uint8_t size;
struct s_listElem* first;
struct s_listElem* last;
} list_t;
typedef struct s_listElem {
void* data;
struct s_listElem* next;
struct s_listElem* prev;
} listElem_t;
list_t* listNew(type_t t);
uint8_t listGetSize(list_t* l);
void listAddFirst(list_t* l, void* data);
void listAddLast(list_t* l, void* data);
void* listGet(list_t* l, uint8_t i);
void* listRemove(list_t* l, uint8_t i);
void listSwap(list_t* l, uint8_t i, uint8_t j);
void listDelete(list_t* l);
void listPrint(list_t* l, FILE* pFile);
list_t* listClone(list_t* l);
listElem_t* listElemClone(list_t* l, listElem_t* elem);
/** Card **/
typedef struct s_card {
char* suit;
int32_t* number;
list_t* stacked;
} card_t;
card_t* cardNew(char* suit, int32_t* number);
char* cardGetSuit(card_t* c);
int32_t* cardGetNumber(card_t* c);
list_t* cardGetStacked(card_t* c);
int32_t cardCmp(card_t* a, card_t* b);
card_t* cardClone(card_t* c);
void cardAddStacked(card_t* c, card_t* card);
void cardDelete(card_t* c);
void cardPrint(card_t* c, FILE* pFile);
/** Game **/
typedef struct s_game {
void* cardDeck;
funcGet_t* funcGet;
funcRemove_t* funcRemove;
funcSize_t* funcSize;
funcPrint_t* funcPrint;
funcDelete_t* funcDelete;
} game_t;
game_t* gameNew(void* cardDeck, funcGet_t* funcGet,
funcRemove_t* funcRemove,
funcSize_t* funcSize,
funcPrint_t* funcPrint,
funcDelete_t* funcDelete);
int gamePlayStep(game_t* g);
uint8_t gameGetCardDeckSize(game_t* g);
void gameDelete(game_t* g);
void gamePrint(game_t* g, FILE* pFile);
#endif
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include "lib.h"
void test_Array_C();
void test_List_C();
int main (void){
remove("salida.propios.txt");
test_Array_C();
test_List_C();
return 0;
}
void test_Array_C(){
FILE* pFile = fopen("salida.propios.txt" ,"a");
/* ==== Crear Mazo 5 cartas ==== */
type_t card_type = 3;
uint8_t capacity = 5;
array_t* Deck_Five = arrayNew(card_type, capacity);
fprintf(pFile, "\nCREO MAZO CARD:\n");
arrayPrint(Deck_Five, pFile);
for(uint8_t i = 0; i < capacity; i++) {
int rand_ind = rand()%3;
char* suits[4] = {"Espada", "Oro", "Copa", "Basto"};
char* suit = suits[rand_ind];
int32_t* number = malloc(sizeof(int32_t));
*number = i;
card_t* data = cardNew(suit, number);
intDelete(number);
arrayAddLast(Deck_Five, data);
cardDelete(data);
}
/*arraySwap(Deck_Five, 0, 4);
arraySwap(Deck_Five, 1, 3);
arraySwap(Deck_Five, 2, 2);
arraySwap(Deck_Five, 0, 3);
arraySwap(Deck_Five, 1, 4);
arraySwap(Deck_Five, 2, 2);*/
void* removed = arrayRemove(Deck_Five, 2);
cardDelete(removed);
/*
uint8_t size = arrayGetSize(Deck_Five);
for (int j = 0; j < size; j++){
fprintf(pFile, "\number%i - ", arrayGetSize(Deck_Five));
arrayPrint(Deck_Five,pFile);
card_t* cardR = arrayRemove(Deck_Five, j);
fprintf(pFile, "\t");
if (cardR != NULL){
cardPrint(cardR, pFile);
cardDelete(cardR);
}else {
fprintf(pFile, "%p\n",(void* ) cardR);
}
}
fprintf(pFile, "\nREMOVI\n");
arrayPrint(Deck_Five, pFile);
*/
/* ==== Imprimir Mazo 5 cartas ==== */
fprintf(pFile, "\nMAZO ORIGINAL CARD:\n");
arrayPrint(Deck_Five, pFile);
/* ==== Apilamos dos cartas ==== */
void* (*data) = Deck_Five->data;
card_t* card_Top = data[0];
card_t* card_Bottom = data[1];
fprintf(pFile, "\nApila segunda en primera:\n");
cardAddStacked(card_Top, card_Bottom);
/* ==== Re-Imprimir Mazo 5 cartas ==== */
arrayPrint(Deck_Five, pFile);
/* ==== Deletear Mazo ==== */
arrayDelete(Deck_Five);
fclose(pFile);
}
void test_List_C(){
FILE* pFile = fopen("salida.propios.txt" ,"a");
/* ==== Crear Mazo 5 cartas ==== */
type_t card_type = 3;
list_t* Deck_Five = listNew(card_type);
//fprintf(pFile, "\nCREO MAZO LIST:\n");
listPrint(Deck_Five, pFile);
uint32_t size = 5;
for(uint8_t i = 0; i < size; i++) {
int rand_ind = rand()%3;
char* suits[4] = {"Espada", "Oro", "Copa", "Basto"};
char* suit = suits[rand_ind];
int32_t* number = malloc(sizeof(int32_t));
*number = i;
card_t* data = cardNew(suit, number);
intDelete(number);
listAddFirst(Deck_Five, data);
cardDelete(data);
}
/* ==== Imprimir Mazo 5 cartas ==== */
//fprintf(pFile, "\nMAZO ORIGINAL LIST:\n");
listPrint(Deck_Five, pFile);
/* ==== Apilamos dos cartas ==== */
listElem_t* elem = Deck_Five->first;
card_t* card_Top = elem->data;
elem = elem->next;
card_t* card_Bottom = elem->data;
//fprintf(pFile, "\nApila (Oro-1-[]) en (Oro-0-[]):\n");
cardAddStacked(card_Top, card_Bottom);
/* ==== Re-Imprimir Mazo 5 cartas ==== */
listPrint(Deck_Five, pFile);
/* ==== Deletear Mazo ==== */
listDelete(Deck_Five);
fclose(pFile);
}
#!/usr/bin/env bash
reset
command -v valgrind > /dev/null
if [ $? -ne 0 ]; then
echo "ERROR: No se encuentra valgrind."
exit 1
fi
make game
if [ $? -ne 0 ]; then
echo "ERROR: Error de compilacion."
exit 1
fi
valgrind --show-reachable=yes --leak-check=full --error-exitcode=1 ./game 0
if [ $? -ne 0 ]; then
echo " **Error de memoria"
exit 1
fi
echo " "
echo "**Corriendo diferencias entre Listas y Arreglos"
DIFFER="diff -d"
ERRORDIFF=0
$DIFFER gameResultArray.txt gameResultList.txt > /tmp/diff
if [ $? -ne 0 ]; then
echo " **Discrepancia entre Listas y Arreglos"
ERRORDIFF=1
fi
#!/usr/bin/env bash
reset
command -v valgrind > /dev/null
if [ $? -ne 0 ]; then
echo "ERROR: No se encuentra valgrind."
exit 1
fi
make main
if [ $? -ne 0 ]; then
echo "ERROR: Error de compilacion."
exit 1
fi
valgrind --show-reachable=yes --leak-check=full --error-exitcode=1 ./main
if [ $? -ne 0 ]; then
echo " **Error de memoria"
exit 1
fi
#!/usr/bin/env bash
reset
echo " "
echo "**Compilando"
make tester
if [ $? -ne 0 ]; then
echo " **Error de compilacion"
exit 1
fi
echo " "
echo "**Corriendo Valgrind"
command -v valgrind > /dev/null
if [ $? -ne 0 ]; then
echo "ERROR: No se encuentra valgrind."
exit 1
fi
valgrind --show-reachable=yes --leak-check=full --error-exitcode=1 ./tester
if [ $? -ne 0 ]; then
echo " **Error de memoria"
exit 1
fi
echo " "
echo "**Corriendo diferencias con la catedra"
DIFFER="diff -d"
ERRORDIFF=0
$DIFFER salida.propios.caso1.txt salida.catedra.caso1.txt > /tmp/diff1
if [ $? -ne 0 ]; then
echo " **Discrepancia en el caso 1"
ERRORDIFF=1
fi
$DIFFER salida.propios.caso2.txt salida.catedra.caso2.txt > /tmp/diff2
if [ $? -ne 0 ]; then
echo " **Discrepancia en el caso 2"
ERRORDIFF=1
fi
echo " "
if [ $ERRORDIFF -eq 0 ]; then
echo "**Todos los tests pasan"
fi
echo " "
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include "lib.h"
#define RUN(filename, action) pfile=fopen(filename,"a"); action; fclose(pfile);
#define NL(filename) pfile=fopen(filename,"a"); fprintf(pfile,"\n"); fclose(pfile);
char *filename_1 = "salida.propios.caso1.txt";
char *filename_2 = "salida.propios.caso2.txt";
void test_1(char* filename);
void test_2(char* filename);
void test_int(char* filename);
void test_string(char* filename);
void test_array(char* filename);
void test_list(char* filename);
void test_card(char* filename);
void test_game(char* filename);
int main() {
remove(filename_1);
remove(filename_2);
srand(12345);
test_int(filename_1);
test_string(filename_1);
test_array(filename_1);
test_list(filename_1);
test_card(filename_1);
test_game(filename_2);
return 0;
}
char* randomString(uint32_t l) {
// 32 a 126 = caracteres validos
char* s = malloc(l+1);
for(uint32_t i=0; i<(l+1); i++)
s[i] = (char)(33+(rand()%(126-33)));
s[l] = 0;
return s;
}
char* randomHexString(uint32_t l) {
char* s = malloc(l+1);
for(uint32_t i=0; i<(l+1); i++) {
do {
s[i] = (char)(rand()%256);
} while (s[i]==0);
}
s[l] = 0;
return s;
}
char* strings[10] = {"aa","bb","dd","ff","00","zz","cc","ee","gg","hh"};
void test_array(char* filename) {
FILE* pfile;
array_t* a;
void* data;
RUN(filename, fprintf(pfile, "== Array ==\n");) NL(filename)
// Agregar
RUN(filename, fprintf(pfile, "Agregar\n");) NL(filename)
a = arrayNew(TypeString, 20);
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) NL(filename)
arrayAddLast(a,strings[0]);
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) NL(filename)
arrayAddLast(a,strings[1]);
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) NL(filename)
arrayAddLast(a,strings[2]);
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) NL(filename)
NL(filename)
// Get
RUN(filename, fprintf(pfile, "Get\n");) NL(filename)
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) NL(filename)
data = arrayGet(a,0);
RUN(filename, fprintf(pfile, "%s\n", (char*)data);)
data = arrayGet(a,1);
RUN(filename, fprintf(pfile, "%s\n", (char*)data);)
data = arrayGet(a,2);
RUN(filename, fprintf(pfile, "%s\n", (char*)data);)
data = arrayGet(a,3);
RUN(filename, fprintf(pfile, "%s\n", (char*)data);)
NL(filename)
// Quitar
RUN(filename, fprintf(pfile, "Quitar\n");) NL(filename)
data = arrayRemove(a,0);
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) RUN(filename, fprintf(pfile, " - %s\n", (char*)data);)
strDelete((char*)data);
data = arrayRemove(a,1);
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) RUN(filename, fprintf(pfile, " - %s\n", (char*)data);)
strDelete((char*)data);
data = arrayRemove(a,0);
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) RUN(filename, fprintf(pfile, " - %s\n", (char*)data);)
strDelete((char*)data);
NL(filename)
// Swap
RUN(filename, fprintf(pfile, "Swap\n");) NL(filename)
arrayAddLast(a,strings[0]); arrayAddLast(a,strings[1]); arrayAddLast(a,strings[2]);
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) NL(filename)
arraySwap(a, 0, 0);
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) NL(filename)
arraySwap(a, 1, 1);
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) NL(filename)
arraySwap(a, 2, 2);
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) NL(filename)
arraySwap(a, 0, 2);
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) NL(filename)
arraySwap(a, 2, 0);
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) NL(filename)
arraySwap(a, 0, 1);
arraySwap(a, 1, 2);
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) NL(filename)
NL(filename)
// Agregar de mas
RUN(filename, fprintf(pfile, "Agregar de mas\n");) NL(filename)
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) NL(filename)
for(int j=0; j<3; j++) {
for(int i=0; i<10; i++) {
arrayAddLast(a,strings[i]);
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) NL(filename)
}
}
NL(filename)
// Quitar de mas
RUN(filename, fprintf(pfile, "Quitar de mas\n");) NL(filename)
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) NL(filename)
for(int j=0; j<3; j++) {
for(int i=0; i<10; i++) {
data = arrayRemove(a,i);
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) RUN(filename, fprintf(pfile, " - %s\n", (char*)data);)
strDelete((char*)data);
}
}
data = arrayRemove(a,0);
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) RUN(filename, fprintf(pfile, " - %s\n", (char*)data);)
strDelete((char*)data);
data = arrayRemove(a,0);
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) RUN(filename, fprintf(pfile, " - %s\n", (char*)data);)
strDelete((char*)data);
data = arrayRemove(a,10);
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) RUN(filename, fprintf(pfile, " - %s\n", (char*)data);)
strDelete((char*)data);
NL(filename)
// Delete
RUN(filename, fprintf(pfile, "Delete\n");) NL(filename)
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) NL(filename)
for(int i=0; i<10; i++) {
arrayAddLast(a,strings[i]);
}
RUN(filename, fprintf(pfile, "%i - ", arrayGetSize(a));) RUN(filename, arrayPrint(a,pfile);) NL(filename)
arrayDelete(a);
NL(filename)
}
void test_list(char* filename) {
FILE* pfile;
list_t *a, *b;
void* data;
RUN(filename, fprintf(pfile, "== List ==\n");) NL(filename)
// Agregar
RUN(filename, fprintf(pfile, "Agregar\n");) NL(filename)
a = listNew(TypeString);
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) NL(filename)
listAddLast(a,strings[0]);
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) NL(filename)
listAddLast(a,strings[1]);
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) NL(filename)
listAddLast(a,strings[2]);
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) NL(filename)
b = listNew(TypeInt);
RUN(filename, fprintf(pfile, "%i - ", listGetSize(b));) RUN(filename, listPrint(b,pfile);) NL(filename)
for(int i = 0; i < 20; i++){
listAddFirst(b,&i);
RUN(filename, fprintf(pfile, "%i - ", listGetSize(b));) RUN(filename, listPrint(b,pfile);) NL(filename)
}
NL(filename)
// Get
RUN(filename, fprintf(pfile, "Get\n");) NL(filename)
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) NL(filename)
data = listGet(a,0);
RUN(filename, fprintf(pfile, "%s\n", (char*)data);)
data = listGet(a,1);
RUN(filename, fprintf(pfile, "%s\n", (char*)data);)
data = listGet(a,2);
RUN(filename, fprintf(pfile, "%s\n", (char*)data);)
data = listGet(a,3);
RUN(filename, fprintf(pfile, "%s\n", (char*)data);)
NL(filename)
// Quitar
RUN(filename, fprintf(pfile, "Quitar\n");) NL(filename)
data = listRemove(a,0);
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) RUN(filename, fprintf(pfile, " - %s\n", (char*)data);)
strDelete((char*)data);
data = listRemove(a,1);
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) RUN(filename, fprintf(pfile, " - %s\n", (char*)data);)
strDelete((char*)data);
data = listRemove(a,0);
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) RUN(filename, fprintf(pfile, " - %s\n", (char*)data);)
strDelete((char*)data);
NL(filename)
// Swap
RUN(filename, fprintf(pfile, "Swap\n");) NL(filename)
listAddLast(a,strings[0]); listAddLast(a,strings[1]); listAddLast(a,strings[2]);
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) NL(filename)
listSwap(a, 0, 0);
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) NL(filename)
listSwap(a, 1, 1);
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) NL(filename)
listSwap(a, 2, 2);
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) NL(filename)
listSwap(a, 0, 2);
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) NL(filename)
listSwap(a, 2, 0);
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) NL(filename)
listSwap(a, 0, 1);
listSwap(a, 1, 2);
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) NL(filename)
NL(filename)
// Agregar de mas
RUN(filename, fprintf(pfile, "Agregar 2\n");) NL(filename)
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) NL(filename)
for(int j=0; j<3; j++) {
for(int i=0; i<10; i++) {
listAddLast(a,strings[i]);
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) NL(filename)
}
}
NL(filename)
// Quitar de mas
RUN(filename, fprintf(pfile, "Quitar de mas\n");) NL(filename)
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) NL(filename)
for(int j=0; j<3; j++) {
for(int i=0; i<10; i++) {
data = listRemove(a,i);
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) RUN(filename, fprintf(pfile, " - %s\n", (char*)data);)
strDelete((char*)data);
}
}
for(int i=0; i<10; i++) {
data = listRemove(a,0);
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) RUN(filename, fprintf(pfile, " - %s\n", (char*)data);)
strDelete((char*)data);
}
NL(filename)
// Delete
RUN(filename, fprintf(pfile, "Delete\n");) NL(filename)
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) NL(filename)
for(int i=0; i<10; i++) {
listAddLast(a,strings[i]);
}
RUN(filename, fprintf(pfile, "%i - ", listGetSize(a));) RUN(filename, listPrint(a,pfile);) NL(filename)
listDelete(a);
listDelete(b);
NL(filename)
}
void test_int(char* filename) {
FILE* pfile;
int32_t ia = 13, ib = 0, ic = -20;
int32_t *a = &ia;
int32_t *b = &ib;
int32_t *c = &ic;
RUN(filename, fprintf(pfile, "== Int ==\n");) NL(filename)
// Clonar
RUN(filename, fprintf(pfile, "Clonar\n");) NL(filename)
int32_t* ac = intClone(a);
assert(a != ac); assert(*a == *ac);
RUN(filename, intPrint(ac,pfile);) NL(filename)
int32_t* bc = intClone(b);
assert(b != bc); assert(*b == *bc);
RUN(filename, intPrint(bc,pfile);) NL(filename)
int32_t* cc = intClone(c);
assert(c != cc); assert(*c == *cc);
RUN(filename, intPrint(cc,pfile);) NL(filename)
NL(filename)
//Borrar
intDelete(ac);
intDelete(bc);
intDelete(cc);
//Comparar
RUN(filename, fprintf(pfile, "Comparar\n");) NL(filename)
RUN(filename, fprintf(pfile,"intCmp(%d,%d) --> %d\n", *a, *a, intCmp(a,a));)
RUN(filename, fprintf(pfile,"intCmp(%d,%d) --> %d\n", *b, *b, intCmp(b,b));)
RUN(filename, fprintf(pfile,"intCmp(%d,%d) --> %d\n", *b, *a, intCmp(b,a));)
RUN(filename, fprintf(pfile,"intCmp(%d,%d) --> %d\n", *a, *b, intCmp(a,b));)
RUN(filename, fprintf(pfile,"intCmp(%d,%d) --> %d\n", *c, *a, intCmp(c,a));)
RUN(filename, fprintf(pfile,"intCmp(%d,%d) --> %d\n", *b, *c, intCmp(b,c));)
RUN(filename, fprintf(pfile,"intCmp(%d,%d) --> %d\n", *c, *b, intCmp(c,b));)
NL(filename)
}
void test_string(char* filename) {
FILE* pfile;
char* a = "Omega 4";
char* b = "Palaven";
char* c = "Feros";
char* n = "";
RUN(filename, fprintf(pfile, "== String ==\n");) NL(filename)
// Clonar
RUN(filename, fprintf(pfile, "Clonar\n");) NL(filename)
char* ac = strClone(a);
assert(a != ac); assert(strCmp(a,ac) == 0);
RUN(filename, fprintf(pfile, "%i - ", strLen(ac));) RUN(filename, strPrint(ac,pfile);) NL(filename)
char* nc = strClone(n);
assert(n != nc); assert(strCmp(n,nc) == 0);
RUN(filename, fprintf(pfile, "%i - ", strLen(nc));) RUN(filename, strPrint(nc,pfile);) NL(filename)
NL(filename)
//Borrar
strDelete(ac);
strDelete(nc);
//Comparar
RUN(filename, fprintf(pfile, "Comparar\n");) NL(filename)
RUN(filename, fprintf(pfile,"strCmp(%s,%s) --> %d\n", a, a, strCmp(a,a));)
RUN(filename, fprintf(pfile,"strCmp(%s,%s) --> %d\n", n, n, strCmp(n,n));)
RUN(filename, fprintf(pfile,"strCmp(%s,%s) --> %d\n", b, a, strCmp(b,a));)
RUN(filename, fprintf(pfile,"strCmp(%s,%s) --> %d\n", a, b, strCmp(a,b));)
RUN(filename, fprintf(pfile,"strCmp(%s,%s) --> %d\n", c, a, strCmp(c,a));)
RUN(filename, fprintf(pfile,"strCmp(%s,%s) --> %d\n", b, c, strCmp(b,c));)
RUN(filename, fprintf(pfile,"strCmp(%s,%s) --> %d\n", c, b, strCmp(c,b));)
char* texts[9] = {"sar","23","taaa","tbb","tix", "taaab", "taa0", "tbb", ""};
for(int i=0; i<9; i++) {
RUN(filename, fprintf(pfile,"strLen(%s) --> %d\n",texts[i],strLen(texts[i])))
for(int j=0; j<9; j++) {
RUN(filename, fprintf(pfile,"strCmp(%s,%s) --> %d\n",texts[i],texts[j],strCmp(texts[i],texts[j])))
}
}
NL(filename)
}
void test_card(char* filename){
FILE* pfile;
RUN(filename, fprintf(pfile, "== Card ==\n");) NL(filename)
RUN(filename, fprintf(pfile, "Nueva\n");) NL(filename)
int32_t v = 1;
int32_t *uno = &v;
card_t *ancho = cardNew("espada",uno);
assert(intCmp(cardGetNumber(ancho),uno) == 0);
assert(strCmp(cardGetSuit(ancho),"espada") == 0);
assert(listGetSize(cardGetStacked(ancho)) == 0);
RUN(filename, cardPrint(ancho,pfile);) NL(filename)
RUN(filename, fprintf(pfile, "Comparar\n");) NL(filename)
card_t *clone = cardClone(ancho);
assert(cardCmp(clone, ancho) == 0);
cardDelete(ancho);
cardDelete(clone);
char *suits[] = {"espada", "basto", "copa", "oro"};
array_t *mazoArray = arrayNew(TypeCard, 48);
list_t *mazoList = listNew(TypeCard);
for (int i=0; i<4; ++i){
for(int32_t j=1; j<13; ++j){
card_t *carta = cardNew(suits[i], &j);
arrayAddLast(mazoArray, (void*) carta);
listAddLast(mazoList, (void*) carta);
RUN(filename, arrayPrint(mazoArray,pfile);) NL(filename)
RUN(filename, listPrint(mazoList,pfile);) NL(filename)
cardDelete(carta);
}
}
NL(filename)
for (uint8_t i=0; i<48; ++i){
card_t *cardi = (card_t*) arrayGet(mazoArray, i);
for(uint8_t j=0; j<48; ++j){
card_t *cardj = (card_t*) arrayGet(mazoArray, j);
RUN(filename, cardPrint(cardi, pfile);)
RUN(filename, fprintf(pfile, " cmp "))
RUN(filename, cardPrint(cardj, pfile);)
RUN(filename, fprintf(pfile, " -> %d", cardCmp(cardi, cardj));) NL(filename)
}
}
NL(filename)
RUN(filename, fprintf(pfile, "Apilando\n");) NL(filename)
card_t *nuevoancho = cardNew("espada",uno);
for(int32_t j=1; j<13; ++j){
card_t *carta = arrayRemove(mazoArray, j);
cardAddStacked(nuevoancho, carta);
RUN(filename, cardPrint(nuevoancho,pfile);) NL(filename)
cardDelete(carta);
}
arrayDelete(mazoArray);
NL(filename)
for(int32_t j=13; j<26; ++j){
card_t *carta = listRemove(mazoList, j);
cardAddStacked(nuevoancho, carta);
RUN(filename, cardPrint(nuevoancho,pfile);) NL(filename)
cardDelete(carta);
}
listDelete(mazoList);
list_t *stacked = cardGetStacked(nuevoancho);
RUN(filename, cardPrint(nuevoancho,pfile);) NL(filename)
while(listGetSize(stacked) != 0){
card_t *carta = listRemove(stacked, 0);
RUN(filename, cardPrint(carta,pfile);) RUN(filename, cardPrint(nuevoancho,pfile);) NL(filename)
cardDelete(carta);
}
RUN(filename, cardPrint(nuevoancho,pfile);) NL(filename)
cardDelete(nuevoancho);
NL(filename)
}
#define TESTING
#include "game.c"
void test_game(char* filename) {
FILE* pfile;
array_t* a;
void* data;
game_t* game;
for(int i=0; i<10; i++) {
RUN(filename, fprintf(pfile, "== Game Array ==\n");) NL(filename)
game = buildGameWithArray(i);
RUN(filename, playGame(game, pfile);) NL(filename)
gameDelete(game);
RUN(filename, fprintf(pfile, "== Game List ==\n");) NL(filename)
game = buildGameWithList(i);
RUN(filename, playGame(game, pfile);) NL(filename)
gameDelete(game);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment