File: open_text.c

package info (click to toggle)
libtrexio 2.2.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 6,996 kB
  • sloc: ansic: 48,761; f90: 6,313; sh: 4,451; makefile: 198; lisp: 58
file content (163 lines) | stat: -rw-r--r-- 3,942 bytes parent folder | download
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
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#define TEST_BACKEND 	TREXIO_TEXT
#define TREXIO_FILE 	"test_open.dir"
#define TREXIO_VOID 	"non_existing_" TREXIO_FILE
#define RM_COMMAND 	"rm -rf " TREXIO_FILE


static int test_open_w (const char* file_name, const back_end_t backend) {

/* Try to open the TREXIO file in 'write' mode */

  trexio_t* file = NULL;
  trexio_exit_code rc;

/*================= START OF TEST ==================*/

  // open file in 'write' mode
  file = trexio_open(file_name, 'w', backend, &rc);
  assert (file != NULL);
  assert (rc == TREXIO_SUCCESS);

  // close current session
  rc = trexio_close(file);
  assert (rc == TREXIO_SUCCESS);

/*================= END OF TEST ==================*/

  return 0;
}


static int test_open_r (const char* file_name, const back_end_t backend) {

/* Try to open the TREXIO file in 'read' mode */

  trexio_t* file = NULL;
  trexio_exit_code rc;

/*================= START OF TEST ==================*/

  // open file in 'write' mode
  file = trexio_open(file_name, 'r', backend, &rc);
  assert (file != NULL);
  assert (rc == TREXIO_SUCCESS);

  // close current session
  rc = trexio_close(file);
  assert (rc == TREXIO_SUCCESS);

/*================= END OF TEST ==================*/

  return 0;
}


static int test_open_auto (const char* file_name) {

/* Try to open the TREXIO file in 'read' mode */

  trexio_t* file = NULL;
  trexio_exit_code rc;

/*================= START OF TEST ==================*/

  // open file in 'write' mode
  file = trexio_open(file_name, 'r', TREXIO_AUTO, &rc);
  assert (file != NULL);
  assert (rc == TREXIO_SUCCESS);

  // close current session
  rc = trexio_close(file);
  assert (rc == TREXIO_SUCCESS);

/*================= END OF TEST ==================*/

  return 0;
}


static int test_open_errors (const back_end_t backend) {

/* Try to call trexio_open with bad arguments */

  trexio_t* file = NULL;
  trexio_exit_code rc;

/*================= START OF TEST ==================*/

  // open non-existing file in 'r' (read) mode, should return TREXIO_OPEN_ERROR
  file = trexio_open(TREXIO_VOID, 'r', backend, &rc);
  assert (file == NULL);
  assert (rc == TREXIO_OPEN_ERROR);
  fprintf(stderr, "%s \n", trexio_string_of_error(rc));

  // open file with empty file name, should return TREXIO_INVALID_ARG_1
  file = trexio_open("", 'w', backend, &rc);
  assert (file == NULL);
  assert (rc == TREXIO_INVALID_ARG_1);
  fprintf(stderr, "%s \n", trexio_string_of_error(rc));

  // open existing file in non-supported I/O mode, should return TREXIO_INVALID_ARG_2
  file = trexio_open(TREXIO_FILE, 'k', backend, &rc);
  assert (file == NULL);
  assert (rc == TREXIO_INVALID_ARG_2);
  fprintf(stderr, "%s \n", trexio_string_of_error(rc));

  // open existing file with non-supported back end, should return TREXIO_INVALID_ARG_3
  file = trexio_open(TREXIO_FILE, 'w', 666, &rc);
  assert (file == NULL);
  assert (rc == TREXIO_INVALID_ARG_3);
  fprintf(stderr, "%s \n", trexio_string_of_error(rc));

/*================= END OF TEST ==================*/

  return 0;
}


static int test_inquire (const back_end_t backend) {

/* Try to call trexio_inquire function */

  trexio_exit_code rc;

/*================= START OF TEST ==================*/

  // inquire non-existing file
  rc = trexio_inquire(TREXIO_VOID);
  assert (rc == TREXIO_FAILURE);

  // inquire existing file
  rc = trexio_inquire(TREXIO_FILE);
  assert (rc == TREXIO_SUCCESS);

/*================= END OF TEST ==================*/

  return 0;
}


int main(void) {

/*============== Test launcher ================*/

  int rc;
  rc = system(RM_COMMAND);
  assert (rc == 0);

  test_open_w     (TREXIO_FILE, TEST_BACKEND);
  test_open_r     (TREXIO_FILE, TEST_BACKEND);
  test_open_auto  (TREXIO_FILE);
  test_open_errors(TEST_BACKEND);
  test_inquire    (TEST_BACKEND);

  rc = system(RM_COMMAND);
  assert (rc == 0);

  return 0;
}