diff --git a/ci/appveyor-cpp-build.bat b/ci/appveyor-cpp-build.bat index 195941e5b138..f59767768c3f 100644 --- a/ci/appveyor-cpp-build.bat +++ b/ci/appveyor-cpp-build.bat @@ -35,6 +35,7 @@ if "%JOB%" == "Static_Crt_Build" ( -DARROW_BOOST_USE_SHARED=OFF ^ -DARROW_BUILD_SHARED=OFF ^ -DCMAKE_BUILD_TYPE=Debug ^ + -DARROW_TEST_LINKAGE=static ^ -DARROW_CXXFLAGS="/MP" ^ .. || exit /B @@ -51,6 +52,7 @@ if "%JOB%" == "Static_Crt_Build" ( -DARROW_BOOST_USE_SHARED=OFF ^ -DARROW_BUILD_SHARED=OFF ^ -DCMAKE_BUILD_TYPE=Release ^ + -DARROW_TEST_LINKAGE=static ^ -DCMAKE_CXX_FLAGS_RELEASE="/MT %CMAKE_CXX_FLAGS_RELEASE%" ^ -DARROW_CXXFLAGS="/WX /MP" ^ .. || exit /B diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index a32ac0f1d974..2361788834d5 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -124,6 +124,10 @@ if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}") "Build the Arrow googletest unit tests" ON) + set(ARROW_TEST_LINKAGE "shared" CACHE STRING + "Linkage of Arrow libraries with unit tests executables. \ +static|shared (default shared)") + set(ARROW_TEST_INCLUDE_LABELS "" CACHE STRING "Only build unit tests having the indicated label or labels. \ Pass multiple labels by dividing with semicolons") @@ -436,8 +440,6 @@ endif() if(NOT ARROW_BUILD_TESTS) set(NO_TESTS 1) -else() - set(ARROW_BUILD_SHARED ON) endif() if(NOT ARROW_BUILD_BENCHMARKS) @@ -683,13 +685,6 @@ if (ARROW_STATIC_LINK_LIBS) add_dependencies(arrow_dependencies ${ARROW_STATIC_LINK_LIBS}) endif() -if (ARROW_BUILD_BENCHMARKS) - set(ARROW_BENCHMARK_LINK_LIBS - arrow_benchmark_main - arrow_static - gtest_static) -endif() - set(ARROW_SHARED_PRIVATE_LINK_LIBS ${ARROW_STATIC_LINK_LIBS} ${BOOST_SYSTEM_LIBRARY} @@ -708,30 +703,50 @@ if (NOT MSVC) ${CMAKE_DL_LIBS}) endif() -if (ARROW_BUILD_STATIC) - set(ARROW_MIN_TEST_LIBS - arrow_static - gtest_main_static - gtest_static) -else () - set(ARROW_MIN_TEST_LIBS - arrow_shared - ${ARROW_LINK_LIBS} - double-conversion_static - ${BOOST_SYSTEM_LIBRARY} - ${BOOST_FILESYSTEM_LIBRARY} - ${BOOST_REGEX_LIBRARY} - gtest_main_static - gtest_static) -endif() +set(ARROW_TEST_STATIC_LINK_LIBS + arrow_testing_static + arrow_static + ${ARROW_LINK_LIBS} + gtest_main_static + gtest_static) + +set(ARROW_TEST_SHARED_LINK_LIBS + arrow_testing_shared + arrow_shared + ${ARROW_LINK_LIBS} + double-conversion_static + ${BOOST_SYSTEM_LIBRARY} + ${BOOST_FILESYSTEM_LIBRARY} + ${BOOST_REGEX_LIBRARY} + gtest_main_static + gtest_static) if(NOT MSVC) - set(ARROW_MIN_TEST_LIBS - ${ARROW_MIN_TEST_LIBS} + set(ARROW_TEST_SHARED_LINK_LIBS + ${ARROW_TEST_SHARED_LINK_LIBS} ${CMAKE_DL_LIBS}) endif() -set(ARROW_TEST_LINK_LIBS ${ARROW_MIN_TEST_LIBS}) +if ("${ARROW_TEST_LINKAGE}" STREQUAL "shared") + if (NOT ARROW_BUILD_SHARED) + message(FATAL_ERROR "If using shared linkage for unit tests, must also \ +pass ARROW_BUILD_SHARED=on") + endif() + # Use shared linking for unit tests if it's available + set(ARROW_TEST_LINK_LIBS ${ARROW_TEST_SHARED_LINK_LIBS}) +else() + if (NOT ARROW_BUILD_STATIC) + message(FATAL_ERROR "If using static linkage for unit tests, must also \ +pass ARROW_BUILD_STATIC=on") + endif() + set(ARROW_TEST_LINK_LIBS ${ARROW_TEST_STATIC_LINK_LIBS}) +endif() + +if (ARROW_BUILD_BENCHMARKS) + set(ARROW_BENCHMARK_LINK_LIBS + arrow_benchmark_main + ${ARROW_TEST_LINK_LIBS}) +endif() if (ARROW_JEMALLOC) add_definitions(-DARROW_JEMALLOC) diff --git a/cpp/cmake_modules/BuildUtils.cmake b/cpp/cmake_modules/BuildUtils.cmake index b79d93cff6fd..57cac82777c8 100644 --- a/cpp/cmake_modules/BuildUtils.cmake +++ b/cpp/cmake_modules/BuildUtils.cmake @@ -259,6 +259,10 @@ function(ADD_ARROW_LIB LIB_NAME) set(LIB_NAME_STATIC ${LIB_NAME}) endif() + if (ARROW_BUILD_STATIC AND WIN32) + target_compile_definitions(${LIB_NAME}_static PUBLIC ARROW_STATIC) + endif() + set_target_properties(${LIB_NAME}_static PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${BUILD_OUTPUT_ROOT_DIRECTORY}" @@ -460,26 +464,6 @@ function(ADD_ARROW_TEST REL_TEST_NAME) LABELS ${ARG_LABELS}) endfunction() -# A wrapper for add_dependencies() that is compatible with NO_TESTS. -function(ADD_ARROW_TEST_DEPENDENCIES REL_TEST_NAME) - if(NO_TESTS) - return() - endif() - get_filename_component(TEST_NAME ${REL_TEST_NAME} NAME_WE) - - add_dependencies(${TEST_NAME} ${ARGN}) -endfunction() - -# A wrapper for target_link_libraries() that is compatible with NO_TESTS. -function(ARROW_TEST_LINK_LIBRARIES REL_TEST_NAME) - if(NO_TESTS) - return() - endif() - get_filename_component(TEST_NAME ${REL_TEST_NAME} NAME_WE) - - target_link_libraries(${TEST_NAME} ${ARGN}) -endfunction() - ############################################################ # Fuzzing ############################################################ diff --git a/cpp/src/arrow/CMakeLists.txt b/cpp/src/arrow/CMakeLists.txt index cec6088d1b44..8c4138beff6b 100644 --- a/cpp/src/arrow/CMakeLists.txt +++ b/cpp/src/arrow/CMakeLists.txt @@ -169,6 +169,28 @@ ADD_ARROW_LIB(arrow SHARED_PRIVATE_LINK_LIBS ${ARROW_SHARED_PRIVATE_LINK_LIBS} STATIC_LINK_LIBS ${ARROW_STATIC_LINK_LIBS}) +if (ARROW_BUILD_STATIC AND WIN32) + target_compile_definitions(arrow_static PUBLIC ARROW_STATIC) +endif() + +if (ARROW_BUILD_TESTS OR ARROW_BUILD_BENCHMARKS) + # that depend on gtest + ADD_ARROW_LIB(arrow_testing + SOURCES test-util.cc + OUTPUTS ARROW_TESTING_LIBRARIES + DEPENDENCIES gtest_static + SHARED_LINK_LIBS arrow_shared gtest_static + STATIC_LINK_LIBS arrow_static) + + if (ARROW_BUILD_STATIC AND WIN32) + target_compile_definitions(arrow_testing_static PUBLIC ARROW_STATIC) + endif() + + set(ARROW_LIBRARIES + ${ARROW_LIBRARIES} + ${ARROW_TESTING_LIBRARIES}) +endif() + find_package(Backtrace) foreach(LIB_TARGET ${ARROW_LIBRARIES}) @@ -180,10 +202,6 @@ foreach(LIB_TARGET ${ARROW_LIBRARIES}) endif() endforeach() -if (ARROW_BUILD_STATIC AND WIN32) - target_compile_definitions(arrow_static PUBLIC ARROW_STATIC) -endif() - # Headers: top level install(FILES allocator.h diff --git a/cpp/src/arrow/dbi/hiveserver2/CMakeLists.txt b/cpp/src/arrow/dbi/hiveserver2/CMakeLists.txt index ba2634b286b7..3a16a7834c3c 100644 --- a/cpp/src/arrow/dbi/hiveserver2/CMakeLists.txt +++ b/cpp/src/arrow/dbi/hiveserver2/CMakeLists.txt @@ -105,9 +105,9 @@ set_property(SOURCE ${ARROW_HIVESERVER2_SRCS} " -Wno-shadow-field") set(ARROW_HIVESERVER2_TEST_LINK_LIBS - ${ARROW_TEST_LINK_LIBS} arrow_hiveserver2_static arrow_hiveserver2_thrift + ${ARROW_TEST_LINK_LIBS} thriftstatic) if (ARROW_BUILD_TESTS) diff --git a/cpp/src/arrow/python/CMakeLists.txt b/cpp/src/arrow/python/CMakeLists.txt index 1c075f83f2d0..ff63eb05675d 100644 --- a/cpp/src/arrow/python/CMakeLists.txt +++ b/cpp/src/arrow/python/CMakeLists.txt @@ -147,6 +147,7 @@ if (ARROW_BUILD_TESTS) set(ARROW_PYTHON_MIN_TEST_LIBS arrow_python_test_main arrow_python_shared + arrow_testing_shared arrow_shared) set(ARROW_PYTHON_TEST_LINK_LIBS ${ARROW_PYTHON_MIN_TEST_LIBS}) diff --git a/cpp/src/arrow/test-util.cc b/cpp/src/arrow/test-util.cc new file mode 100644 index 000000000000..60e5fd39d527 --- /dev/null +++ b/cpp/src/arrow/test-util.cc @@ -0,0 +1,299 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow/test-util.h" + +#ifndef _WIN32 +#include +#include +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "arrow/array.h" +#include "arrow/buffer.h" +#include "arrow/builder.h" +#include "arrow/memory_pool.h" +#include "arrow/pretty_print.h" +#include "arrow/status.h" +#include "arrow/table.h" +#include "arrow/type.h" +#include "arrow/type_traits.h" +#include "arrow/util/bit-util.h" +#include "arrow/util/decimal.h" +#include "arrow/util/logging.h" + +void sleep_for(double seconds) { + std::this_thread::sleep_for( + std::chrono::nanoseconds(static_cast(seconds * 1e9))); +} + +namespace arrow { + +void random_null_bytes(int64_t n, double pct_null, uint8_t* null_bytes) { + const int random_seed = 0; + std::mt19937 gen(random_seed); + std::uniform_real_distribution d(0.0, 1.0); + std::generate(null_bytes, null_bytes + n, + [&d, &gen, &pct_null] { return d(gen) > pct_null; }); +} + +void random_is_valid(int64_t n, double pct_null, std::vector* is_valid) { + const int random_seed = 0; + std::mt19937 gen(random_seed); + std::uniform_real_distribution d(0.0, 1.0); + is_valid->resize(n, false); + std::generate(is_valid->begin(), is_valid->end(), + [&d, &gen, &pct_null] { return d(gen) > pct_null; }); +} + +void random_bytes(int64_t n, uint32_t seed, uint8_t* out) { + std::mt19937 gen(seed); + std::uniform_int_distribution d(0, std::numeric_limits::max()); + std::generate(out, out + n, [&d, &gen] { return static_cast(d(gen)); }); +} + +int32_t DecimalSize(int32_t precision) { + DCHECK_GE(precision, 1) << "decimal precision must be greater than or equal to 1, got " + << precision; + DCHECK_LE(precision, 38) << "decimal precision must be less than or equal to 38, got " + << precision; + + switch (precision) { + case 1: + case 2: + return 1; // 127 + case 3: + case 4: + return 2; // 32,767 + case 5: + case 6: + return 3; // 8,388,607 + case 7: + case 8: + case 9: + return 4; // 2,147,483,427 + case 10: + case 11: + return 5; // 549,755,813,887 + case 12: + case 13: + case 14: + return 6; // 140,737,488,355,327 + case 15: + case 16: + return 7; // 36,028,797,018,963,967 + case 17: + case 18: + return 8; // 9,223,372,036,854,775,807 + case 19: + case 20: + case 21: + return 9; // 2,361,183,241,434,822,606,847 + case 22: + case 23: + return 10; // 604,462,909,807,314,587,353,087 + case 24: + case 25: + case 26: + return 11; // 154,742,504,910,672,534,362,390,527 + case 27: + case 28: + return 12; // 39,614,081,257,132,168,796,771,975,167 + case 29: + case 30: + case 31: + return 13; // 10,141,204,801,825,835,211,973,625,643,007 + case 32: + case 33: + return 14; // 2,596,148,429,267,413,814,265,248,164,610,047 + case 34: + case 35: + return 15; // 664,613,997,892,457,936,451,903,530,140,172,287 + case 36: + case 37: + case 38: + return 16; // 170,141,183,460,469,231,731,687,303,715,884,105,727 + default: + DCHECK(false); + break; + } + return -1; +} + +void random_decimals(int64_t n, uint32_t seed, int32_t precision, uint8_t* out) { + std::mt19937 gen(seed); + std::uniform_int_distribution d(0, std::numeric_limits::max()); + const int32_t required_bytes = DecimalSize(precision); + constexpr int32_t byte_width = 16; + std::fill(out, out + byte_width * n, '\0'); + + for (int64_t i = 0; i < n; ++i, out += byte_width) { + std::generate(out, out + required_bytes, + [&d, &gen] { return static_cast(d(gen)); }); + + // sign extend if the sign bit is set for the last byte generated + // 0b10000000 == 0x80 == 128 + if ((out[required_bytes - 1] & '\x80') != 0) { + std::fill(out + required_bytes, out + byte_width, '\xFF'); + } + } +} + +void random_ascii(int64_t n, uint32_t seed, uint8_t* out) { + rand_uniform_int(n, seed, static_cast('A'), static_cast('z'), out); +} + +int64_t CountNulls(const std::vector& valid_bytes) { + return static_cast(std::count(valid_bytes.cbegin(), valid_bytes.cend(), '\0')); +} + +Status MakeRandomByteBuffer(int64_t length, MemoryPool* pool, + std::shared_ptr* out, uint32_t seed) { + std::shared_ptr result; + RETURN_NOT_OK(AllocateResizableBuffer(pool, length, &result)); + random_bytes(length, seed, result->mutable_data()); + *out = result; + return Status::OK(); +} + +void AssertArraysEqual(const Array& expected, const Array& actual) { + ASSERT_ARRAYS_EQUAL(expected, actual); +} + +void AssertChunkedEqual(const ChunkedArray& expected, const ChunkedArray& actual) { + ASSERT_EQ(expected.num_chunks(), actual.num_chunks()) << "# chunks unequal"; + if (!actual.Equals(expected)) { + std::stringstream pp_result; + std::stringstream pp_expected; + + for (int i = 0; i < actual.num_chunks(); ++i) { + auto c1 = actual.chunk(i); + auto c2 = expected.chunk(i); + if (!c1->Equals(*c2)) { + EXPECT_OK(::arrow::PrettyPrint(*c1, 0, &pp_result)); + EXPECT_OK(::arrow::PrettyPrint(*c2, 0, &pp_expected)); + FAIL() << "Chunk " << i << " Got: " << pp_result.str() + << "\nExpected: " << pp_expected.str(); + } + } + } +} + +void AssertChunkedEqual(const ChunkedArray& actual, const ArrayVector& expected) { + AssertChunkedEqual(ChunkedArray(expected, actual.type()), actual); +} + +void AssertBufferEqual(const Buffer& buffer, const std::vector& expected) { + ASSERT_EQ(buffer.size(), expected.size()) << "Mismatching buffer size"; + const uint8_t* buffer_data = buffer.data(); + for (size_t i = 0; i < expected.size(); ++i) { + ASSERT_EQ(buffer_data[i], expected[i]); + } +} + +void AssertBufferEqual(const Buffer& buffer, const std::string& expected) { + ASSERT_EQ(buffer.size(), expected.length()) << "Mismatching buffer size"; + const uint8_t* buffer_data = buffer.data(); + for (size_t i = 0; i < expected.size(); ++i) { + ASSERT_EQ(buffer_data[i], expected[i]); + } +} + +void AssertBufferEqual(const Buffer& buffer, const Buffer& expected) { + ASSERT_EQ(buffer.size(), expected.size()) << "Mismatching buffer size"; + ASSERT_TRUE(buffer.Equals(expected)); +} + +void AssertSchemaEqual(const Schema& lhs, const Schema& rhs) { + if (!lhs.Equals(rhs)) { + std::stringstream ss; + ss << "left schema: " << lhs.ToString() << std::endl + << "right schema: " << rhs.ToString() << std::endl; + FAIL() << ss.str(); + } +} + +void PrintColumn(const Column& col, std::stringstream* ss) { + const ChunkedArray& carr = *col.data(); + for (int i = 0; i < carr.num_chunks(); ++i) { + auto c1 = carr.chunk(i); + *ss << "Chunk " << i << std::endl; + EXPECT_OK(::arrow::PrettyPrint(*c1, 0, ss)); + *ss << std::endl; + } +} + +void AssertTablesEqual(const Table& expected, const Table& actual, + bool same_chunk_layout) { + ASSERT_EQ(expected.num_columns(), actual.num_columns()); + + if (same_chunk_layout) { + for (int i = 0; i < actual.num_columns(); ++i) { + AssertChunkedEqual(*expected.column(i)->data(), *actual.column(i)->data()); + } + } else { + std::stringstream ss; + if (!actual.Equals(expected)) { + for (int i = 0; i < expected.num_columns(); ++i) { + ss << "Actual column " << i << std::endl; + PrintColumn(*actual.column(i), &ss); + + ss << "Expected column " << i << std::endl; + PrintColumn(*expected.column(i), &ss); + } + FAIL() << ss.str(); + } + } +} + +void CompareBatch(const RecordBatch& left, const RecordBatch& right) { + if (!left.schema()->Equals(*right.schema())) { + FAIL() << "Left schema: " << left.schema()->ToString() + << "\nRight schema: " << right.schema()->ToString(); + } + ASSERT_EQ(left.num_columns(), right.num_columns()) + << left.schema()->ToString() << " result: " << right.schema()->ToString(); + ASSERT_EQ(left.num_rows(), right.num_rows()); + for (int i = 0; i < left.num_columns(); ++i) { + if (!left.column(i)->Equals(right.column(i))) { + std::stringstream ss; + ss << "Idx: " << i << " Name: " << left.column_name(i); + ss << std::endl << "Left: "; + ASSERT_OK(PrettyPrint(*left.column(i), 0, &ss)); + ss << std::endl << "Right: "; + ASSERT_OK(PrettyPrint(*right.column(i), 0, &ss)); + FAIL() << ss.str(); + } + } +} + +} // namespace arrow diff --git a/cpp/src/arrow/test-util.h b/cpp/src/arrow/test-util.h index c4ce57d2c0c8..296aa8833d3a 100644 --- a/cpp/src/arrow/test-util.h +++ b/cpp/src/arrow/test-util.h @@ -51,11 +51,7 @@ #include "arrow/util/bit-util.h" #include "arrow/util/decimal.h" #include "arrow/util/logging.h" - -static inline void sleep_for(double seconds) { - std::this_thread::sleep_for( - std::chrono::nanoseconds(static_cast(seconds * 1e9))); -} +#include "arrow/util/visibility.h" #define STRINGIFY(x) #x @@ -165,117 +161,36 @@ static inline Status GetBitmapFromVector(const std::vector& is_valid, // Sets approximately pct_null of the first n bytes in null_bytes to zero // and the rest to non-zero (true) values. -static inline void random_null_bytes(int64_t n, double pct_null, uint8_t* null_bytes) { - const int random_seed = 0; - std::mt19937 gen(random_seed); - std::uniform_real_distribution d(0.0, 1.0); - std::generate(null_bytes, null_bytes + n, - [&d, &gen, &pct_null] { return d(gen) > pct_null; }); -} - -static inline void random_is_valid(int64_t n, double pct_null, - std::vector* is_valid) { - const int random_seed = 0; - std::mt19937 gen(random_seed); - std::uniform_real_distribution d(0.0, 1.0); - is_valid->resize(n, false); - std::generate(is_valid->begin(), is_valid->end(), - [&d, &gen, &pct_null] { return d(gen) > pct_null; }); -} - -static inline void random_bytes(int64_t n, uint32_t seed, uint8_t* out) { - std::mt19937 gen(seed); - std::uniform_int_distribution d(0, std::numeric_limits::max()); - std::generate(out, out + n, [&d, &gen] { return static_cast(d(gen)); }); -} - -static int32_t DecimalSize(int32_t precision) { - DCHECK_GE(precision, 1) << "decimal precision must be greater than or equal to 1, got " - << precision; - DCHECK_LE(precision, 38) << "decimal precision must be less than or equal to 38, got " - << precision; - - switch (precision) { - case 1: - case 2: - return 1; // 127 - case 3: - case 4: - return 2; // 32,767 - case 5: - case 6: - return 3; // 8,388,607 - case 7: - case 8: - case 9: - return 4; // 2,147,483,427 - case 10: - case 11: - return 5; // 549,755,813,887 - case 12: - case 13: - case 14: - return 6; // 140,737,488,355,327 - case 15: - case 16: - return 7; // 36,028,797,018,963,967 - case 17: - case 18: - return 8; // 9,223,372,036,854,775,807 - case 19: - case 20: - case 21: - return 9; // 2,361,183,241,434,822,606,847 - case 22: - case 23: - return 10; // 604,462,909,807,314,587,353,087 - case 24: - case 25: - case 26: - return 11; // 154,742,504,910,672,534,362,390,527 - case 27: - case 28: - return 12; // 39,614,081,257,132,168,796,771,975,167 - case 29: - case 30: - case 31: - return 13; // 10,141,204,801,825,835,211,973,625,643,007 - case 32: - case 33: - return 14; // 2,596,148,429,267,413,814,265,248,164,610,047 - case 34: - case 35: - return 15; // 664,613,997,892,457,936,451,903,530,140,172,287 - case 36: - case 37: - case 38: - return 16; // 170,141,183,460,469,231,731,687,303,715,884,105,727 - default: - DCHECK(false); - break; - } - return -1; -} - -static inline void random_decimals(int64_t n, uint32_t seed, int32_t precision, - uint8_t* out) { - std::mt19937 gen(seed); - std::uniform_int_distribution d(0, std::numeric_limits::max()); - const int32_t required_bytes = DecimalSize(precision); - constexpr int32_t byte_width = 16; - std::fill(out, out + byte_width * n, '\0'); - - for (int64_t i = 0; i < n; ++i, out += byte_width) { - std::generate(out, out + required_bytes, - [&d, &gen] { return static_cast(d(gen)); }); - - // sign extend if the sign bit is set for the last byte generated - // 0b10000000 == 0x80 == 128 - if ((out[required_bytes - 1] & '\x80') != 0) { - std::fill(out + required_bytes, out + byte_width, '\xFF'); - } - } -} +ARROW_EXPORT void random_null_bytes(int64_t n, double pct_null, uint8_t* null_bytes); +ARROW_EXPORT void random_is_valid(int64_t n, double pct_null, + std::vector* is_valid); +ARROW_EXPORT void random_bytes(int64_t n, uint32_t seed, uint8_t* out); +ARROW_EXPORT int32_t DecimalSize(int32_t precision); +ARROW_EXPORT void random_decimals(int64_t n, uint32_t seed, int32_t precision, + uint8_t* out); +ARROW_EXPORT void random_ascii(int64_t n, uint32_t seed, uint8_t* out); +ARROW_EXPORT int64_t CountNulls(const std::vector& valid_bytes); + +ARROW_EXPORT Status MakeRandomByteBuffer(int64_t length, MemoryPool* pool, + std::shared_ptr* out, + uint32_t seed = 0); + +ARROW_EXPORT void AssertArraysEqual(const Array& expected, const Array& actual); +ARROW_EXPORT void AssertChunkedEqual(const ChunkedArray& expected, + const ChunkedArray& actual); +ARROW_EXPORT void AssertChunkedEqual(const ChunkedArray& actual, + const ArrayVector& expected); +ARROW_EXPORT void AssertBufferEqual(const Buffer& buffer, + const std::vector& expected); +ARROW_EXPORT void AssertBufferEqual(const Buffer& buffer, const std::string& expected); +ARROW_EXPORT void AssertBufferEqual(const Buffer& buffer, const Buffer& expected); +ARROW_EXPORT void AssertSchemaEqual(const Schema& lhs, const Schema& rhs); + +ARROW_EXPORT void PrintColumn(const Column& col, std::stringstream* ss); +ARROW_EXPORT void AssertTablesEqual(const Table& expected, const Table& actual, + bool same_chunk_layout = true); + +ARROW_EXPORT void CompareBatch(const RecordBatch& left, const RecordBatch& right); template void rand_uniform_int(int64_t n, uint32_t seed, T min_value, T max_value, U* out) { @@ -296,14 +211,6 @@ struct GenerateRandom::value>::ty } }; -static inline void random_ascii(int64_t n, uint32_t seed, uint8_t* out) { - rand_uniform_int(n, seed, static_cast('A'), static_cast('z'), out); -} - -static inline int64_t CountNulls(const std::vector& valid_bytes) { - return static_cast(std::count(valid_bytes.cbegin(), valid_bytes.cend(), '\0')); -} - template Status MakeRandomBuffer(int64_t length, MemoryPool* pool, std::shared_ptr* out, uint32_t seed = 0) { @@ -315,105 +222,6 @@ Status MakeRandomBuffer(int64_t length, MemoryPool* pool, return Status::OK(); } -Status MakeRandomByteBuffer(int64_t length, MemoryPool* pool, - std::shared_ptr* out, uint32_t seed = 0) { - std::shared_ptr result; - RETURN_NOT_OK(AllocateResizableBuffer(pool, length, &result)); - random_bytes(length, seed, result->mutable_data()); - *out = result; - return Status::OK(); -} - -void AssertArraysEqual(const Array& expected, const Array& actual) { - ASSERT_ARRAYS_EQUAL(expected, actual); -} - -void AssertChunkedEqual(const ChunkedArray& expected, const ChunkedArray& actual) { - ASSERT_EQ(expected.num_chunks(), actual.num_chunks()) << "# chunks unequal"; - if (!actual.Equals(expected)) { - std::stringstream pp_result; - std::stringstream pp_expected; - - for (int i = 0; i < actual.num_chunks(); ++i) { - auto c1 = actual.chunk(i); - auto c2 = expected.chunk(i); - if (!c1->Equals(*c2)) { - EXPECT_OK(::arrow::PrettyPrint(*c1, 0, &pp_result)); - EXPECT_OK(::arrow::PrettyPrint(*c2, 0, &pp_expected)); - FAIL() << "Chunk " << i << " Got: " << pp_result.str() - << "\nExpected: " << pp_expected.str(); - } - } - } -} - -void AssertChunkedEqual(const ChunkedArray& actual, const ArrayVector& expected) { - AssertChunkedEqual(ChunkedArray(expected, actual.type()), actual); -} - -void AssertBufferEqual(const Buffer& buffer, const std::vector& expected) { - ASSERT_EQ(buffer.size(), expected.size()) << "Mismatching buffer size"; - const uint8_t* buffer_data = buffer.data(); - for (size_t i = 0; i < expected.size(); ++i) { - ASSERT_EQ(buffer_data[i], expected[i]); - } -} - -void AssertBufferEqual(const Buffer& buffer, const std::string& expected) { - ASSERT_EQ(buffer.size(), expected.length()) << "Mismatching buffer size"; - const uint8_t* buffer_data = buffer.data(); - for (size_t i = 0; i < expected.size(); ++i) { - ASSERT_EQ(buffer_data[i], expected[i]); - } -} - -void AssertBufferEqual(const Buffer& buffer, const Buffer& expected) { - ASSERT_EQ(buffer.size(), expected.size()) << "Mismatching buffer size"; - ASSERT_TRUE(buffer.Equals(expected)); -} - -static inline void AssertSchemaEqual(const Schema& lhs, const Schema& rhs) { - if (!lhs.Equals(rhs)) { - std::stringstream ss; - ss << "left schema: " << lhs.ToString() << std::endl - << "right schema: " << rhs.ToString() << std::endl; - FAIL() << ss.str(); - } -} - -void PrintColumn(const Column& col, std::stringstream* ss) { - const ChunkedArray& carr = *col.data(); - for (int i = 0; i < carr.num_chunks(); ++i) { - auto c1 = carr.chunk(i); - *ss << "Chunk " << i << std::endl; - EXPECT_OK(::arrow::PrettyPrint(*c1, 0, ss)); - *ss << std::endl; - } -} - -void AssertTablesEqual(const Table& expected, const Table& actual, - bool same_chunk_layout = true) { - ASSERT_EQ(expected.num_columns(), actual.num_columns()); - - if (same_chunk_layout) { - for (int i = 0; i < actual.num_columns(); ++i) { - AssertChunkedEqual(*expected.column(i)->data(), *actual.column(i)->data()); - } - } else { - std::stringstream ss; - if (!actual.Equals(expected)) { - for (int i = 0; i < expected.num_columns(); ++i) { - ss << "Actual column " << i << std::endl; - PrintColumn(*actual.column(i), &ss); - - ss << "Expected column " << i << std::endl; - PrintColumn(*expected.column(i), &ss); - } - FAIL() << ss.str(); - } - } -} - template void ArrayFromVector(const std::shared_ptr& type, const std::vector& is_valid, const std::vector& values, @@ -534,27 +342,6 @@ Status MakeArray(const std::vector& valid_bytes, const std::vector& } \ } while (false) -static inline void CompareBatch(const RecordBatch& left, const RecordBatch& right) { - if (!left.schema()->Equals(*right.schema())) { - FAIL() << "Left schema: " << left.schema()->ToString() - << "\nRight schema: " << right.schema()->ToString(); - } - ASSERT_EQ(left.num_columns(), right.num_columns()) - << left.schema()->ToString() << " result: " << right.schema()->ToString(); - ASSERT_EQ(left.num_rows(), right.num_rows()); - for (int i = 0; i < left.num_columns(); ++i) { - if (!left.column(i)->Equals(right.column(i))) { - std::stringstream ss; - ss << "Idx: " << i << " Name: " << left.column_name(i); - ss << std::endl << "Left: "; - ASSERT_OK(PrettyPrint(*left.column(i), 0, &ss)); - ss << std::endl << "Right: "; - ASSERT_OK(PrettyPrint(*right.column(i), 0, &ss)); - FAIL() << ss.str(); - } - } -} - // ---------------------------------------------------------------------- // A RecordBatchReader for serving a sequence of in-memory record batches diff --git a/cpp/src/parquet/CMakeLists.txt b/cpp/src/parquet/CMakeLists.txt index 0381a7b229b1..1538b58164b6 100644 --- a/cpp/src/parquet/CMakeLists.txt +++ b/cpp/src/parquet/CMakeLists.txt @@ -58,9 +58,13 @@ endfunction() if (ARROW_BUILD_STATIC) set(PARQUET_STATIC_LINK_LIBS arrow_static) - set(ARROW_LIBRARY_TESTING arrow_static) + set(ARROW_LIBRARIES_FOR_STATIC_TESTS + arrow_testing_static + arrow_static) else() - set(ARROW_LIBRARY_TESTING arrow_shared) + set(ARROW_LIBRARIES_FOR_STATIC_TESTS + arrow_testing_shared + arrow_shared) endif() set(PARQUET_BOOST_LINK_LIBS @@ -87,13 +91,14 @@ elseif(NOT MSVC) endif() set(PARQUET_SHARED_TEST_LINK_LIBS + arrow_testing_shared ${PARQUET_MIN_TEST_LIBS} parquet_shared thriftstatic) set(PARQUET_STATIC_TEST_LINK_LIBS ${PARQUET_MIN_TEST_LIBS} - ${ARROW_LIBRARY_TESTING} + ${ARROW_LIBRARIES_FOR_STATIC_TESTS} parquet_static) set(PARQUET_BENCHMARK_LINK_LIBRARIES