1515// specific language governing permissions and limitations
1616// under the License.
1717
18-
1918#include < cstdint>
2019#include < cstdio>
2120#include < cstring>
2726#include " gtest/gtest.h"
2827
2928#include " arrow/array.h"
30- #include " arrow/ipc/json.h"
3129#include " arrow/ipc/json-internal.h"
30+ #include " arrow/ipc/json.h"
3231#include " arrow/test-util.h"
3332#include " arrow/type.h"
3433#include " arrow/type_traits.h"
@@ -74,8 +73,6 @@ void TestArrayRoundTrip(const Array& array) {
7473 std::shared_ptr<Array> out;
7574 ASSERT_OK (ReadJsonArray (default_memory_pool (), d, array.type (), &out));
7675
77- std::cout << array_as_json << std::endl;
78-
7976 ASSERT_TRUE (array.Equals (out)) << array_as_json;
8077}
8178
@@ -98,6 +95,25 @@ void CheckPrimitive(const std::shared_ptr<DataType>& type,
9895 TestArrayRoundTrip (*array.get ());
9996}
10097
98+ template <typename TYPE, typename C_TYPE>
99+ void MakeArray (const std::shared_ptr<DataType>& type,
100+ const std::vector<bool >& is_valid, const std::vector<C_TYPE>& values,
101+ std::shared_ptr<Array>* out) {
102+ std::shared_ptr<Buffer> values_buffer = test::GetBufferFromVector (values);
103+ std::shared_ptr<Buffer> values_bitmap;
104+ ASSERT_OK (test::GetBitmapFromBoolVector (is_valid, &values_bitmap));
105+
106+ using ArrayType = typename TypeTraits<TYPE>::ArrayType;
107+
108+ int32_t null_count = 0 ;
109+ for (bool val : is_valid) {
110+ if (!val) { ++null_count; }
111+ }
112+
113+ *out = std::make_shared<ArrayType>(type, static_cast <int32_t >(values.size ()),
114+ values_buffer, null_count, values_bitmap);
115+ }
116+
101117TEST (TestJsonSchemaWriter, FlatTypes) {
102118 std::vector<std::shared_ptr<Field>> fields = {field (" f0" , int8 ()),
103119 field (" f1" , int16 (), false ), field (" f2" , int32 ()), field (" f3" , int64 (), false ),
@@ -148,11 +164,8 @@ TEST(TestJsonArrayWriter, NestedTypes) {
148164 std::vector<bool > values_is_valid = {true , false , true , true , false , true , true };
149165 std::vector<int32_t > values = {0 , 1 , 2 , 3 , 4 , 5 , 6 };
150166
151- std::shared_ptr<Buffer> values_buffer = test::GetBufferFromVector (values);
152- std::shared_ptr<Buffer> values_bitmap;
153- ASSERT_OK (test::GetBitmapFromBoolVector (values_is_valid, &values_bitmap));
154- auto values_array = std::make_shared<Int32Array>(
155- value_type, static_cast <int32_t >(values.size ()), values_buffer, 2 , values_bitmap);
167+ std::shared_ptr<Array> values_array;
168+ MakeArray<Int32Type, int32_t >(int32 (), values_is_valid, values, &values_array);
156169
157170 // List
158171 std::vector<bool > list_is_valid = {true , false , true , true , true };
@@ -180,5 +193,43 @@ TEST(TestJsonArrayWriter, NestedTypes) {
180193 TestArrayRoundTrip (struct_array);
181194}
182195
196+ TEST (TestJsonFileReadWrite, BasicRoundTrip) {
197+ auto v1_type = int8 ();
198+ auto v2_type = int32 ();
199+ auto v3_type = utf8 ();
200+
201+ std::vector<bool > is_valid = {true , false , true , true , false , true , true };
202+
203+ std::vector<int8_t > v1_values = {0 , 1 , 2 , 3 , 4 , 5 , 6 };
204+ std::shared_ptr<Array> v1;
205+ MakeArray<Int8Type, int8_t >(v1_type, is_valid, v1_values, &v1);
206+
207+ std::vector<int32_t > v2_values = {0 , 1 , 2 , 3 , 4 , 5 , 6 };
208+ std::shared_ptr<Array> v2;
209+ MakeArray<Int32Type, int32_t >(v2_type, is_valid, v2_values, &v2);
210+
211+ std::vector<std::string> v3_values = {" foo" , " bar" , " " , " " , " " , " baz" , " qux" };
212+ std::shared_ptr<Array> v3;
213+ MakeArray<StringType, std::string>(v3_type, is_valid, v3_values, &v3);
214+
215+ std::shared_ptr<Schema> schema ({field (" f1" , v1_type), field (" f2" , v2_type),
216+ field (" f3" , v3_type)});
217+
218+ std::vector<std::shared_ptr<Array>> arrays = {v1, v2, v3}
219+
220+ std::unique_ptr<JsonWriter> writer;
221+ ASSERT_OK (JsonWriter::Open (schema, &writer));
222+
223+ const int nbatches = 3 ;
224+ const int32_t num_rows = static_cast <int32_t >(v1_values.size ());
225+
226+ for (int i = 0 ; i < nbatches; ++i) {
227+ ASSERT_OK (writer_->WriteRecordBatch (arrays, num_rows));
228+ }
229+
230+ std::shared_ptr<Buffer> data;
231+ ASSERT_OK (writer->Finish (&data));
232+ }
233+
183234} // namespace ipc
184235} // namespace arrow
0 commit comments