Tools a C++ programmer may need

Pages: 123
OK, I will check updates later on and will bear that in mind.

The "installed" folder contains very many files! I'm not sure which one explains how to use catch2 in visual studio simply. (If any)

The following code exists in a C++ project on Visual Studio:

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
#include <iostream>
#include <vector>

int solution(int X, std::vector<int>& A) {
	std::vector<bool> steps(X, true);
	int passedRoute = 0;

	for (size_t i = 0; i < A.size(); ++i) {
		if (A[i] >= 1 && A[i] <= X)
			if (steps[A[i] - 1] != 0) {
				steps[A[i] - 1] = 0;
				++passedRoute;
			}
		if (passedRoute == X)
			return i;
	}
	return -1;
}

int main() {
	
	std::vector A{ 1, 3, 1, 4, 2, 3, 5, 4 };
	std::cout << solution(5, A) << '\n';

	return 0;
}



How to test its correctness using Catch2, please?
Docs for catch2 are (probably) located in C:\dev\vcpkg\installed\x64-windows\share\doc\Catch2. I've glanced at the tutorial doc and it seems to be easy to understand and fairly straightforward.

How to test its correctness using Catch2, please?

That is why I suggested you READ the docs available for catch2.

I don't have a clue since I've never used that library, and likely won't anytime soon.
Last edited on
They're all .md files!

I'm also having a look at: https://github.com/catchorg/Catch2/blob/devel/docs/tutorial.md#writing-tests

There, a header: #include <catch2/catch_test_macros.hpp> is used for the test apparently. But that catch_test_macros.hpp doesn't exist in the vcpkg folder!
Last edited on
They're all .md files!

Yes, they are. Do you not have an app that can properly display .md files?

Notepad++ is what I use for those files.
https://notepad-plus-plus.org/

Notepad++ is another "must have" tool for programmers IMO.

But that catch_test_macros.hpp doesn't exist in the vcpkg folder!
Au contraire. It does exist, in the catch2 subfolder, along with other .hpp files you will probably use for other testing purposes as explained in the documentation.

That tutorial.md file your are reading online is in the doc folder I told you about. In both the x64 and x86 folders.

So you can peruse offline.
I searched the whole vcpkg folder for the file catch_test_macros.hpp but there's not such a file there!
SUB-FOLDER! SUB-FOLDER! SUB-FOLDER!

C:\dev\vcpkg\installed\x64-windows\include\catch2

There is also a repeat of the headers and documentation in the x-86-windows folder, with the same sub-folder structure.

I'm only a self-taught programming hobbyist, ZIPPO formal/classroom training or instruction, but I knew where and how to look ON MY OWN!

I decided to help you with getting vcpkg working properly because I calculated I'd learn how to get it to work first myself. And lo! I did!

You need to learn to be more self-sufficient, stop expecting people to hold your hand for every little new thing you run across.

Yes, dammit, I'm cranky because a programmer, a good one or better, should be a self-starter. Curious and self-motivated to push the boundaries of their knowledge on their own.
I agree with you. If I tell you about my background and how I reached the current point you will change your mind about me.

When I say I've searched for such file and there is not that in the folder, I'm almost quite confident. Here's also a screenshot: https://imgur.com/Ma8vkDw

I tested the following two ways before asking about that file:
1) The include didn't recognize the header
2) there was not that file in the include folder nor was it in the whole folder.

Do I need to search the whole C:\ drive for that?
https://imgur.com/VETc1W2
Last edited on
Did you ever consider the online documentation (or portions of github) MIGHT be outdated? That happens often enough to be a PITA.

The local installed documentation, tutorial.md, doesn't mention that catch_test_macros.hpp header.

There's a downloads folder for vcpkg that contains the source files for any installed packages.

The first test example in the tutorial is 010-TestCase.cpp. Find it in the in the catchorg-Catch2-v2.13.8.tar.gz in the downloads folder and what's the #include used in that source?

#include <catch2/catch.hpp>

The offline .md tutorial installed with vcpkg mentions what header to use, and it ain't catch_test_macros.hpp.

When in doubt look to the installed offline documentation before assuming something is in error.

As it turns out vcpkg pulls its library package from a different location of the github:
https://github.com/catchorg/Catch2/releases

Another reason to learn to be flexible and be curious enough to go LOOK "outside the box."

I was able to get the downloaded 010-TestCase.cpp example code to compile and run without a hitch.

Because I didn't just throw-up my hands and surrender.

Well, the test cases and asserts had some failures when executed, but that was expected.

I learned all this COLD. I had never heard of Catch2 before, and hadn't been very successful in getting vcpkg to work in the past.

But I didn't give up, any road-block thrown up I kept looking around for possible alternate solutions.

Which yielded success.
For instance we have a factorial algorithm and wish to test it using Catch2 in Visual Studio. My purpose is to know first: how Catch2 works and second: how beneficial it is (since there's much about that being quite profitable and a must for good developers).

I wrote it this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#define CATCH_CONFIG_MAIN
#include <catch.hpp>

unsigned int Factorial(unsigned int number) {
	return number <= 1 ? number : Factorial(number - 1) * number;
}

TEST_CASE("Factorials are computed", "[factorial]") {
	REQUIRE(Factorial(0) == 1);
	REQUIRE(Factorial(1) == 1);
	REQUIRE(Factorial(2) == 2);
	REQUIRE(Factorial(3) == 6);
	REQUIRE(Factorial(10) == 3628800);
}

int main() {
	std::cout << Factorial(6) << '\n';

	return 0;
}


Two odd things:
1- We've got an output, 720, without having <iostream> included!
2- When the project is run normally (by pressing F5), it runs as if there's no unit test. That is, nothing additional is shown as the output of Catch2.
Last edited on
Topic archived. No new replies allowed.
Pages: 123