You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TEST_CASE("Date has uninitialised state") {
Date today;
std::cout << "Today is: " << endl;
printDate(today);
}
The test case in gtest is:
TEST(Date, hasUninitialisedState) {
Date today;
std::cout << "Today is: " << endl;
printDate(today);
}
printDate is:
voidprintDate(const Date& date) {
cout << date.day() << "/"// cast to an integer to allow the month to be sent to the stream
<< static_cast<int>(date.month()) << "/"
<< date.year()
<< endl;
}
doctest's output is:
doctest] doctest version is "1.2.1"
[doctest] run with "--help" for options
Today is:
0/0/0
gtest's output is:
Running main() from gtest_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from Date
[ RUN ] Date.hasUninitialisedState
Today is:
5382784/0/16
Why are the Date's integer values default initialised to 0 when using doctest?
The text was updated successfully, but these errors were encountered:
It should be purely by coincidence. Perhaps just before calling the test function some other internal function of doctest has used the same stack space for a variable which left the memory with zeroes.
The following program shows this:
#include<cstdio>voidf() {
int a = 45;
printf("f(): %d\n", a);
}
voidg() {
int b; // uninitialized - but will occupy the same stack space as "a" from "f()"printf("g(): %d\n", b);
}
intmain() {
f();
g();
}
Built without optimizations (G++ 7 under Windows) it prints the following:
f(): 45
g(): 45
Reads from uninitialized variables is dangerous :) - static analysis / compiler warnings or some runtime tooling (sanitization or valgrind) may help in detecting such cases.
onqtam
changed the title
Differences between doctest and googletest (gtest) for uninitialised class data members
[question] Differences between doctest and googletest (gtest) for uninitialised local variables in test cases
Aug 23, 2017
This is not a bug report, per se. I'm just trying to understand why doctest produces a different output to gtest (and what I was expecting).
I have a simple Date class with no constructor. I would expect the initialisation of its integer data members (
day
andyear
) to be undefined as described here: https://stackoverflow.com/questions/2417065/does-the-default-constructor-initialize-built-in-typesThe test case in doctest is:
The test case in gtest is:
printDate
is:doctest's output is:
gtest's output is:
Why are the
Date
's integer values default initialised to 0 when using doctest?The text was updated successfully, but these errors were encountered: