简体   繁体   中英

Making Test cases?

I am new to C and I am trying to make test cases to test node swapping, but dont know how can i make a test case. If someone could give me an example, would be great. Thx

Could someone tell me what i am doing wrong in my swap function as the values dont get swaped?

#include <stdio.h>
#include <stdlib.h>


 struct lnode {
    int data;
    struct lnode* next;
 };


 void swap(int* a, int* b );

 int main()
    {
      int x = 10;
  int y = 14;

  swap(&x, &y);
  swapNodes(x, y);
  getchar();
  return 0;
    }

   void swap(int* a, int* b )
  {
  int* temp;
  temp = a;
  a = b;
  b = temp;

  printf("x= %d  y= %d",*a,*b);
   }

   void swapNodes(struct lnode* n1, struct lnode* n2)
   {
    struct lnode* temp;
    temp = n1->next;
    n1->next = n2;
    n2->next = temp;
   }

Start with the simplest test case. You need two nodes to do a node swap. Just declare two node structures, assign each node a different value, swap the nodes, and print the result. Something like this:

struct lnode nodeA, nodeB;

nodeA.data = 1;
nodeB.data = 2;

swapNodes(&nodeA, &nodeB);

printf("nodeA has value %d, should be 2\n", nodeA.data);
printf("nodeB has value %d, should be 1\n", nodeB.data);

the swap function is wrong, modify it like this

void swap(int* a, int* b )
{
  int temp;
  temp = *a;
  *a = *b;
  *b = temp;
  printf("x= %d  y= %d",*a,*b);
}

then you are changing the values, since you cannot change what a and b are pointing to with those arguments

the reason why it didn't work is this

void swap(int* a, int* b )
{
  int* temp; // pointer 
  temp = a;  // temppointing to same as a is pointing to

        +------------+
temp -> | some value |
        +------------+

  a = b;   // a now pointing to same as b is pointing to  

     +------------------+
a -> | some other value |
     +------------------+

  b = temp;  // b now pointing to same as temp pointing to, a

     +------------+
b -> | some value |
     +------------+

but when you return from the function the pointers remain the same. if you want to change what a and `b point to you need to have arguments swap( int **a, int **b)`

similar to

int foo(int a) {
a = 123;
}

int a = 1;
foo(a);

the call to foo does not change the argument, it is just copied and then modified, when returning from the function a has still its original value.

int foo(int* a)
{
  *a = 1;
}

changes not what a points to but the value, but a still points to the same spot

int foo(int**a )
{
  *a = malloc(sizeof(int)); // changes where a points to
...
}

Making tests is pretty easy. Using preprocessor macros, you can do it without flinching. For example, you have:

int main()
{
  int x = 10;
  int y = 14;

  swap(&x, &y);
  swapNodes(x, y);
  getchar();
  return 0;
}

Make it

#define TEST
#ifndef TEST
int main()
{ //regular running
  int x = 10;
  int y = 14;

  swap(&x, &y);
  swapNodes(x, y);
  getchar();
  return 0;
}
#endif
#ifdef TEST
    int main()
{
    //test your code!
    return 0;
}
#endif

The # things are instructions when making C. #define TEST means "we're in testing mode". #ifndef TEST means "if we're not testing". #ifdef TEST means "if we're testing". We could have used

} //end regular main
#else
int main() //begin test main

But it doesn't really matter. Be careful that the #define line comes before all of your #ifdef and #ifndef lines.

If that still doesn't help, you might want to try using preprocessor macros to cover print statements

#define DEBUG
#ifdef DEBUG
#include <stdio.h>
#endif

...

void myFunc(){
    int x = 0;
    #ifdef DEBUG
    printf("%d", x);
    #endif
}

If you really want to, you can define things during compilation (which is considered fancier).

gcc -DDEBUG -DTEST -o swap_test swap.c

If those don't help you, you should check out GDB to debug your code. If you're using Ubuntu, I think it's in the software center.

And as for what's actually wrong with your code? Well, I'm not going to tell you, because that's not going to help you out in the long run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM