File: parcompwrite.cpp

package info (click to toggle)
cadical 2.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,216 kB
  • sloc: cpp: 36,901; ansic: 4,521; sh: 1,770; makefile: 91
file content (275 lines) | stat: -rw-r--r-- 6,144 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "../../src/cadical.hpp"
#include "../../src/file.hpp"
#include "../../src/internal.hpp"

#include <cassert>
#include <cctype>
#include <climits>
#include <cstdint>
#include <iostream>
#include <string>

extern "C" {
#include <pthread.h>
#include <unistd.h>
}

using namespace std;
using namespace CaDiCaL;

static string prefix (const char *tester) {
  string res = "/tmp/cadical-api-test-parcompwrite-";
  res += tester;
  res += "-";
  res += to_string (getpid ());
  return res;
}

static const char *suffix = ".gz";

static string path (const char *tester, unsigned i) {
  return prefix (tester) + "-" + to_string (i) + suffix;
}

static pthread_mutex_t serialize_mutex = PTHREAD_MUTEX_INITIALIZER;

static void lock () {
  if (pthread_mutex_lock (&serialize_mutex))
    perror ("error: pthread_mutex_lock failed");
}

static void unlock () {
  if (pthread_mutex_unlock (&serialize_mutex))
    perror ("error: pthread_mutex_unlock failed");
}

class tester {
  pthread_t thread;
  bool spath_cached = false;
  string spath;

protected:
  unsigned i;

public:
  tester (unsigned j) : i (j) {}
  virtual ~tester () {}
  virtual const char *name () = 0;
  const char *path ();
  void message (const char *);
  virtual void writing () = 0;
  virtual void reading () = 0;
  virtual void write () = 0;
  virtual bool read (unsigned &j) = 0;
  virtual void close () = 0;
  void unlink ();
  void run ();
  void create ();
  void join ();
};

class stdio_tester : public tester {
  FILE *file = 0;

public:
  stdio_tester (unsigned j) : tester (j) {}
  const char *name () override { return "stdio"; }
  void writing () override {
    file = fopen (path (), "w");
    if (!file)
      perror ("error: fopen to write failed"), exit (1);
  }
  void reading () override {
    file = fopen (path (), "r");
    if (!file)
      perror ("error: fopen to read failed"), exit (1);
  }
  void write () override {
    assert (file);
    if (fprintf (file, "%u\n", i) <= 0)
      fprintf (stderr, "error: fprintf failed\n");
  }
  bool read (unsigned &j) override {
    assert (file);
    if (fscanf (file, "%u", &j) <= 0) {
      fprintf (stderr, "error: fscanf failed\n");
      return false;
    }
    return true;
  }
  void close () override {
    if (fclose (file))
      perror ("error: fclose written failed");
  }
};

class cadical_file_tester : public tester {
  File *file = 0;
  Internal *internal;

public:
  cadical_file_tester (unsigned j) : tester (j) {
    internal = new Internal ();
#ifndef QUIET
    internal->opts.verbose = 0;
    internal->opts.quiet = 1;
#endif
  }
  ~cadical_file_tester () { delete internal; }
  const char *name () override { return "cadical-file"; }
  void writing () override {
    file = File::write (internal, path ());
    if (!file)
      fprintf (stderr, "error: 'File::write' failed\n"), exit (1);
  }
  void reading () override {
    file = File::read (internal, path ());
    if (!file)
      fprintf (stderr, "error: File::read failed\n"), exit (1);
  }
  void write () override {
    assert (file);
    if (!file->put ((uint64_t) i) || !file->endl ())
      fprintf (stderr, "error: File::put failed\n");
  }
  bool read (unsigned &j) override {
    assert (file);
    int ch = file->get ();
    if (!isdigit (ch)) {
    INVALID_NUMBER:
      fprintf (stderr,
               "error: invalid number in 'cadical_file_tester::read'\n");
      return false;
    }
    unsigned tmp = ch - '0';
    while (isdigit (ch = file->get ())) {
      if (UINT_MAX / 10 < tmp)
        goto INVALID_NUMBER;
      tmp *= 10;
      unsigned digit = ch - '0';
      if (UINT_MAX - digit < tmp)
        goto INVALID_NUMBER;
      tmp += digit;
    }
    if (ch != '\n') {
      fprintf (stderr, "error: expected new-line after number");
      return false;
    }
    if (file->get () != EOF) {
      fprintf (stderr,
               "error: expected end-of-file after line with number");
      return false;
    }
    j = tmp;
    return true;
  }
  void close () override {
    assert (file);
    file->close ();
  }
};

const char *tester::path () {
  if (!spath_cached) {
    spath = ::path (name (), i);
    spath_cached = true;
  }
  return spath.c_str ();
}

void tester::message (const char *what) {
  lock ();
  printf ("%-17s %s\n", what, path ());
  fflush (stdout);
  unlock ();
}

void tester::run () {
  message ("opening-to-write");
  writing ();
  message ("writing");
  write ();
  message ("closing");
  close ();
  message ("reading");
  reading ();
  unsigned j;
  if (read (j) && i == j)
    message ("checked");
  else {
    lock ();
    fprintf (stderr,
             "error: writing and reading back '%u' from '%s' failed\n", i,
             path ());
    fflush (stderr);
    unlock ();
    exit (1);
  }
  message ("closing");
  close ();
  unlink ();
}

void tester::unlink () {
  message ("deleting");
  if (::unlink (path ()))
    perror ("error: unlink failed");
}

void *start (void *p) {
  tester *t = (tester *) p;
  t->run ();
  return t;
}

void tester::create () {
  if (pthread_create (&thread, 0, start, this))
    perror ("error: 'pthread_created' failed");
}

void tester::join () {
  void *p;
  if (pthread_join (thread, &p))
    perror ("error: 'pthread_join' failed");
  assert (p == this);
}

#include <csignal>

static void catch_alarm (int sig) {
  assert (sig == SIGALRM);
  const char *msg = "error: unexpected alarm (file I/O hanging?)\n";
  if (::write (2, msg, strlen (msg)))
    exit (2);
  (void) sig;
  exit (1);
}

int main () {
  const char *suffixes[] = {"", ".gz"};
  (void) ::signal (SIGALRM, catch_alarm);
  ::alarm (2);
  for (auto s : suffixes) {
    suffix = s;
    for (unsigned c = 0; c != 2; c++) {
      if (!c && *suffix)
        continue;
      vector<tester *> testers;
      for (size_t i = 0; i != 100; i++) {
        tester *t;
        if (c)
          t = new cadical_file_tester (i);
        else
          t = new stdio_tester (i);
        testers.push_back (t);
      }
      for (auto t : testers)
        t->create ();
      for (auto t : testers)
        t->join ();
      for (auto t : testers)
        delete t;
    }
  }
  return 0;
}