summaryrefslogtreecommitdiff
path: root/ex1-13.c
blob: ff2d53dad5b460d37b672d7b1fa627c9155a580c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

#define max_word 100
#define max(a, b) ((a) > (b) ? (a) : (b))

typedef enum _state {in,out} state;
bool is_word(char c) {
  return c != ' ' 
   && c != '\t' 
   && c != '\n';
}

/* print a histogram of word lengths
   read from input
   print histo to output
   print errors warnings to output_err */
int whisto(FILE* input, FILE* output, FILE* output_err) {
  uint32_t lens[max_word+1]={0};
  state s = out;
  uint32_t cl = 0;
  while(true) {
    int c = fgetc(input);
    if (is_word(c)){
      if (s == out) {
        s = in;
        cl=1;
      } else { 
        cl++;
      }
    } else {
      if (s == in) {
        s = out;
        if (cl > max_word) {
          printf("cl=%d\n", cl);
          fprintf(output_err, "word too long\n");
          fflush(output_err);
          return 1;
        }
        lens[cl]++;
      } else {
        // do nothing I guess
      } 
    }

    if (c == EOF) {
      break;
    }
  }
  // Do 2 passes to find the longest word length with some data in it
  uint16_t mx = 0;
  for(uint16_t i=1;
      i<=max_word;
      i ++)
    mx = max(mx, lens[i]>0 ? i : 0);
  for(uint16_t i=1;
      i<=mx;
      i ++) {
    fprintf(output, "%2d: ",i);
    for(uint16_t j=0;
        j<lens[i];
        j++)
      fputc('-', output);
    fputc('\n', output);
  }
  fflush(output);
  return 0;
}

#ifndef test

int main(void) {
    return whisto(stdin, stdout, stderr);
}

#else

#include "CuTest.h"

void do_test(CuTest* tc, char* input, const char* expected, const char* err_expected, int res_expected) {
    char buf[100] = {0};
    char err_buf[100] = {0};
    FILE* tin = fmemopen(input, strlen(input), "r");
    FILE* tout = fmemopen(buf, 100, "w");
    FILE* terr = fmemopen(err_buf, 100, "w");
    int res = whisto(tin, tout, terr);
    CuAssertIntEquals(tc, res_expected, res);
    CuAssertStrEquals(tc, expected, buf);
    CuAssertStrEquals(tc, err_expected, err_buf);
}
void test_whisto_empty(CuTest* tc) {
    do_test(tc, "", "", "", 0);
}
void test_whisto_oneword(CuTest* tc) {
    do_test(tc, "fo", 
      " 1: \n"
      " 2: -\n", 
      "", 
      0);
}
void test_whisto_leadtrailspace(CuTest* tc) {
    do_test(tc, "  fuzz fizz   bar  ", 
      " 1: \n"
      " 2: \n"
      " 3: -\n"
      " 4: --\n", 
      "",
      0);
}
void test_whisto_overflow(CuTest* tc) {
    // 101 characters
    do_test(tc, "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901",
      "",
      "word too long\n",
      1);
}
CuSuite* whisto_suite() {
    CuSuite* suite = CuSuiteNew();
    SUITE_ADD_TEST(suite, test_whisto_empty);
    SUITE_ADD_TEST(suite, test_whisto_oneword);
    SUITE_ADD_TEST(suite, test_whisto_leadtrailspace);
    SUITE_ADD_TEST(suite, test_whisto_overflow);
    return suite;
}
int main(void) {
    CuString* output = CuStringNew();
    CuSuite* suite = CuSuiteNew();
    CuSuiteAddSuite(suite, whisto_suite());
    CuSuiteRun(suite);
    CuSuiteSummary(suite, output);
    CuSuiteDetails(suite, output);
    printf("%s\n", output->buffer);
    return 0;
}

#endif