J
jacob navia
#include "containers.h"
#include <stdio.h>
int testList(void)
{
List *l = newList(sizeof(double)),*l1,*l2;
double d,*pd,sum;
int isum=0,msum=0,i,errors=0;
for (i=0; i<1000;i++) {
d = i;
isum += i;
l->lpVtbl->Add(l,(void *)&d);
}
if (l->lpVtbl->GetCount(l) != 1000) {
printf("error\n");
errors++;
}
sum = 0.0;
for (i=0; i<1000;i++) {
pd = l->lpVtbl->GetElement(l,i);
sum += *pd;
}
printf ("%g %d\n",sum,isum);
sum=0;
for (i=0; i< 500;i++) {
list_element *le;
le = l->lpVtbl->Pop(l);
msum += i;
sum += *(double *)le->Data;
free(le);
}
printf ("%g %d\n",sum,msum);
if ( l->lpVtbl->GetCount(l) != 500) {
printf("error in list count\n");
errors++;
}
printf("Fisrt = %g, last =%g\n",
*(double *)l->lpVtbl->GetElement(l,0),
*(double *)l->lpVtbl->GetElement(l,499));
l1 = l->lpVtbl->GetRange(l,0,499);
if (!l->lpVtbl->Equal(l,l1))
printf("error\n"),errors++;
l2 = l->lpVtbl->Copy(l);
if (!l2->lpVtbl->Equal(l2,l1))
printf("error\n"),errors++;
for (i=0; i<l1->lpVtbl->GetCount(l); i++) {
d = *(double *)l->lpVtbl->GetElement(l,i);
if (d != i+500) {
printf("error\n");
errors++;
}
}
return errors;
}
static void PrintArrayList(ArrayList *AL)
{
int i;
printf("Count %d, Capacity %d\n",AL->count,AL->capacity);
for (i=0; i<AL->count;i++) {
printf("%s\n",AL->lpVtbl->GetElement(AL,i));
}
printf("\n");
}
int testArrayList(void)
{
int errors=0;
ArrayList *AL = newArrayList(sizeof(void *),10);
char *p;
AL->lpVtbl->Add(AL,"Martin");
AL->lpVtbl->Insert(AL,"Jakob");
if (!AL->lpVtbl->Contains(AL,"Martin")) {
printf("error, string not found!\n");
errors++;
}
printf("Count should be 2, is %d\n",AL->lpVtbl->GetCount(AL));
PrintArrayList(AL);
AL->lpVtbl->InsertAt(AL,1,"Position 1");
AL->lpVtbl->InsertAt(AL,2,"Position 2");
PrintArrayList(AL);
AL->lpVtbl->Remove(AL,"Jakob");
PrintArrayList(AL);
AL->lpVtbl->Push(AL,"pushed");
PrintArrayList(AL);
AL->lpVtbl->Pop(AL);
PrintArrayList(AL);
p = AL->lpVtbl->GetElement(AL,1);
printf("Item position 1:%s\n",p);
PrintArrayList(AL);
return errors;
}
#include <stdio.h>
static void PrintStringCollection(StringCollection *SC){
int i;
printf("Count %d, Capacity %d\n",SC->count,SC->capacity);
for (i=0; i<SC->count;i++) {
printf("%s\n",SC->lpVtbl->GetElement(SC,i));
}
printf("\n");
}
int testArrayCollection(void){
StringCollection *SC = newStringCollection(10);
char *p;
SC->lpVtbl->Add(SC,"Martin");
SC->lpVtbl->Insert(SC,"Jakob");
if (!SC->lpVtbl->Contains(SC,"Martin")) {
printf("error, string not found!\n");
}
printf("Count should be 2, is %d\n",SC->lpVtbl->GetCount(SC));
PrintStringCollection(SC);
SC->lpVtbl->InsertAt(SC,1,"Position 1");
SC->lpVtbl->InsertAt(SC,2,"Position 2");
PrintStringCollection(SC);
SC->lpVtbl->Remove(SC,"Jakob");
PrintStringCollection(SC);
SC->lpVtbl->Push(SC,"pushed");
PrintStringCollection(SC);
SC->lpVtbl->Pop(SC);
PrintStringCollection(SC);
p = SC->lpVtbl->GetElement(SC,1);
printf("Item position 1:%s\n",p);
PrintStringCollection(SC);
return 0;
}
int main(void)
{
int errors = testArrayCollection();
errors += testArrayList();
errors += testList();
return errors;
}
#include <stdio.h>
int testList(void)
{
List *l = newList(sizeof(double)),*l1,*l2;
double d,*pd,sum;
int isum=0,msum=0,i,errors=0;
for (i=0; i<1000;i++) {
d = i;
isum += i;
l->lpVtbl->Add(l,(void *)&d);
}
if (l->lpVtbl->GetCount(l) != 1000) {
printf("error\n");
errors++;
}
sum = 0.0;
for (i=0; i<1000;i++) {
pd = l->lpVtbl->GetElement(l,i);
sum += *pd;
}
printf ("%g %d\n",sum,isum);
sum=0;
for (i=0; i< 500;i++) {
list_element *le;
le = l->lpVtbl->Pop(l);
msum += i;
sum += *(double *)le->Data;
free(le);
}
printf ("%g %d\n",sum,msum);
if ( l->lpVtbl->GetCount(l) != 500) {
printf("error in list count\n");
errors++;
}
printf("Fisrt = %g, last =%g\n",
*(double *)l->lpVtbl->GetElement(l,0),
*(double *)l->lpVtbl->GetElement(l,499));
l1 = l->lpVtbl->GetRange(l,0,499);
if (!l->lpVtbl->Equal(l,l1))
printf("error\n"),errors++;
l2 = l->lpVtbl->Copy(l);
if (!l2->lpVtbl->Equal(l2,l1))
printf("error\n"),errors++;
for (i=0; i<l1->lpVtbl->GetCount(l); i++) {
d = *(double *)l->lpVtbl->GetElement(l,i);
if (d != i+500) {
printf("error\n");
errors++;
}
}
return errors;
}
static void PrintArrayList(ArrayList *AL)
{
int i;
printf("Count %d, Capacity %d\n",AL->count,AL->capacity);
for (i=0; i<AL->count;i++) {
printf("%s\n",AL->lpVtbl->GetElement(AL,i));
}
printf("\n");
}
int testArrayList(void)
{
int errors=0;
ArrayList *AL = newArrayList(sizeof(void *),10);
char *p;
AL->lpVtbl->Add(AL,"Martin");
AL->lpVtbl->Insert(AL,"Jakob");
if (!AL->lpVtbl->Contains(AL,"Martin")) {
printf("error, string not found!\n");
errors++;
}
printf("Count should be 2, is %d\n",AL->lpVtbl->GetCount(AL));
PrintArrayList(AL);
AL->lpVtbl->InsertAt(AL,1,"Position 1");
AL->lpVtbl->InsertAt(AL,2,"Position 2");
PrintArrayList(AL);
AL->lpVtbl->Remove(AL,"Jakob");
PrintArrayList(AL);
AL->lpVtbl->Push(AL,"pushed");
PrintArrayList(AL);
AL->lpVtbl->Pop(AL);
PrintArrayList(AL);
p = AL->lpVtbl->GetElement(AL,1);
printf("Item position 1:%s\n",p);
PrintArrayList(AL);
return errors;
}
#include <stdio.h>
static void PrintStringCollection(StringCollection *SC){
int i;
printf("Count %d, Capacity %d\n",SC->count,SC->capacity);
for (i=0; i<SC->count;i++) {
printf("%s\n",SC->lpVtbl->GetElement(SC,i));
}
printf("\n");
}
int testArrayCollection(void){
StringCollection *SC = newStringCollection(10);
char *p;
SC->lpVtbl->Add(SC,"Martin");
SC->lpVtbl->Insert(SC,"Jakob");
if (!SC->lpVtbl->Contains(SC,"Martin")) {
printf("error, string not found!\n");
}
printf("Count should be 2, is %d\n",SC->lpVtbl->GetCount(SC));
PrintStringCollection(SC);
SC->lpVtbl->InsertAt(SC,1,"Position 1");
SC->lpVtbl->InsertAt(SC,2,"Position 2");
PrintStringCollection(SC);
SC->lpVtbl->Remove(SC,"Jakob");
PrintStringCollection(SC);
SC->lpVtbl->Push(SC,"pushed");
PrintStringCollection(SC);
SC->lpVtbl->Pop(SC);
PrintStringCollection(SC);
p = SC->lpVtbl->GetElement(SC,1);
printf("Item position 1:%s\n",p);
PrintStringCollection(SC);
return 0;
}
int main(void)
{
int errors = testArrayCollection();
errors += testArrayList();
errors += testList();
return errors;
}