CPP Unit Testing & Code Quality 2 — Questions and Answers
Question 1: In Google Test, which macro should you use to verify that two floating-point values are approximately equal within a default tolerance?
- EXPECT_EQ
- EXPECT_FLOAT_EQ (Correct answer)
- ASSERT_NEAR
- EXPECT_DOUBLE_EQ
Correct answer: EXPECT_FLOAT_EQ
EXPECT_FLOAT_EQ checks that two float values are within 4 ULPs of each other, which is the standard way to compare floats in GTest.
Question 2: What is a 'test fixture' in the context of C++ unit testing frameworks like Google Test?
- A mock object that replaces a dependency
- A class that provides shared setup and teardown logic for a group of tests (Correct answer)
- A compile-time assertion utility
- A performance benchmarking harness
Correct answer: A class that provides shared setup and teardown logic for a group of tests
A test fixture is a class (often inheriting from ::testing::Test) that encapsulates shared state and setup/teardown methods reused across multiple related test cases.
Question 3: Which clang-tidy check category specifically targets potential C++ modernization opportunities such as replacing loops with range-based for?
- cppcoreguidelines-*
- modernize-* (Correct answer)
- readability-*
- bugprone-*
Correct answer: modernize-*
The modernize-* checks in clang-tidy suggest transformations that move code toward modern C++ idioms like range-based for, nullptr, and override.
Question 4: In test-driven development (TDD), what is the correct order of the 'Red-Green-Refactor' cycle?
- Write passing test → refactor → make test fail
- Write failing test → make it pass → refactor (Correct answer)
- Refactor code → write test → run tests
- Make test pass → write test → refactor
Correct answer: Write failing test → make it pass → refactor
TDD's Red-Green-Refactor cycle starts with a failing test (Red), then writes minimal code to pass it (Green), then cleans up the code (Refactor).
Question 5: What does the AddressSanitizer (ASan) tool primarily detect in C++ programs?
- Data races in multithreaded code
- Memory safety errors such as buffer overflows and use-after-free (Correct answer)
- Integer overflow and undefined arithmetic
- Leaked file descriptors
Correct answer: Memory safety errors such as buffer overflows and use-after-free
AddressSanitizer instruments the binary at compile time to detect memory errors including heap/stack buffer overflows, use-after-free, and use-after-return.
Question 6: Which of the following is the best practice when naming unit test functions in Google Test using TEST_F?
- TestSuiteName_TestName (underscore-separated descriptive names) (Correct answer)
- camelCaseTestName
- test_suite__test_name (double underscore)
- All lowercase with no separators
Correct answer: TestSuiteName_TestName (underscore-separated descriptive names)
Google Test uses TEST_F(TestSuiteName, TestName) where both parts are PascalCase or descriptive identifiers separated by an underscore at the macro level.
Question 7: What is 'code coverage' as measured by tools like gcov or llvm-cov?
- The ratio of functions that compile without warnings
- The percentage of source lines or branches executed during test runs (Correct answer)
- The number of test cases per class
- The fraction of code reviewed by peers
Correct answer: The percentage of source lines or branches executed during test runs
Code coverage measures what fraction of executable lines, branches, or functions are actually exercised when the test suite runs.
In Google Test, which macro should you use to verify that two floating-point values are approximately equal within a default tolerance?