TIL: Recovery & Testing

TestDisk Data Recovery

On Discord someone recently had one of their disk formatted by accident (remember kids, always backup before playing around with partitions for your dual-boot setup) and went around looking for a recovery software. In the past I’ve used R-Studio, not to be confused with RStudio, the IDE for the statistical programming language R and more, but who wants to spend money on software, when you can get it for free? (Imagine if I were a software developer, oh wait!)

Personally I haven’t tried TestDisk, but it seems to support pretty much anything, going from MS-DOS and Mac OS X PowerPC to Windows 10 and aarch64, while being able to handle different FAT, NTFS, MFT and various ext filesystem formats. And did I mention it’s free?

As a reminder: If you don’t already have an automatic backup solution, you should really invest in one, at least for your most precious data. Do you want to risk all your family photos, just to save one hundred bucks or less?

Using Catch2 with CMake

While playing around with the idea of a new, small library in C++, I wanted to start out with writing tests, so that I don’t have to write toy programs to ensure that the implementation actually works. Picking Catch2 as testing library was a no-brainer, but then the question arose on how to use Catch2 with CMake and CTest.

For the following example, I’ll assume it’s a header-only library. If you do use source files, then you just need to add a library target and link that with your test executables.

├───include
│   └───Example
|        └───Example.hpp
├───test
│   ├───CatchMain.cpp
│   ├───CMakeLists.txt
│   └───ExampleTest.cpp
└───CMakeLists.txt

Since it is a header-only library, the root level CMakeLists.txt only defines a project and adds the test sub-directory and some additional configs.

cmake_minimum_required(VERSION 3.11)

project(Example)

add_subdirectory("test")

The CatchMain.cpp provides a common entry-point for Catch2 and would allow for general configurations.

#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>

The test CMakeLists.txt uses the FetchContent module to dynamically download the Catch2 header, so that we don’t have to ship it with our code. This of course then puts the constraint on the CI system, if you run such a thing, that it needs to have access to the internet or at least GitHub.

We create a target per test file, with the CatchMain.cpp, “link” Catch2 against the target, tell it where to find the library headers, ensure we have access to C++17 and then enable CTest and handover the testable target.

include(FetchContent)

FetchContent_Declare(Catch2
                     GIT_REPOSITORY https://github.com/catchorg/Catch2.git
                     GIT_TAG        v2.13.6)

FetchContent_MakeAvailable(Catch2)

add_executable(example CatchMain.cpp Example.cpp)
target_link_libraries(example PRIVATE Catch2::Catch2)
target_include_directories(example PUBLIC ${PROJECT_SOURCE_DIR}/include)
set_property(TARGET example PROPERTY CXX_STANDARD 17)

enable_testing()
add_test(NAME ExampleTest COMMAND example)

And for the grand finale we need the test source file, which holds the test cases and to complete the example also a header file that acts as our “library”.

#pragma once

bool ReturnTrue()
{
    return true;
}

#include <catch2/catch.hpp>
#include <Example/Example.hpp>

TEST_CASE("ReturnTrue returns true", "[Example]")
{
    REQUIRE(ReturnTrue() == true);
}

Learning the Catch2 syntax and possibilities is left to the reader as an exercise.

Leave a Comment

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.