简体   繁体   English

做测试用例?

[英]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. 我是C语言的新手,我正在尝试制作测试用例以测试节点交换,但不知道如何制作测试用例。 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 swap函数是错误的,像这样修改它

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 那么您正在更改值,因为您无法更改这些参数所指向的ab

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)` 如果要更改a`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. 从函数a返回时,对foo的调用不会更改参数,只是将其复制然后修改,然后仍具有其原始值。

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

changes not what a points to but the value, but a still points to the same spot 变化不是a点,而是价值,但仍指向同一地点

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". #东西是制作C时的指令。 # #define TEST表示“我们处于测试模式”。 #ifndef TEST means "if we're not testing". #ifndef TEST表示“如果我们不进行测试”。 #ifdef TEST means "if we're testing". #ifdef TEST表示“如果我们正在测试”。 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. 要小心的是#define全系标配所有之前#ifdef#ifndef线。

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. 如果这些方法对您没有帮助,则应签出GDB调试代码。 If you're using Ubuntu, I think it's in the software center. 如果您使用的是Ubuntu,我认为它在软件中心中。

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. 好吧,我不会告诉您,因为从长远来看,这不会帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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