Line Code
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
#include "catch.hpp"
#include "FWCore/Utilities/interface/OftenEmptyCString.h"
#include <cstring>

TEST_CASE("test edm::OftenEmptyCString", "[OftenEmptyCString]") {
  SECTION("Constructors") {
    SECTION("default") {
      edm::OftenEmptyCString s;
      REQUIRE(s.c_str() != nullptr);
      REQUIRE(s.c_str()[0] == '\0');
    }
    SECTION("from const *") {
      SECTION("nullptr") {
        edm::OftenEmptyCString s(nullptr);
        REQUIRE(s.c_str() != nullptr);
        REQUIRE(s.c_str()[0] == '\0');
      }
      SECTION("empty") {
        const char* kEmpty = "";
        edm::OftenEmptyCString s(kEmpty);
        REQUIRE(s.c_str() != nullptr);
        REQUIRE(s.c_str() != kEmpty);
        REQUIRE(s.c_str()[0] == '\0');
      }
      SECTION("non empty string") {
        const char* kValue = "something";
        edm::OftenEmptyCString s(kValue);
        REQUIRE(s.c_str() != nullptr);
        REQUIRE(s.c_str() != kValue);
        REQUIRE(strncmp(kValue, s.c_str(), 9) == 0);
        REQUIRE(strlen(kValue) == strlen(s.c_str()));
      }
    }
    SECTION("Copy") {
      SECTION("from non empty") {
        edm::OftenEmptyCString s("something");
        edm::OftenEmptyCString copy(s);
        REQUIRE(s.c_str() != copy.c_str());
        REQUIRE(strcmp(s.c_str(), copy.c_str()) == 0);
      }
      SECTION("from default") {
        edm::OftenEmptyCString s;
        edm::OftenEmptyCString copy(s);
        REQUIRE(s.c_str() == copy.c_str());
        REQUIRE(s.c_str() != nullptr);
        REQUIRE(strlen(s.c_str()) == 0);
      }
    }
    SECTION("Move") {
      SECTION("from non empty") {
        edm::OftenEmptyCString s("something");
        edm::OftenEmptyCString copy(std::move(s));
        REQUIRE(s.c_str() != copy.c_str());
        REQUIRE(s.c_str() == nullptr);
        REQUIRE(strcmp("something", copy.c_str()) == 0);
      }
      SECTION("from default") {
        edm::OftenEmptyCString s;
        edm::OftenEmptyCString copy(std::move(s));
        REQUIRE(s.c_str() == nullptr);
        REQUIRE(copy.c_str() == edm::OftenEmptyCString().c_str());
      }
    }
  }
  SECTION("operator=") {
    SECTION("copy version") {
      SECTION("from non empty to non empty") {
        edm::OftenEmptyCString s("something");
        edm::OftenEmptyCString copy("else");
        copy = s;
        REQUIRE(s.c_str() != copy.c_str());
        REQUIRE(strcmp("something", copy.c_str()) == 0);
        REQUIRE(strcmp(s.c_str(), copy.c_str()) == 0);
      }
      SECTION("from default to non empty") {
        edm::OftenEmptyCString s;
        edm::OftenEmptyCString copy("original");
        copy = s;
        REQUIRE(strcmp(s.c_str(), copy.c_str()) == 0);
      }
      SECTION("from non empty to empty") {
        edm::OftenEmptyCString s("something");
        edm::OftenEmptyCString copy;
        copy = s;
        REQUIRE(s.c_str() != copy.c_str());
        REQUIRE(strcmp("something", copy.c_str()) == 0);
        REQUIRE(strcmp(s.c_str(), copy.c_str()) == 0);
      }
    }
    SECTION("move version") {
      SECTION("from non empty to non empty") {
        edm::OftenEmptyCString s("something");
        edm::OftenEmptyCString copy("else");
        copy = std::move(s);
        REQUIRE(s.c_str() != copy.c_str());
        REQUIRE(s.c_str() == nullptr);
        REQUIRE(strcmp("something", copy.c_str()) == 0);
      }
      SECTION("from default to non empty") {
        edm::OftenEmptyCString s;
        edm::OftenEmptyCString copy("original");
        copy = std::move(s);
        REQUIRE(copy.c_str() != nullptr);
        REQUIRE(copy.c_str()[0] == '\0');
        REQUIRE(s.c_str() == nullptr);
      }
      SECTION("from non empty to empty") {
        edm::OftenEmptyCString s("something");
        edm::OftenEmptyCString copy;
        copy = std::move(s);
        REQUIRE(s.c_str() != copy.c_str());
        REQUIRE(s.c_str() == nullptr);
        REQUIRE(strcmp("something", copy.c_str()) == 0);
      }
    }
  }
}