Skip to content

Commit 8fda70e

Browse files
committed
[Code] Fix get matrix marker logic
1 parent e8a5633 commit 8fda70e

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

cubool/include/cubool/cubool.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Matrix_SetMarker(
290290
* @note Pass null marker if you want to retrieve only the required marker buffer size.
291291
* @note After the function call the actual size of the marker is stored in the size variable.
292292
*
293+
* @note size is set to the actual marker length plus null terminator symbol.
294+
* For marker "matrix" size variable will be set to 7.
295+
*
293296
* @param matrix Matrix handle to perform operation on
294297
* @param[in,out] marker Where to store null-terminated UTF-8 encoded marker string.
295298
* @param[in,out] size Size of the provided buffer in bytes to save marker string.

cubool/sources/cuBool_Matrix_Marker.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include <cuBool_Common.hpp>
2626
#include <cstring>
27+
#include <cmath>
2728

2829
cuBool_Status cuBool_Matrix_Marker(
2930
cuBool_Matrix matrix,
@@ -37,11 +38,15 @@ cuBool_Status cuBool_Matrix_Marker(
3738

3839
auto m = (cubool::Matrix*) matrix;
3940
auto actualSize = m->getDebugMarkerSizeWithNullT();
41+
auto toCopy = std::min(*size, actualSize);
4042

41-
if (marker != nullptr && *size > 0) {
43+
if (marker != nullptr && toCopy > 0) {
44+
// C str (with \0)
4245
const auto* text = m->getDebugMarker();
43-
std::memcpy(marker, text, *size);
44-
marker[*size - 1] = '\0';
46+
std::memcpy(marker, text, toCopy);
47+
48+
// Explicitly terminate (for case size < actualSize)
49+
marker[toCopy - 1] = '\0';
4550
}
4651

4752
*size = actualSize;

cubool/tests/test_matrix_misc.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
/**********************************************************************************/
2424

2525
#include <testing/testing.hpp>
26+
#include <cstring>
2627

2728
TEST(cuBool_Matrix, Duplicate) {
2829
cuBool_Matrix matrix = nullptr, duplicated = nullptr;
@@ -98,4 +99,55 @@ TEST(cuBool_Matrix, ExtractPairs) {
9899
ASSERT_EQ(cuBool_Finalize(), CUBOOL_STATUS_SUCCESS);
99100
}
100101

102+
TEST(cuBool_Matrix, Marker) {
103+
cuBool_Matrix matrix = nullptr;
104+
cuBool_Index m, n;
105+
106+
m = n = 100;
107+
108+
const cuBool_Index BUFFER_SIZE = 100;
109+
const char* marker = "Test Matrix Marker";
110+
cuBool_Index size = 0;
111+
char buffer[BUFFER_SIZE];
112+
113+
ASSERT_EQ(cuBool_Initialize(CUBOOL_HINT_NO), CUBOOL_STATUS_SUCCESS);
114+
ASSERT_EQ(cuBool_Matrix_New(&matrix, m, n), CUBOOL_STATUS_SUCCESS);
115+
ASSERT_EQ(cuBool_Matrix_SetMarker(matrix, marker), CUBOOL_STATUS_SUCCESS);
116+
ASSERT_EQ(cuBool_Matrix_Marker(matrix, nullptr, &size), CUBOOL_STATUS_SUCCESS);
117+
ASSERT_LE(size, BUFFER_SIZE);
118+
ASSERT_EQ(cuBool_Matrix_Marker(matrix, buffer, &size), CUBOOL_STATUS_SUCCESS);
119+
ASSERT_EQ(cuBool_Matrix_Free(matrix), CUBOOL_STATUS_SUCCESS);
120+
ASSERT_EQ(cuBool_Finalize(), CUBOOL_STATUS_SUCCESS);
121+
122+
ASSERT_EQ(std::strlen(buffer), size - 1);
123+
ASSERT_LE(std::strcmp(marker, buffer), 0);
124+
125+
std::cout << "Source marker: " << marker << std::endl;
126+
std::cout << "Returned marker: " << buffer << std::endl;
127+
}
128+
129+
TEST(cuBool_Matrix, MarkerShort) {
130+
cuBool_Matrix matrix = nullptr;
131+
cuBool_Index m, n;
132+
133+
m = n = 100;
134+
135+
const cuBool_Index BUFFER_SIZE = 10;
136+
const char* marker = "Test Matrix Marker";
137+
cuBool_Index size = BUFFER_SIZE;
138+
char buffer[BUFFER_SIZE];
139+
140+
ASSERT_EQ(cuBool_Initialize(CUBOOL_HINT_NO), CUBOOL_STATUS_SUCCESS);
141+
ASSERT_EQ(cuBool_Matrix_New(&matrix, m, n), CUBOOL_STATUS_SUCCESS);
142+
ASSERT_EQ(cuBool_Matrix_SetMarker(matrix, marker), CUBOOL_STATUS_SUCCESS);
143+
ASSERT_EQ(cuBool_Matrix_Marker(matrix, buffer, &size), CUBOOL_STATUS_SUCCESS);
144+
ASSERT_EQ(cuBool_Matrix_Free(matrix), CUBOOL_STATUS_SUCCESS);
145+
ASSERT_EQ(cuBool_Finalize(), CUBOOL_STATUS_SUCCESS);
146+
147+
ASSERT_GE(std::strcmp(marker, buffer), 0);
148+
149+
std::cout << "Source marker: " << marker << std::endl;
150+
std::cout << "Returned marker: " << buffer << std::endl;
151+
}
152+
101153
CUBOOL_GTEST_MAIN

0 commit comments

Comments
 (0)