Skip to content

Instantly share code, notes, and snippets.

@jucie
Last active January 7, 2020 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jucie/389cc88c0378f8ac4712d361abd27e44 to your computer and use it in GitHub Desktop.
Save jucie/389cc88c0378f8ac4712d361abd27e44 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
void funcao_correta(FILE *out, int idade) {
fprintf(out,"a pessoa tem %d anos", idade);
}
void funcao_incorreta(FILE *out, int idade) {
fprintf(out,"a pessoa tem %d anos", idade*2);
}
int main(int argc, const char *argv) {
const char O_CERTO[] = "a pessoa tem 3 anos";
char buffer[32];
FILE *f;
f = fopen("NUL","wt"); /* para unix troque o nome de arquivo por /dev/null */
if ( !f ) {
fputs("Falhou abrindo arquivo", stderr);
return 1;
}
setvbuf(f, buffer, _IOFBF, sizeof buffer);
funcao_correta(f, 3);
if (memcmp(buffer, O_CERTO, strlen(O_CERTO)) != 0) {
fputs("funcao_correta() nao produziu o resultado correto\n", stderr);
}
setvbuf(f, buffer, _IOFBF, sizeof buffer);
funcao_incorreta(f, 3);
if (memcmp(buffer, O_CERTO, strlen(O_CERTO)) != 0) {
fputs("funcao_incorreta() nao produziu o resultado correto\n", stderr);
}
fclose(f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment