~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/stdio/test_fgetc_ungetc.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-09-20 22:44:35 UTC
  • mfrom: (4.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130920224435-apuwj4fsl3fqv1a6
Tags: 1.5.6~20130920~6010666-1
* New snapshot release
* Update the list of supported architectures to the same as libv8
  (Closes: #723129)
* emlibtool has been removed from upstream.
* Fix warning syntax-error-in-dep5-copyright
* Refresh of the patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <assert.h>
 
2
#include <fcntl.h>
 
3
#include <signal.h>
 
4
#include <stdio.h>
 
5
#include <stdlib.h>
 
6
#include <string.h>
 
7
#include <unistd.h>
 
8
 
 
9
static void create_file(const char *path, const char *buffer, int mode) {
 
10
  int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
 
11
  assert(fd >= 0);
 
12
 
 
13
  int err = write(fd, buffer, sizeof(char) * strlen(buffer));
 
14
  assert(err ==  (sizeof(char) * strlen(buffer)));
 
15
 
 
16
  close(fd);
 
17
}
 
18
 
 
19
void setup() {
 
20
  create_file("file.txt", "cd", 0666);
 
21
}
 
22
 
 
23
void cleanup() {
 
24
  unlink("file.txt");
 
25
}
 
26
 
 
27
void test() {
 
28
  FILE *file;
 
29
  int err;
 
30
  char buffer[256];
 
31
 
 
32
  file = fopen("file.txt", "r");
 
33
  assert(file);
 
34
 
 
35
  // pushing EOF always returns EOF
 
36
  rewind(file);
 
37
  err = ungetc(EOF, file);
 
38
  assert(err == EOF);
 
39
 
 
40
  // ungetc should return itself
 
41
  err = ungetc('a', file);
 
42
  assert(err == (int)'a');
 
43
 
 
44
  // push two chars and make sure they're read back in
 
45
  // the correct order (both by fgetc and fread)
 
46
  rewind(file);
 
47
  ungetc('b', file);
 
48
  ungetc('a', file);
 
49
  err = fgetc(file);
 
50
  assert(err == (int)'a');
 
51
  int r = fread(buffer, sizeof(char), sizeof(buffer), file);
 
52
  assert(r == 3);
 
53
  buffer[3] = 0;
 
54
  assert(!strcmp(buffer, "bcd"));
 
55
 
 
56
  // rewind and fseek should reset anything that's been
 
57
  // pushed to the stream
 
58
  ungetc('a', file);
 
59
  rewind(file);
 
60
  err = fgetc(file);
 
61
  assert(err == (int)'c');
 
62
  ungetc('a', file);
 
63
  fseek(file, 0, SEEK_SET);
 
64
  err = fgetc(file);
 
65
  assert(err == (int)'c');
 
66
 
 
67
  // fgetc, when nothing is left, should return EOF
 
68
  fseek(file, 0, SEEK_END);
 
69
  err = fgetc(file);
 
70
  assert(err == EOF);
 
71
  err = feof(file);
 
72
  assert(err);
 
73
 
 
74
  // ungetc should reset the EOF indicator
 
75
  ungetc('e', file);
 
76
  err = feof(file);
 
77
  assert(!err);
 
78
 
 
79
  fclose(file);
 
80
 
 
81
  puts("success");
 
82
}
 
83
 
 
84
int main() {
 
85
  atexit(cleanup);
 
86
  signal(SIGABRT, cleanup);
 
87
  setup();
 
88
  test();
 
89
  return EXIT_SUCCESS;
 
90
}