]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/csharp/test/Apache.Arrow.Tests/TypeTests.cs
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / csharp / test / Apache.Arrow.Tests / TypeTests.cs
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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15
16 using Apache.Arrow.Types;
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 using Xunit;
21 using Xunit.Sdk;
22
23 namespace Apache.Arrow.Tests
24 {
25 public class TypeTests
26 {
27 [Fact]
28 public void Basics()
29 {
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();
33
34 Assert.True(f0_nullable.Name == "f0");
35 Assert.True(f0_nullable.DataType.Name == Int32Type.Default.Name);
36
37 Assert.True(f0_nullable.IsNullable);
38 Assert.False(f0_nonnullable.IsNullable);
39 }
40
41 [Fact]
42 public void Equality()
43 {
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();
48
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));
52 }
53
54 [Fact]
55 public void TestMetadataConstruction()
56 {
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);
63 }
64
65 [Fact]
66 public void TestStructBasics()
67 {
68
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();
72
73 List<Field> fields = new List<Field>() { f0_nullable, f1_nullable, f2_nullable };
74 StructType struct_type = new StructType(fields);
75
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);
80 }
81
82 [Fact]
83 public void TestStructGetFieldByName()
84 {
85
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();
89
90 List<Field> fields = new List<Field>() { f0_nullable, f1_nullable, f2_nullable };
91 StructType struct_type = new StructType(fields);
92
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);
97 }
98
99 [Fact]
100 public void TestStructGetFieldIndex()
101 {
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();
105
106 StructType struct_type = new StructType(new[] { f0_nullable, f1_nullable, f2_nullable });
107
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"));
114 }
115
116 [Fact]
117 public void TestListTypeConstructor()
118 {
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);
122
123 FieldComparer.Compare(stringType1.ValueField, stringType2.ValueField);
124 Assert.Equal(stringType1.ValueDataType.TypeId, stringType2.ValueDataType.TypeId);
125 }
126
127 // Todo: StructType::GetFieldIndexDuplicate test
128
129
130 }
131 }