aboutsummaryrefslogtreecommitdiff
path: root/backend/unittests/Support/ErrorTest.cpp
blob: 803cceec39a3ff0a74d45a21d9454ee7b8e3d661 (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
// SPDX-License-Identifier: GPL-3.0-or-later

#include "rcc/Support/Error.h"

#include <iostream>
#include <string>

namespace {

int fail(const char *Message) {
  std::cerr << Message << '\n';
  return 1;
}

} // namespace

int main() {
  rcc::Expected<int> Value = 42;
  if (!Value || *Value != 42)
    return fail("Expected must preserve a successful value");

  rcc::Expected<int> Failure = rcc::makeUnexpected(rcc::ErrorCode::ParseError, "bad MIR");
  if (Failure)
    return fail("Expected must preserve an error");
  if (Failure.error().code() != rcc::ErrorCode::ParseError)
    return fail("Error must preserve its code");
  if (Failure.error().message() != "bad MIR")
    return fail("Error must preserve its message");
  if (rcc::getErrorCodeName(Failure.error().code()) != "parse error")
    return fail("Error code must have a stable name");
  if (rcc::getErrorCodeName(rcc::ErrorCode::FrameLayoutFailed) != "frame layout failed")
    return fail("frame-layout errors must have a stable name");
  if (rcc::getErrorCodeName(rcc::ErrorCode::SchedulingFailed) != "scheduling failed")
    return fail("scheduling errors must have a stable name");
  if (rcc::getErrorCodeName(rcc::ErrorCode::BranchRelaxationFailed) != "branch relaxation failed")
    return fail("branch-relaxation errors must have a stable name");
  if (rcc::getErrorCodeName(rcc::ErrorCode::PseudoExpansionFailed) != "pseudo expansion failed")
    return fail("pseudo-expansion errors must have a stable name");
  if (rcc::getErrorCodeName(rcc::ErrorCode::FinalEncodabilityFailed) != "final encodability failed")
    return fail("final-encodability errors must have a stable name");
  if (rcc::getErrorCodeName(rcc::ErrorCode::CFGOptimizationFailed) != "CFG optimization failed")
    return fail("CFG-optimization errors must have a stable name");
  if (rcc::getErrorCodeName(rcc::ErrorCode::AssemblyLoweringFailed) != "assembly lowering failed")
    return fail("assembly-lowering errors must have a stable name");
  if (rcc::getErrorCodeName(rcc::ErrorCode::InstructionEncodingFailed) !=
      "instruction encoding failed")
    return fail("instruction-encoding errors must have a stable name");
  if (rcc::getErrorCodeName(rcc::ErrorCode::FixupApplicationFailed) != "fixup application failed")
    return fail("fixup-application errors must have a stable name");
  if (rcc::getErrorCodeName(rcc::ErrorCode::RelocationFailed) != "relocation failed")
    return fail("relocation errors must have a stable name");
  if (rcc::getErrorCodeName(rcc::ErrorCode::ObjectWritingFailed) != "object writing failed")
    return fail("object-writing errors must have a stable name");

  return 0;
}