Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions lang/csharp/src/apache/main/Schema/UnionSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,21 @@ public int MatchingBranch(Schema s)
{
if (s is UnionSchema) throw new AvroException("Cannot find a match against union schema");
// Try exact match.
//for (int i = 0; i < Count; i++) if (Schemas[i].Equals(s)) return i; // removed this for performance's sake
for (int i = 0; i < Count; i++) if (Schemas[i].CanRead(s)) return i;
// CanRead might find a compatible schema which can read. e.g. double and long
for (int i = 0; i < Count; i++)
{
if (Schemas[i].Equals(s))
{
return i;
}
}
for (int i = 0; i < Count; i++)
{
if (Schemas[i].CanRead(s))
{
return i;
}
}
return -1;
}

Expand Down
12 changes: 12 additions & 0 deletions lang/csharp/src/apache/test/Generic/GenericTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ private static void test<T>(string s, T value)
[TestCase("[\"int\", \"long\"]", 100L)]
[TestCase("[\"float\", \"double\"]", 100.75)]
[TestCase("[\"float\", \"double\"]", 23.67f)]
[TestCase("[\"float\", \"int\"]", 0)]
[TestCase("[\"float\", \"int\"]", 0.0f)]
[TestCase("[\"float\", \"int\"]", 100)]
[TestCase("[\"float\", \"int\"]", 100.0f)]
[TestCase("[\"float\", \"int\"]", -100)]
[TestCase("[\"float\", \"int\"]", -100.0f)]
[TestCase("[\"double\", \"long\"]", 0L)]
[TestCase("[\"double\", \"long\"]", 0.0)]
[TestCase("[\"double\", \"long\"]", 100L)]
[TestCase("[\"double\", \"long\"]", 100.0)]
[TestCase("[\"double\", \"long\"]", -100L)]
[TestCase("[\"double\", \"long\"]", -100.0)]
[TestCase("[{\"type\": \"array\", \"items\": \"float\"}, \"double\"]", new float[] { 23.67f, 22.78f })]
[TestCase("[{\"type\": \"array\", \"items\": \"float\"}, \"double\"]", 100.89)]
[TestCase("[{\"type\": \"array\", \"items\": \"string\"}, \"string\"]", "a")]
Expand Down
73 changes: 73 additions & 0 deletions lang/csharp/src/apache/test/Specific/DoubleLongUnionRecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* 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
*
* https://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.
*/
// ------------------------------------------------------------------------------
// <auto-generated>
// Generated by avrogen, version 1.11.0.0
// Changes to this file may cause incorrect behavior and will be lost if code
// is regenerated
// </auto-generated>
// ------------------------------------------------------------------------------
namespace Avro.Test.Specific
{
using System;
using System.Collections.Generic;
using System.Text;
using Avro;
using Avro.Specific;

public partial class DoubleLongUnionRecord : ISpecificRecord
{
public static Schema _SCHEMA = Avro.Schema.Parse("{\"type\":\"record\",\"name\":\"DoubleLongUnionRecord\",\"namespace\":\"Avro.Test.Specific\",\"fields\":[{\"name" +
"\":\"Property\",\"type\":[\"double\",\"long\"]}]}");
private object _Property;
public virtual Schema Schema
{
get
{
return DoubleLongUnionRecord._SCHEMA;
}
}
public object Property
{
get
{
return this._Property;
}
set
{
this._Property = value;
}
}
public virtual object Get(int fieldPos)
{
switch (fieldPos)
{
case 0: return this.Property;
default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Get()");
};
}
public virtual void Put(int fieldPos, object fieldValue)
{
switch (fieldPos)
{
case 0: this.Property = (System.Object)fieldValue; break;
default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Put()");
};
}
}
}
32 changes: 32 additions & 0 deletions lang/csharp/src/apache/test/Specific/SpecificTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,38 @@ public void TestEnumDefault()
Assert.AreEqual(EnumType.DEFAULT, rec2.enumType);
}

[TestCase(0L)]
[TestCase(100L)]
[TestCase(-100L)]
[TestCase(0.0)]
[TestCase(100.0)]
[TestCase(-100.0)]
public void TestDoubleLongUnion(object value)
{
var testRecord = new DoubleLongUnionRecord();
testRecord.Property = value;

// serialize
var stream = serialize(DoubleLongUnionRecord._SCHEMA, testRecord);

// deserialize
var rec2 = deserialize<DoubleLongUnionRecord>(stream, DoubleLongUnionRecord._SCHEMA, DoubleLongUnionRecord._SCHEMA);
Assert.AreEqual(value, rec2.Property);
Assert.AreEqual(value.GetType(), rec2.Property.GetType());
}

[TestCase(0)]
[TestCase(100)]
[TestCase(-100)]
[TestCase(0.0f)]
[TestCase(100.0f)]
[TestCase(-100.0f)]
[TestCase("0")]
[TestCase("100")]
public void TestDoubleLongUnionNoMatchException(object value)
{
Assert.Throws<AvroException>(() => serialize(DoubleLongUnionRecord._SCHEMA, new DoubleLongUnionRecord() { Property = value }));
}

[Test]
public void TestArrayWithReservedWords()
Expand Down