main( ){
}
C言語で別のプログラムを作成し、system 関数などを使って最初のプログラムを実行し、その出力を読み取って比較する方法です。
int main() {
char expected_output[] = "hello, world\n";
char actual_output[100]; // 十分なバッファサイズを確保
// hello プログラムを実行し、出力を actual_output にリダイレクト(環境依存)
// これは非常に簡略化された概念であり、実際にはパイプ処理などが必要になります
#ifdef _WIN32
sprintf(command, "hello.exe > temp_output.txt");
#else
sprintf(command, "./hello > temp_output.txt");
#endif
system(command);
FILE *fp = fopen("temp_output.txt", "r");
if (fp != NULL) {
fgets(actual_output, sizeof(actual_output), fp);
fclose(fp);
remove("temp_output.txt"); // 一時ファイルを削除
} else {
printf("エラー:一時ファイルのオープンに失敗しました\n");
return 1;
}
if (strcmp(actual_output, expected_output) == 0) {
return 0;
} else {
printf("テスト失敗: 期待された出力 '%s'、実際の出力 '%s'\n", expected_output, actual_output);
return 1;
}
}