summaryrefslogtreecommitdiffstats
path: root/Tests/CMakeLib/testCTestResourceSpec.cxx
blob: b49f8ff2b47f638c556a1d02de66ba4fa0e4c4a0 (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
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
#include <iostream>
#include <string>
#include <vector>

#include "cmCTestResourceSpec.h"

struct ExpectedSpec
{
  std::string Path;
  cmCTestResourceSpec::ReadFileResult ParseResult;
  cmCTestResourceSpec Expected;
};

static const std::vector<ExpectedSpec> expectedResourceSpecs = {
  { "spec1.json",
    cmCTestResourceSpec::ReadFileResult::READ_OK,
    { { {
      { "gpus",
        {
          { "2", 4 },
          { "e", 1 },
        } },
      { "threads", {} },
    } } } },
  { "spec2.json", cmCTestResourceSpec::ReadFileResult::READ_OK, {} },
  { "spec3.json",
    cmCTestResourceSpec::ReadFileResult::INVALID_SOCKET_SPEC,
    {} },
  { "spec4.json",
    cmCTestResourceSpec::ReadFileResult::INVALID_SOCKET_SPEC,
    {} },
  { "spec5.json",
    cmCTestResourceSpec::ReadFileResult::INVALID_SOCKET_SPEC,
    {} },
  { "spec6.json",
    cmCTestResourceSpec::ReadFileResult::INVALID_SOCKET_SPEC,
    {} },
  { "spec7.json",
    cmCTestResourceSpec::ReadFileResult::INVALID_RESOURCE_TYPE,
    {} },
  { "spec8.json", cmCTestResourceSpec::ReadFileResult::INVALID_RESOURCE, {} },
  { "spec9.json", cmCTestResourceSpec::ReadFileResult::INVALID_RESOURCE, {} },
  { "spec10.json", cmCTestResourceSpec::ReadFileResult::INVALID_RESOURCE, {} },
  { "spec11.json", cmCTestResourceSpec::ReadFileResult::INVALID_RESOURCE, {} },
  { "spec12.json", cmCTestResourceSpec::ReadFileResult::INVALID_ROOT, {} },
  { "spec13.json", cmCTestResourceSpec::ReadFileResult::JSON_PARSE_ERROR, {} },
  { "spec14.json", cmCTestResourceSpec::ReadFileResult::READ_OK, {} },
  { "spec15.json", cmCTestResourceSpec::ReadFileResult::READ_OK, {} },
  { "spec16.json", cmCTestResourceSpec::ReadFileResult::READ_OK, {} },
  { "spec17.json", cmCTestResourceSpec::ReadFileResult::INVALID_RESOURCE, {} },
  { "spec18.json", cmCTestResourceSpec::ReadFileResult::INVALID_RESOURCE, {} },
  { "spec19.json", cmCTestResourceSpec::ReadFileResult::INVALID_VERSION, {} },
  { "spec20.json", cmCTestResourceSpec::ReadFileResult::READ_OK, {} },
  { "spec21.json", cmCTestResourceSpec::ReadFileResult::INVALID_VERSION, {} },
  { "spec22.json", cmCTestResourceSpec::ReadFileResult::INVALID_VERSION, {} },
  { "spec23.json", cmCTestResourceSpec::ReadFileResult::INVALID_VERSION, {} },
  { "spec24.json", cmCTestResourceSpec::ReadFileResult::INVALID_VERSION, {} },
  { "spec25.json",
    cmCTestResourceSpec::ReadFileResult::UNSUPPORTED_VERSION,
    {} },
  { "spec26.json",
    cmCTestResourceSpec::ReadFileResult::UNSUPPORTED_VERSION,
    {} },
  { "spec27.json", cmCTestResourceSpec::ReadFileResult::INVALID_VERSION, {} },
  { "spec28.json", cmCTestResourceSpec::ReadFileResult::INVALID_VERSION, {} },
  { "spec29.json", cmCTestResourceSpec::ReadFileResult::INVALID_VERSION, {} },
  { "spec30.json", cmCTestResourceSpec::ReadFileResult::INVALID_VERSION, {} },
  { "spec31.json", cmCTestResourceSpec::ReadFileResult::INVALID_VERSION, {} },
  { "spec32.json", cmCTestResourceSpec::ReadFileResult::INVALID_VERSION, {} },
  { "spec33.json", cmCTestResourceSpec::ReadFileResult::INVALID_VERSION, {} },
  { "spec34.json", cmCTestResourceSpec::ReadFileResult::INVALID_VERSION, {} },
  { "spec35.json", cmCTestResourceSpec::ReadFileResult::INVALID_VERSION, {} },
  { "spec36.json", cmCTestResourceSpec::ReadFileResult::NO_VERSION, {} },
  { "noexist.json", cmCTestResourceSpec::ReadFileResult::FILE_NOT_FOUND, {} },
};

static bool testSpec(const std::string& path,
                     cmCTestResourceSpec::ReadFileResult expectedResult,
                     const cmCTestResourceSpec& expected)
{
  cmCTestResourceSpec actual;
  auto result = actual.ReadFromJSONFile(path);
  if (result != expectedResult) {
    std::cout << "ReadFromJSONFile(\"" << path << "\") returned \""
              << cmCTestResourceSpec::ResultToString(result)
              << "\", should be \""
              << cmCTestResourceSpec::ResultToString(expectedResult) << "\""
              << std::endl;
    return false;
  }

  if (actual != expected) {
    std::cout << "ReadFromJSONFile(\"" << path
              << "\") did not give expected spec" << std::endl;
    return false;
  }

  return true;
}

int testCTestResourceSpec(int argc, char** const argv)
{
  if (argc < 2) {
    std::cout << "Invalid arguments.\n";
    return -1;
  }

  int retval = 0;
  for (auto const& spec : expectedResourceSpecs) {
    std::string path = argv[1];
    path += "/testCTestResourceSpec_data/";
    path += spec.Path;
    if (!testSpec(path, spec.ParseResult, spec.Expected)) {
      retval = -1;
    }
  }

  return retval;
}