1 // Licensed to the Apache Software Foundation (ASF) under one or more
2 // contributor license agreements. See the NOTICE file distributed with
3 // this work for additional information regarding copyright ownership.
4 // The ASF licenses this file to You under the Apache License, Version 2.0
5 // (the "License"); you may not use this file except in compliance with
6 // the License. You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
16 using Apache.Arrow.Types;
18 using System.Collections.Generic;
23 namespace Apache.Arrow.Tests
25 public class TypeTests
30 Field.Builder fb = new Field.Builder();
31 Field f0_nullable = fb.Name("f0").DataType(Int32Type.Default).Build();
32 Field f0_nonnullable = fb.Name("f0").DataType(Int32Type.Default).Nullable(false).Build();
34 Assert.True(f0_nullable.Name == "f0");
35 Assert.True(f0_nullable.DataType.Name == Int32Type.Default.Name);
37 Assert.True(f0_nullable.IsNullable);
38 Assert.False(f0_nonnullable.IsNullable);
42 public void Equality()
44 Field f0_nullable = new Field.Builder().Name("f0").DataType(Int32Type.Default).Build();
45 Field f0_nonnullable = new Field.Builder().Name("f0").DataType(Int32Type.Default).Nullable(false).Build();
46 Field f0_other = new Field.Builder().Name("f0").DataType(Int32Type.Default).Build();
47 Field f0_with_meta = new Field.Builder().Name("f0").DataType(Int32Type.Default).Nullable(true).Metadata("a", "1").Metadata("b", "2").Build();
49 FieldComparer.Compare(f0_nullable, f0_other);
50 Assert.Throws<EqualException>(() => FieldComparer.Compare(f0_nullable, f0_nonnullable));
51 Assert.Throws<EqualException>(() => FieldComparer.Compare(f0_nullable, f0_with_meta));
55 public void TestMetadataConstruction()
57 var metadata = new Dictionary<string, string> { { "foo", "bar" }, { "bizz", "buzz" } };
58 var metadata1 = new Dictionary<string, string>(metadata);
59 Field f0_nullable = new Field.Builder().Name("f0").DataType(Int32Type.Default).Metadata(metadata).Build();
60 Field f1_nullable = new Field.Builder().Name("f0").DataType(Int32Type.Default).Metadata(metadata1).Build();
61 Assert.True(metadata.Keys.SequenceEqual(f0_nullable.Metadata.Keys) && metadata.Values.SequenceEqual(f0_nullable.Metadata.Values));
62 FieldComparer.Compare(f0_nullable, f1_nullable);
66 public void TestStructBasics()
69 Field f0_nullable = new Field.Builder().Name("f0").DataType(Int32Type.Default).Build();
70 Field f1_nullable = new Field.Builder().Name("f1").DataType(StringType.Default).Build();
71 Field f2_nullable = new Field.Builder().Name("f2").DataType(UInt8Type.Default).Build();
73 List<Field> fields = new List<Field>() { f0_nullable, f1_nullable, f2_nullable };
74 StructType struct_type = new StructType(fields);
76 var structFields = struct_type.Fields;
77 FieldComparer.Compare(structFields.ElementAt(0), f0_nullable);
78 FieldComparer.Compare(structFields.ElementAt(1), f1_nullable);
79 FieldComparer.Compare(structFields.ElementAt(2), f2_nullable);
83 public void TestStructGetFieldByName()
86 Field f0_nullable = new Field.Builder().Name("f0").DataType(Int32Type.Default).Build();
87 Field f1_nullable = new Field.Builder().Name("f1").DataType(StringType.Default).Build();
88 Field f2_nullable = new Field.Builder().Name("f2").DataType(UInt8Type.Default).Build();
90 List<Field> fields = new List<Field>() { f0_nullable, f1_nullable, f2_nullable };
91 StructType struct_type = new StructType(fields);
93 FieldComparer.Compare(struct_type.GetFieldByName("f0"), f0_nullable);
94 FieldComparer.Compare(struct_type.GetFieldByName("f1"), f1_nullable);
95 FieldComparer.Compare(struct_type.GetFieldByName("f2"), f2_nullable);
96 Assert.True(struct_type.GetFieldByName("not_found") == null);
100 public void TestStructGetFieldIndex()
102 Field f0_nullable = new Field.Builder().Name("f0").DataType(Int32Type.Default).Build();
103 Field f1_nullable = new Field.Builder().Name("f1").DataType(StringType.Default).Build();
104 Field f2_nullable = new Field.Builder().Name("f2").DataType(UInt8Type.Default).Build();
106 StructType struct_type = new StructType(new[] { f0_nullable, f1_nullable, f2_nullable });
108 Assert.Equal(0, struct_type.GetFieldIndex("f0"));
109 Assert.Equal(1, struct_type.GetFieldIndex("f1"));
110 Assert.Equal(2, struct_type.GetFieldIndex("F2", StringComparer.OrdinalIgnoreCase));
111 Assert.Equal(-1, struct_type.GetFieldIndex("F2"));
112 Assert.Equal(-1, struct_type.GetFieldIndex("F2", StringComparer.Ordinal));
113 Assert.Equal(-1, struct_type.GetFieldIndex("not_found"));
117 public void TestListTypeConstructor()
119 var stringField = new Field.Builder().Name("item").DataType(StringType.Default).Build();
120 var stringType1 = new ListType(stringField);
121 var stringType2 = new ListType(StringType.Default);
123 FieldComparer.Compare(stringType1.ValueField, stringType2.ValueField);
124 Assert.Equal(stringType1.ValueDataType.TypeId, stringType2.ValueDataType.TypeId);
127 // Todo: StructType::GetFieldIndexDuplicate test