1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#include <memory>
19#include <string>
20#include <unordered_map>
21#include <vector>
22
23#include <gtest/gtest.h>
24
25#include "arrow/util/key_value_metadata.h"
26
27namespace arrow {
28
29TEST(KeyValueMetadataTest, SimpleConstruction) {
30 KeyValueMetadata metadata;
31 ASSERT_EQ(0, metadata.size());
32}
33
34TEST(KeyValueMetadataTest, StringVectorConstruction) {
35 std::vector<std::string> keys = {"foo", "bar"};
36 std::vector<std::string> values = {"bizz", "buzz"};
37
38 KeyValueMetadata metadata(keys, values);
39 ASSERT_EQ("foo", metadata.key(0));
40 ASSERT_EQ("bar", metadata.key(1));
41 ASSERT_EQ("bizz", metadata.value(0));
42 ASSERT_EQ("buzz", metadata.value(1));
43 ASSERT_EQ(2, metadata.size());
44}
45
46TEST(KeyValueMetadataTest, StringMapConstruction) {
47 std::unordered_map<std::string, std::string> pairs = {{"foo", "bizz"}, {"bar", "buzz"}};
48 std::unordered_map<std::string, std::string> result_map;
49 result_map.reserve(pairs.size());
50
51 KeyValueMetadata metadata(pairs);
52 metadata.ToUnorderedMap(&result_map);
53 ASSERT_EQ(pairs, result_map);
54 ASSERT_EQ(2, metadata.size());
55}
56
57TEST(KeyValueMetadataTest, StringAppend) {
58 std::vector<std::string> keys = {"foo", "bar"};
59 std::vector<std::string> values = {"bizz", "buzz"};
60
61 KeyValueMetadata metadata(keys, values);
62 ASSERT_EQ("foo", metadata.key(0));
63 ASSERT_EQ("bar", metadata.key(1));
64 ASSERT_EQ("bizz", metadata.value(0));
65 ASSERT_EQ("buzz", metadata.value(1));
66 ASSERT_EQ(2, metadata.size());
67
68 metadata.Append("purple", "orange");
69 metadata.Append("blue", "red");
70
71 ASSERT_EQ("purple", metadata.key(2));
72 ASSERT_EQ("blue", metadata.key(3));
73
74 ASSERT_EQ("orange", metadata.value(2));
75 ASSERT_EQ("red", metadata.value(3));
76}
77
78TEST(KeyValueMetadataTest, Copy) {
79 std::vector<std::string> keys = {"foo", "bar"};
80 std::vector<std::string> values = {"bizz", "buzz"};
81
82 KeyValueMetadata metadata(keys, values);
83 auto metadata2 = metadata.Copy();
84 ASSERT_TRUE(metadata.Equals(*metadata2));
85}
86
87TEST(KeyValueMetadataTest, Equals) {
88 std::vector<std::string> keys = {"foo", "bar"};
89 std::vector<std::string> values = {"bizz", "buzz"};
90
91 KeyValueMetadata metadata(keys, values);
92 KeyValueMetadata metadata2(keys, values);
93 KeyValueMetadata metadata3(keys, {"buzz", "bizz"});
94
95 ASSERT_TRUE(metadata.Equals(metadata2));
96 ASSERT_FALSE(metadata.Equals(metadata3));
97}
98
99TEST(KeyValueMetadataTest, ToString) {
100 std::vector<std::string> keys = {"foo", "bar"};
101 std::vector<std::string> values = {"bizz", "buzz"};
102
103 KeyValueMetadata metadata(keys, values);
104
105 std::string result = metadata.ToString();
106 std::string expected = R"(
107-- metadata --
108foo: bizz
109bar: buzz)";
110
111 ASSERT_EQ(expected, result);
112}
113
114} // namespace arrow
115