Advertisement
Guest User

trash.c

a guest
Jun 5th, 2016
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.74 KB | None | 0 0
  1. #include <check.h>
  2. #include "tmc-check.h"
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <math.h>
  8. #include "../src/source.h"
  9.  
  10.  
  11. START_TEST(test_simple_sum)
  12. {
  13.      FILE *mock_input = freopen("mockinput", "w+", stdin);
  14.      fputs("6 8", mock_input);
  15.      fseek(mock_input, 0, SEEK_SET);
  16.      freopen("mockoutput", "w", stdout);
  17.      simple_sum();
  18.      fflush(stdout);
  19.      FILE *fp = fopen("mockoutput", "r");
  20.      char str [100];
  21.      memset(str, 0, sizeof(str));
  22.      fgets(str, 100, fp);
  23.      char *ref = "6 + 8 = 14\n";
  24.      char infostr[1000] = "";
  25.      int ret = mycompare(str, ref, infostr);
  26.      fail_unless(!ret, "[Task 1.2] When giving input \"6 8\", your output:\n%s\nReference output:\n%s\nReason: %s\n",
  27.             str, ref, infostr);
  28.      fclose(fp);
  29.  
  30.      mock_input = freopen("mockinput", "w+", stdin);
  31.      fputs("10200 20031", mock_input);
  32.      fseek(mock_input, 0, SEEK_SET);
  33.      freopen("mockoutput", "w", stdout);
  34.      simple_sum();
  35.      fflush(stdout);
  36.      fp = fopen("mockoutput", "r");
  37.      memset(str, 0, sizeof(str));
  38.      fgets(str, 100, fp);
  39.      ref = "10200 + 20031 = 30231\n";
  40.      infostr[0] = 0;
  41.      ret = mycompare(str, ref, infostr);
  42.      fail_unless(!ret, "[Task 1.2] When giving input \"10200 20031\", your output:\n%s\nReference output:\n%s\nReason: %s\n",
  43.             str, ref, infostr);
  44.      fclose(fp);
  45. }
  46. END_TEST
  47.  
  48.  
  49. struct t13_st {
  50.     float a;
  51.     char op;
  52.     float b;
  53.     char res[16];
  54. };
  55.  
  56. void simple_math_helper2(float a, char op, float b, char *res)
  57. {
  58.     char inp[100];
  59.     sprintf(inp, "%.1f %c %.1f", a, op, b);
  60.     FILE *mock_input = freopen("mockinput", "w+", stdin);
  61.     fputs(inp, mock_input);
  62.     fseek(mock_input, 0, SEEK_SET);
  63.    
  64.     freopen("mockoutput", "w", stdout);
  65.     simple_math();
  66.     fflush(stdout);
  67.  
  68.     FILE *fp = fopen("mockoutput", "r");
  69.     fgets(res, 99, fp);
  70.     fclose(fp);
  71.     remove_newline(res);
  72. }
  73.  
  74.  
  75. START_TEST(test_simple_math)
  76. {
  77. struct t13_st ts[5] =
  78. {{ 1.5, '+', 2.4, "3.9" },
  79. { 4.3, '-', 0.2, "4.1" },
  80. { 2.0, '*', 1.2, "2.4" },
  81. { 5.0, '/', 2.0, "2.5" },
  82. { 1.0, 'l', 2.0, "ERR" }};
  83.  
  84. for (int i = 0; i < 5; i++) {
  85. char stu[100];
  86. memset(stu, 0, 100);
  87. simple_math_helper2(ts[i].a, ts[i].op, ts[i].b, stu);
  88.  
  89. if (strcmp(stu, ts[i].res)) {
  90. fail("[Task 1.3] With input \"%.1f %c %.1f\" your function printed '%s'. Should have printed '%s'",
  91. ts[i].a, ts[i].op, ts[i].b, stu, ts[i].res);
  92. }
  93. }
  94. }
  95. END_TEST
  96.  
  97.  
  98.  
  99.  
  100. int main(int argc, const char *argv[])
  101. {
  102.     srand((unsigned)time(NULL));
  103.     Suite *s = suite_create("Test-05-calc");
  104.  
  105.     tmc_register_test(s, test_simple_sum, "M1.05.1");
  106.     tmc_register_test(s, test_simple_math, "M1.05.2");
  107.  
  108.     return tmc_run_tests(argc, argv, s);
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement