File: testcoverage.c

package info (click to toggle)
kissat 4.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,068 kB
  • sloc: ansic: 39,201; sh: 1,226; makefile: 91
file content (77 lines) | stat: -rw-r--r-- 2,215 bytes parent folder | download | duplicates (2)
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 <ctype.h>
#include <dirent.h>
#include <sys/types.h>

#include "test.h"

static bool is_cover_file_name (const char *name) {
#define CHAR(CH) (*p++ == CH)
#define DIGIT() (isdigit ((int) *p++))
  const char *p = name;
  // clang-format off
  return 
    CHAR ('c') && CHAR ('o') && CHAR ('v') && CHAR ('e') && CHAR ('r') &&
    DIGIT () && DIGIT () && DIGIT () && DIGIT () &&
    CHAR ('.') && CHAR ('c') && CHAR ('n') && CHAR ('f');
// clang-format on
#undef CHAR
#undef DIGIT
}

static void schedule_cover_file (const char *dir, const char *name) {
  char path[512];
  assert (dir[0]);
  assert (dir[strlen (dir) - 1] == '/');
  assert (strlen (dir) + strlen (name) + 1 < sizeof path);
  sprintf (path, "%s%s", dir, name);
  FILE *file = fopen (path, "r");
  if (!file)
    FATAL ("could not read '%s'", path);
  int status;
  if (fscanf (file, "c status %d\n", &status) != 1)
    FATAL ("parse error at line 1 in '%s': expected 'c status <status>'",
           path);
  fclose (file);
  tissat_schedule_application (status, path);
}

#define MAX_COVER_FILES (1 << 16)

static char *cover_files[MAX_COVER_FILES];
static size_t size_cover_files;

static void push_cover_file (const char *name) {
  assert (size_cover_files < MAX_COVER_FILES);
  char *tmp = malloc (strlen (name) + 1);
  cover_files[size_cover_files++] = strcpy (tmp, name);
}

static int cmp (const void *p, const void *q) {
  return strcmp (*(char **) p, *(char **) q);
}

static void sort_cover_files (void) {
  qsort (cover_files, size_cover_files, sizeof (char *), cmp);
}

void tissat_schedule_coverage (void) {
  if (!tissat_found_test_directory)
    return;
  const char *path = "../test/cover/";
  DIR *dir = opendir (path);
  if (!dir)
    FATAL ("could not open directory '%s' "
           "(even though '../test/' claimed to exist)",
           path);
  struct dirent *entry;
  const char *name;
  while ((entry = readdir (dir)))
    if (is_cover_file_name ((name = entry->d_name)))
      push_cover_file (name);
  closedir (dir);
  sort_cover_files ();
  for (size_t i = 0; i < size_cover_files; i++)
    schedule_cover_file (path, cover_files[i]);
  for (size_t i = 0; i < size_cover_files; i++)
    free (cover_files[i]);
}