簡體   English   中英

如何打印一個function的輸入和output進行測試

[英]How to print the input and output of a function for testing

我想測試我已經創建的函數,並想將 function 的輸入和 output 打印到 a.out,如下例所示

樣本 function

int check_len(int color, int destination, int origin, int len){}

樣本輸出/打印

check_len(GREEN, 3, 6, 4): VALID

我想你可以用普通的printf()和一個專用的 function 來完成它,它將顏色編號轉換為一個字符串。 您也可以使用測試庫,例如google test

const char *colors[] = {"GREEN", "BLUE", "YELLOW", "RED"};

const char *get_color(int color_index)
{
   int size = sizeof(colors) / sizeof(colors[0]);

   if (color_index >= size)
   {
      /* handle out of bounds error */
   }

   return colors[color_index];
}


void test(int color, int dest, int org, int len)
{

   int retv = check_len(color, dest, org, len);
   /* lets assume 0 if for green */
   printf("check_len(%s, %d, %d, %d): %s\n", get_color(color), dest, org, len, retv == 1 ? "VALID" : "INVALID");
}

這里,使用條件表達式來檢查返回值是否是你要求的,在這種情況下,如果你想1作為check_len的返回值,則打印"VALID"否則"INVALID"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM