summaryrefslogtreecommitdiff
path: root/lib/libc_r/TEST/test_stdio_1.c
blob: fe146b25b4533a71f22c8314f9b0d59f2e7df17e (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
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "test.h"

char * base_name = "test_stdio_1.c";
char * dir_name = SRCDIR;
char * fullname;

/* Test fopen()/ftell()/getc() */
void
test_1()
{
	struct stat statbuf;
	FILE * fp;
	int i;

	CHECKe(stat(fullname, &statbuf));

	CHECKn((fp = fopen(fullname, "r")));

	/* Get the entire file */
	while ((i = getc(fp)) != EOF)
		;

	ASSERT(ftell(fp) == statbuf.st_size);

	CHECKe(fclose(fp));
}

/* Test fopen()/fclose() */
void
test_2()
{
	FILE *fp1, *fp2;

	CHECKn(fp1 = fopen(fullname, "r"));
	CHECKe(fclose(fp1));

	CHECKn(fp2 = fopen(fullname, "r"));
	CHECKe(fclose(fp2));

	ASSERT(fp1 == fp2);
}

/* Test sscanf()/sprintf() */
void
test_3(void)
{
	char * str = "10 4.53";
	char buf[64];
	double d;
	int    i;

	ASSERT(sscanf(str, "%d %lf", &i, &d) == 2);

	/* Should have a check */
	sprintf(buf, "%d %2.2f", i, d);
	ASSERT(strcmp(buf, str) == 0);
}

int
main()
{

	CHECKn(fullname = malloc (strlen (dir_name) + strlen (base_name) + 2));
	sprintf (fullname, "%s/%s", dir_name, base_name);

	test_1();
	test_2();
	test_3();

	SUCCEED;
}