brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.3 KiB · 0d31082 Raw
248 lines · cpp
1// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s2//3// UNSUPPORTED: target={{.*(linux|solaris).*}}, darwin4 5#include <ctype.h>6#include <err.h>7#include <inttypes.h>8#include <stdint.h>9#include <stdio.h>10#include <stdlib.h>11#include <vis.h>12 13void test_vis() {14  char visout[5];15  int ch = toascii(0x1);16  vis(visout, ch, VIS_SAFE | VIS_NOSLASH, 0);17  printf("vis: %s\n", visout);18}19 20void test_nvis() {21  char visout[5];22  int ch = toascii(0x2);23  nvis(visout, sizeof visout, ch, VIS_SAFE | VIS_NOSLASH, 0);24  printf("nvis: %s\n", visout);25}26 27void test_strvis() {28  char visout[5];29  strvis(visout, "\3", VIS_SAFE | VIS_NOSLASH);30  printf("strvis: %s\n", visout);31}32 33void test_stravis() {34  char *visout;35  stravis(&visout, "\4", VIS_SAFE | VIS_NOSLASH);36  printf("stravis: %s\n", visout);37  free(visout);38}39 40void test_strnvis() {41  char visout[5];42  strnvis(visout, sizeof visout, "\5", VIS_SAFE | VIS_NOSLASH);43  printf("strnvis: %s\n", visout);44}45 46void test_strvisx() {47  char visout[5];48  char src[] = "\6";49  strvisx(visout, src, sizeof src - 1 /* skip final \0 */,50          VIS_SAFE | VIS_NOSLASH);51  printf("strvisx: %s\n", visout);52}53 54void test_strnvisx() {55  char visout[5];56  char src[] = "\1";57  strnvisx(visout, sizeof visout, src, sizeof src - 1 /* skip final \0 */,58           VIS_SAFE | VIS_NOSLASH);59  printf("strnvisx: %s\n", visout);60}61 62void test_strenvisx() {63  char visout[5];64  char src[] = "\2";65  strenvisx(visout, sizeof visout, src, sizeof src - 1 /* skip final \0 */,66            VIS_SAFE | VIS_NOSLASH, NULL);67  printf("strenvisx: %s\n", visout);68}69 70void test_svis() {71  char visout[5];72  int ch = toascii(0x3);73  svis(visout, ch, VIS_SAFE | VIS_NOSLASH, 0, "x");74  printf("svis: %s\n", visout);75}76 77void test_snvis() {78  char visout[5];79  int ch = toascii(0x2);80  snvis(visout, sizeof visout, ch, VIS_SAFE | VIS_NOSLASH, 0, "x");81  printf("snvis: %s\n", visout);82}83 84void test_strsvis() {85  char visout[5];86  strsvis(visout, "\4", VIS_SAFE | VIS_NOSLASH, "x");87  printf("strsvis: %s\n", visout);88}89 90void test_strsnvis() {91  char visout[5];92  strsnvis(visout, sizeof visout, "\5", VIS_SAFE | VIS_NOSLASH, "x");93  printf("strsnvis: %s\n", visout);94}95 96void test_strsvisx() {97  char visout[5];98  char src[] = "\5";99  strsvisx(visout, src, sizeof src - 1 /* skip final \0 */,100           VIS_SAFE | VIS_NOSLASH, "x");101  printf("strsvisx: %s\n", visout);102}103 104void test_strsnvisx() {105  char visout[5];106  char src[] = "\6";107  strsnvisx(visout, sizeof visout, src, sizeof src - 1 /* skip final \0 */,108            VIS_SAFE | VIS_NOSLASH, "x");109  printf("strsnvisx: %s\n", visout);110}111 112void test_strsenvisx() {113  char visout[5];114  char src[] = "\1";115  strsenvisx(visout, sizeof visout, src, sizeof src - 1 /* skip final \0 */,116             VIS_SAFE | VIS_NOSLASH, "x", NULL);117  printf("strsenvisx: %s\n", visout);118}119 120void test_unvis() {121  char visout[5];122  int ch = toascii(0x1);123  vis(visout, ch, VIS_SAFE, 0);124 125  int state = 0;126  char out;127  char *p = visout;128  while ((ch = *(p++)) != '\0') {129  again:130    switch (unvis(&out, ch, &state, 0)) {131    case 0:132    case UNVIS_NOCHAR:133      break;134    case UNVIS_VALID:135      printf("unvis: %" PRIx8 "\n", (unsigned char)out);136      break;137    case UNVIS_VALIDPUSH:138      printf("unvis: %" PRIx8 "\n", (unsigned char)out);139      goto again;140    case UNVIS_SYNBAD:141      errx(1, "Bad character sequence!");142    }143  }144  if (unvis(&out, '\0', &state, UNVIS_END) == UNVIS_VALID)145    printf("unvis: %" PRIx8 "\n", (unsigned char)out);146}147 148void test_strunvis() {149  char visout[5];150  int ch = toascii(0x2);151  vis(visout, ch, VIS_SAFE, 0);152 153  char p[5];154  strunvis(p, visout);155 156  char *pp = p;157  while ((ch = *(pp++)) != '\0')158    printf("strunvis: %" PRIx8 "\n", (unsigned char)ch);159}160 161void test_strnunvis() {162  char visout[5];163  int ch = toascii(0x3);164  vis(visout, ch, VIS_SAFE, 0);165 166  char p[5];167  strnunvis(p, sizeof p, visout);168 169  char *pp = p;170  while ((ch = *(pp++)) != '\0')171    printf("strnunvis: %" PRIx8 "\n", (unsigned char)ch);172}173 174void test_strunvisx() {175  char visout[5];176  int ch = toascii(0x4);177  vis(visout, ch, VIS_SAFE, 0);178 179  char p[5];180  strunvisx(p, visout, VIS_SAFE);181 182  char *pp = p;183  while ((ch = *(pp++)) != '\0')184    printf("strunvisx: %" PRIx8 "\n", (unsigned char)ch);185}186 187void test_strnunvisx() {188  char visout[5];189  int ch = toascii(0x5);190  vis(visout, ch, VIS_SAFE, 0);191 192  char p[5];193  strnunvisx(p, sizeof p, visout, VIS_SAFE);194 195  char *pp = p;196  while ((ch = *(pp++)) != '\0')197    printf("strnunvisx: %" PRIx8 "\n", (unsigned char)ch);198}199 200int main(void) {201  printf("vis\n");202 203  test_vis();204  test_nvis();205  test_strvis();206  test_stravis();207  test_strnvis();208  test_strvisx();209  test_strnvisx();210  test_strenvisx();211  test_svis();212  test_snvis();213  test_strsvis();214  test_strsnvis();215  test_strsvisx();216  test_strsnvisx();217  test_strsenvisx();218  test_unvis();219  test_strunvis();220  test_strnunvis();221  test_strunvisx();222  test_strnunvisx();223 224  // CHECK: vis225  // CHECK: vis: ^A226  // CHECK: nvis: ^B227  // CHECK: strvis: ^C228  // CHECK: stravis: ^D229  // CHECK: strnvis: ^E230  // CHECK: strvisx: ^F231  // CHECK: strnvisx: ^A232  // CHECK: strenvisx: ^B233  // CHECK: svis: ^C234  // CHECK: snvis: ^B235  // CHECK: strsvis: ^D236  // CHECK: strsnvis: ^E237  // CHECK: strsvisx: ^E238  // CHECK: strsnvisx: ^F239  // CHECK: strsenvisx: ^A240  // CHECK: unvis: 1241  // CHECK: strunvis: 2242  // CHECK: strnunvis: 3243  // CHECK: strunvisx: 4244  // CHECK: strnunvisx: 5245 246  return 0;247}248