Skip to content

Commit a790a07

Browse files
author
Naveen Kumar
committed
Extended unit tests.
1 parent 5c47260 commit a790a07

File tree

4 files changed

+99
-6
lines changed

4 files changed

+99
-6
lines changed

unittest/CMakeLists.txt

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
# Locate GTest
2-
find_package(GTest REQUIRED)
2+
# Attempt to find GTest using find_package
3+
4+
# Look for libgtest.a in CMAKE_LIBRARY_PATH
5+
find_library(GTEST_LIBRARY
6+
NAMES gtest
7+
PATHS ${CMAKE_LIBRARY_PATH}
8+
)
9+
10+
# Check if libgtest.a is found
11+
if (GTEST_LIBRARY)
12+
message(STATUS "Found GTest library: ${GTEST_LIBRARY}")
13+
# Set variables expected by GTest
14+
set(GTEST_LIBRARIES ${GTEST_LIBRARY})
15+
# Set the include directories where GTest headers are located
16+
get_filename_component(GTEST_INCLUDE_DIR "${GTEST_LIBRARY}" DIRECTORY)
17+
set(GTEST_INCLUDE_DIRS "${GTEST_INCLUDE_DIR}/../include")
18+
message(STATUS "Found GTest include folder: ${GTEST_INCLUDE_DIRS}")
19+
else()
20+
message(STATUS "GTest library not found, using find_package!")
21+
# Search the standard way and fail the standard way
22+
find_package(GTest REQUIRED)
23+
endif()
24+
325
include_directories(${GTEST_INCLUDE_DIRS})
426

527
# Add all test files in the current directory to a variable
@@ -11,10 +33,11 @@ add_executable(all_unit_tests ${TEST_SOURCES})
1133

1234
# Link against GTest and your_library
1335
target_link_libraries(all_unit_tests ${GTEST_LIBRARIES} pthread rsodbc64-test)
36+
target_include_directories(all_unit_tests PUBLIC ${CMAKE_SOURCE_DIR}/src/logging)
1437
target_include_directories(all_unit_tests PUBLIC ${CMAKE_SOURCE_DIR}/src/odbc/rsodbc)
1538
target_include_directories(all_unit_tests PUBLIC ${CMAKE_SOURCE_DIR}/src/pgclient/src/interfaces/libpq)
1639
target_compile_definitions_rsodbc(all_unit_tests)
1740

18-
gtest_discover_tests(all_unit_tests)
41+
add_test(NAME all_unit_tests COMMAND all_unit_tests)
1942

2043

unittest/bindoffset_test.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "common.h"
2+
#include <rsdesc.h>
3+
#include <rsodbc.h>
4+
#include <sql.h>
5+
6+
TEST(BIND_OFFSET_TEST_SUITE, test_value_loss) {
7+
8+
long offset = 4294967295;
9+
SQLPOINTER offsetPtr = &offset;
10+
RS_ENV_INFO _phenv;
11+
RS_ENV_INFO *_phenv_ = &_phenv;
12+
RS_CONN_INFO _phdbc(_phenv_);
13+
RS_CONN_INFO *_phdbc_ = &_phdbc;
14+
RS_DESC_INFO phdesc_(_phdbc_, 0, 0);
15+
phdesc_.pDescHeader = RS_DESC_HEADER(0);
16+
phdesc_.pDescHeader.valid = 1;
17+
SQLHDESC phdesc = &phdesc_;
18+
19+
RsDesc::RS_SQLSetDescField(phdesc, 0, SQL_DESC_BIND_OFFSET_PTR, offsetPtr,
20+
0, TRUE);
21+
22+
int iBindOffset = 0; // just to depict the buggy behavior
23+
SQLLEN iBindOffset2 = 0;
24+
EXPECT_EQ(iBindOffset, iBindOffset2); // baseline
25+
iBindOffset = *(phdesc_.pDescHeader.plBindOffsetPtr); // integer offset
26+
iBindOffset2 = *(phdesc_.pDescHeader.plBindOffsetPtr); // 64bit offset
27+
EXPECT_NE(iBindOffset, iBindOffset2);
28+
EXPECT_EQ(iBindOffset2, offset); // value is preserved
29+
EXPECT_LT(iBindOffset, 0); // value is lost/rotated (-1)
30+
}

unittest/rslog_test.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
#include "common.h"
3+
#include <rslog.h>
4+
#include <iostream>
5+
#include <fstream>
6+
#include <string>
7+
#include <cstdio>
8+
9+
int findInFile(const std::string& filename, const std::string& target) {
10+
int cnt = 0;
11+
std::ifstream file(filename);
12+
if (!file) {
13+
std::cerr << "Failed to open the file." << std::endl;
14+
return 1;
15+
}
16+
17+
std::string line;
18+
while (std::getline(file, line)) {
19+
if (line.find(target.c_str()) != std::string::npos) {
20+
++cnt;
21+
}
22+
}
23+
24+
file.close();
25+
return cnt;
26+
}
27+
28+
TEST(RSLOG_TEST_SUITE, ascii_37) {
29+
const std::string filename = "rslog_test.log";
30+
std::remove(filename.c_str());
31+
RS_LOG_VARS rsLogVars;
32+
rsLogVars.iTraceLevel = 6;
33+
sprintf(rsLogVars.szTraceFile, filename.c_str());
34+
rsLogVars.isInitialized = 1;
35+
initializeLoggingWithGlobalLogVars(&rsLogVars);
36+
std::string str="%";
37+
RS_LOG_DEBUG("CAT", "%s", "123");
38+
RS_LOG_DEBUG("CAT", "%s", "%");
39+
RS_LOG_DEBUG("CAT", "%s", str.c_str());
40+
shutdownLogging();
41+
ASSERT_EQ(2, findInFile(filename, "%"));
42+
}

unittest/version_test.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,9 @@ TEST(VERSION_TEST_SUITE, odbc_dependency_change_check) {
5353

5454
// Any change to the number or names of dependencies will be detected. And this
5555
// test will enforce a manual check
56+
std::set<std::string> expectedDependencies = {"aws-sdk-cpp", "openssl", "c-ares"};
5657
#ifdef LINUX
57-
std::set<std::string> expectedDependencies = {
58-
"aws-sdk-cpp", "openssl", "c-ares", "curl", "nghttp2", "zlib"};
59-
#else
60-
std::set<std::string> expectedDependencies = {"aws-sdk-cpp", "openssl"};
58+
expectedDependencies.insert({"aws-sdk-cpp", "openssl", "curl", "nghttp2", "zlib"});
6159
#endif
6260
std::vector<std::string> dependencies;
6361

0 commit comments

Comments
 (0)