aboutsummaryrefslogtreecommitdiffstats
path: root/test/regression/switch3.c
blob: df3ad747218ff2c3aab8056aebc5e0f3ad6cf0f1 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <stdlib.h>
#include <stdio.h>

/* Unstructured switch statements */

/* Duff's device */

void send(char * to, char * from, int count)
{
  register int n = (count + 7) / 8;
  switch (count % 8) {
    case 0: do { *to++ = *from++;
    case 7:      *to++ = *from++;
    case 6:      *to++ = *from++;
    case 5:      *to++ = *from++;
    case 4:      *to++ = *from++;
    case 3:      *to++ = *from++;
    case 2:      *to++ = *from++;
    case 1:      *to++ = *from++;
            } while (--n > 0);
  }
}

/* OpenBSD, usr.bin/locale */

void quotestring(char * value, int double_quoted)
{
  char c;
  while ((c = *value++) != '\0')
    switch (c) {
    case ' ': case '\t': case '\n': case '\'':
    case '(': case ')': case '<': case '>':
    case '&': case ';': case '|': case '~':
      if (!double_quoted)
    case '"': case '\\': case '$': case '`':
        putchar('\\');
    default:
      putchar(c);
      break;
    }
}

/* Simon Tatham's coroutines */

int nextchar(void)
{
  static unsigned char input[] = {
    'a', 'b', 0xFF, 3, 'c', 'd', 0xFF, 2, 'e', '\n', 0
  };
  static unsigned char * next = input;
  if (*next == 0)
    return EOF;
  else
    return *next++;
}

#define crBegin static int state=0; switch(state) { case 0:
#define crReturn(x) do { state=__LINE__; return x; \
                         case __LINE__:; } while (0)
#define crFinish }

int decompressor(void) {
  static int c, len;
  crBegin;
  while (1) {
    c = nextchar();
    if (c == EOF)
      break;
    if (c == 0xFF) {
      len = nextchar();
      c = nextchar();
      while (len--)
        crReturn(c);
    } else
      crReturn(c);
  }
  crReturn(EOF);
  crFinish;
}

/* Pigeon's device */

static enum { REVDFWDT, FORWARD, REVERSE } sorting_mode;

#define DAYOF(stamp)  ((stamp) / 10000)
#define TIMEOF(stamp) ((stamp) % 10000)

static inline int intcmp(unsigned int x, unsigned int y)
{
  if (x < y) return -1;
  if (x > y) return 1;
  return 0;
}

int lfdcmp(const void *a, const void *b) {
  unsigned int ta = *((unsigned int *) a);
  unsigned int tb = *((unsigned int *) b);
                  /* Isn't C a wonderful language? */
  switch (sorting_mode) {
    case REVDFWDT: if (DAYOF(ta) == DAYOF(tb)) {
    case FORWARD :     return intcmp(ta, tb);
                   } else {
    case REVERSE :     return intcmp(tb, ta);
                   }
  }
  return 0;
}

void testpigeon(int mode)
{
  unsigned int data[6] = {
    10001, 10004, 20022, 20002, 30008, 30007
  };
  sorting_mode = mode;
  qsort(data, 6, sizeof(unsigned int), lfdcmp);
  for (int i = 0; i < 6; i++) printf("%5u ", data[i]);
  printf("\n");
}

/* Delicate cases: unstructured switch containing
     early break
     loop w/ break
     regular switch
*/

void torture(int a, int b)
{
  switch(a) {
    L1: printf("L1 "); /*fallthrough*/
    L2: printf("L2 "); break;
    case 0:
      printf("0 "); /*fallthrough*/
    case 1:
      printf("1 "); break;
    case 2:
      printf("2 "); goto L1;
    case 3:
    L3:
      printf("3 "); goto L2;
    case 4:
      printf("4 "); goto L3;
    case 5:
      while (1) { printf("5"); b--; if (b <= 0) { printf(" "); break; }};
      break;
    case 6:
      switch (b) {
        case 1: printf("60"); break;
        case 2: printf("61"); break;
        default: printf("6X");
      }
      printf("Y ");
      break;
    default:
      printf("? ");
  }
}

int main()
{
  char dest[16];
  send(dest, "0123456789AB", 13);
  printf("%s\n", dest);

  quotestring("(ab$cd)", 0); putchar('\n');
  quotestring("(ab$cd)", 1); putchar('\n');

  int c;
  while ((c = decompressor()) != EOF) putchar(c);

  testpigeon(FORWARD);
  testpigeon(REVERSE);
  testpigeon(REVDFWDT);

  torture(0, 0); torture(1, 0); torture(2, 0); torture(3, 0); torture(4, 0);
  printf("\n");
  for (int b = 1; b <= 3; b++) torture(5, b);
  printf("\n");
  for (int b = 1; b <= 3; b++) torture(6, b);
  printf("\n");

  return 0;
}