Skip to content

Instantly share code, notes, and snippets.

@cdecl
Last active August 29, 2015 14:16
Show Gist options
  • Save cdecl/c7f0df52c991f906bb01 to your computer and use it in GitHub Desktop.
Save cdecl/c7f0df52c991f906bb01 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <memory>
using namespace std;
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
TEST_CASE("std::string", "test")
{
string s = "abcedf";
REQUIRE(s.capacity() > 0);
SECTION("TEST 1") {
REQUIRE(s.length() > 0);
REQUIRE(s[0] == 'a');
string ss(move(s));
CAPTURE(ss);
CAPTURE(s.capacity());
REQUIRE(s.capacity() == 0); // ERROR
REQUIRE(ss.length() > 0);
}
s = "ab";
SECTION("TEST 2") {
REQUIRE_THROWS([]{ throw "error"; }());
REQUIRE_THROWS(s.at(5) = 'e');
REQUIRE_NOTHROW(s.at(0) = 's');
}
}
TEST_CASE("std::shared_ptr", "test")
{
auto sp = std::make_shared<string>("init");
CHECK(sp);
auto sp2 = sp;
CHECK(sp.use_count() == 2);
weak_ptr<string> wp = sp;
CHECK(sp.use_count() == 2);
SECTION("WeakPtr 1") {
auto spp = wp.lock();
CAPTURE(sp.use_count());
CAPTURE(spp.use_count());
CHECK(spp.use_count() == 3);
}
sp.reset();
sp2.reset();
SECTION("WeakPtr 2") {
auto spp = wp.lock();
CAPTURE(sp.use_count());
CAPTURE(spp.use_count());
CHECK(spp); // ERROR
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment