uint8_t command_read(const FILE* const in)
から読み取るC関数がありますin
。関数の単体テストを書きたいのですが。FILE*
ファイルシステムとの対話を避けたいので、テスト用のメモリ内にを作成することは可能ですか?そうでない場合、代替手段は何ですか?
1 に答える
9
テスト用にメモリ内にFILE*を作成することは可能ですか?
もちろん。書くために:
char *buf;
size_t sz;
FILE *f = open_memstream(&buf, &sz);
// do stuff with `f`
fclose(f);
// here you can access the contents of `f` using `buf` and `sz`
free(buf); // when done
これはPOSIXです。ドキュメント。
読むために:
char buf[] = "Hello world! This is not a file, it just pretends to be one.";
FILE *f = fmemopen(buf, sizeof(buf), "r");
// read from `f`, then
fclose(f);
サイドノート:
テストがファイルシステムと対話する必要がないようにしたいと思います。
なんで?
于 2013-03-23T21:35:05.870 に答える