about summary refs log tree commit diff
path: root/stdio-common/tst-fmemopen2.c
blob: bcb136e8c1f0e226f6b6ff30dc6165675d382b39 (plain) (blame)
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
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>


static int
do_test (void)
{
  int result = 0;
  char buf[100];
  FILE *fp = fmemopen (buf, sizeof (buf), "w");
  if (fp == NULL)
    {
      puts ("fmemopen failed");
      return 0;
    }
  static const char str[] = "hello world";
#define nstr (sizeof (str) - 1)
  fputs (str, fp);
  off_t o = ftello (fp);
  if (o != nstr)
    {
      printf ("first ftello returned %ld, expected %zu\n", o, nstr);
      result = 1;
    }
  rewind (fp);
  o = ftello (fp);
  if (o != 0)
    {
      printf ("second ftello returned %ld, expected %zu\n", o, 0);
      result = 1;
    }
  if (fseeko (fp, 0, SEEK_END) != 0)
    {
      puts ("fseeko failed");
      return 1;
    }
  o = ftello (fp);
  if (o != nstr)
    {
      printf ("third ftello returned %ld, expected %zu\n", o, nstr);
      result = 1;
    }
  rewind (fp);
  static const char str2[] = "just hello";
#define nstr2 (sizeof (str2) - 1)
  assert (nstr2 < nstr);
  fputs (str2, fp);
  o = ftello (fp);
  if (o != nstr2)
    {
      printf ("fourth ftello returned %ld, expected %zu\n", o, nstr2);
      result = 1;
    }
  fclose (fp);
  static const char str3[] = "just hellod";
  if (strcmp (buf, str3) != 0)
    {
      printf ("final string is \"%s\", expected \"%s\"\n",
              buf, str3);
      result = 1;
    }
  return result;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"