test.c

Discussion in 'C Programming' started by jacob navia, Oct 4, 2009.

  1. jacob navia

    jacob navia Guest

    #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;
    }
     
    jacob navia, Oct 4, 2009
    #1
    1. Advertising

  2. jacob navia

    jacob navia Guest

    Malcolm McLean a écrit :
    > "jacob navia" <> wrote in message
    >> sum = 0.0;
    >> for (i=0; i<1000;i++) {
    >> pd = l->lpVtbl->GetElement(l,i);
    >> sum += *pd;
    >> }

    >
    > Here's how ypu would do it without the container library
    > sum = 0.0;
    > for(i=0;i<N;i++)
    > sum += array;
    >
    > you see the problem? It makes something as simple as stepping through an
    > array cluntsy.
    >
    >


    Sure, why use a container when you can use an array?

    There several answers to your remark.

    (1) lcc-win implements operator overloading. With it, the syntax is the
    same old syntax we know. In that context your remark is not valid

    (2) The problem with arrays, is that if you want to insert an item
    you have to do some work. This work is done for you in the arraylist container

    (3) the problem with arrays is that adding items means resizing the array.
    This is done for you with the arraylist container.

    (4) The problem with arrays is that if you want to replace them with a list
    your code needs to be changed. The container library is designed to
    minimize that kind of changes and make changing a container by another
    much more transparent.

    But if you want to use an array (and nobody is proposing you to drop them!)
    use them of course.
     
    jacob navia, Oct 4, 2009
    #2
    1. Advertising

  3. jacob navia

    bartc Guest

    "jacob navia" <> wrote in message
    news:haaogs$h3i$...
    > Malcolm McLean a écrit :
    >> "jacob navia" <> wrote in message
    >>> sum = 0.0;
    >>> for (i=0; i<1000;i++) {
    >>> pd = l->lpVtbl->GetElement(l,i);
    >>> sum += *pd;
    >>> }

    >>
    >> Here's how ypu would do it without the container library
    >> sum = 0.0;
    >> for(i=0;i<N;i++)
    >> sum += array;
    >>
    >> you see the problem? It makes something as simple as stepping through an
    >> array cluntsy.
    >>
    >>

    >
    > Sure, why use a container when you can use an array?
    >
    > There several answers to your remark.
    >
    > (1) lcc-win implements operator overloading. With it, the syntax is the
    > same old syntax we know. In that context your remark is not valid


    So this example is a selling point for lcc-win32, so that we don't have to
    write:

    pd = l->lpVtbl->GetElement(l,i);
    sum += *pd;

    But presumably we can also write:

    sum += *(l->lpVtbl->GetElement(l,i));

    I'm worried though about "l" having to appear twice (you don't notice with
    this example because l is so short). Why can't we just write:

    sum += *GetElement(l,i); ?

    That's what people would expect when they can't use overloaded ops. To be
    more precise, they would expect to write:

    sum += GetElement(l,i);

    (Your use of the "GetElement" name being a trifle misleading.)

    --
    Bartc
     
    bartc, Oct 4, 2009
    #3
  4. jacob navia

    jacob navia Guest

    bartc a écrit :
    >
    > So this example is a selling point for lcc-win32, so that we don't have to
    > write:
    >
    > pd = l->lpVtbl->GetElement(l,i);
    > sum += *pd;
    >
    > But presumably we can also write:
    >
    > sum += *(l->lpVtbl->GetElement(l,i));
    >
    > I'm worried though about "l" having to appear twice (you don't notice with
    > this example because l is so short). Why can't we just write:
    >
    > sum += *GetElement(l,i); ?
    >
    > That's what people would expect when they can't use overloaded ops. To be
    > more precise, they would expect to write:
    >
    > sum += GetElement(l,i);
    >
    > (Your use of the "GetElement" name being a trifle misleading.)
    >


    We could have done this with simple APIs but the problem with an API like that
    is that you can't change it dynamically.

    Since we use function pointers, we can at any time change the API (and maintaining the
    interface) with a pointer to ANOTHER function that does the same but modifies a bit the
    behavior to implement feature "XYZ" not included in the existing functions.

    Another point is that the name space pollution is much smaller with this
    approach... I am 100% sure that many programs implement a "GetElement" function.

    We would need to use a prefix like

    ContainerLib_GetElement or similar.

    You can (of course) define a macro

    #define CLGetElement(list,index) list->lpVtbl->GetElement(list,index)

    Then you write

    sum += CLGetElement(l,i);
     
    jacob navia, Oct 4, 2009
    #4
  5. jacob navia

    Seebs Guest

    On 2009-10-04, Malcolm McLean <> wrote:
    > "jacob navia" <> wrote in message
    >> sum = 0.0;
    >> for (i=0; i<1000;i++) {
    >> pd = l->lpVtbl->GetElement(l,i);
    >> sum += *pd;
    >> }


    > Here's how ypu would do it without the container library
    > sum = 0.0;
    > for(i=0;i<N;i++)
    > sum += array;


    > you see the problem? It makes something as simple as stepping through an
    > array cluntsy.


    I basically agree, although I'd point out that you could do

    sum += *(l->lpVtbl->GetElement(l,i));

    Jacob: No one is going to standardize a C interface which uses CamelCase.

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Oct 4, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sting
    Replies:
    5
    Views:
    1,651
    Natty Gur
    Jun 8, 2004
  2. Robert Allan Schwartz
    Replies:
    7
    Views:
    395
    Marco Manfredini
    Aug 13, 2004
  3. Chris
    Replies:
    1
    Views:
    680
    Chris
    May 11, 2006
  4. Edvard Majakari
    Replies:
    4
    Views:
    701
    Edvard Majakari
    Feb 25, 2005
  5. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    748
    Skybuck Flying
    Oct 4, 2009
Loading...

Share This Page