From 19fcec84d8d7d21e796c7624e521b60d28ee21ed Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 20:45:59 +0200 Subject: Adding upstream version 16.2.11+ds. Signed-off-by: Daniel Baumann --- .../Protocols/TJsonProtocolHelperTests.cs | 172 +++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Protocols/TJsonProtocolHelperTests.cs (limited to 'src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Protocols/TJsonProtocolHelperTests.cs') diff --git a/src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Protocols/TJsonProtocolHelperTests.cs b/src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Protocols/TJsonProtocolHelperTests.cs new file mode 100644 index 000000000..6d391516e --- /dev/null +++ b/src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Protocols/TJsonProtocolHelperTests.cs @@ -0,0 +1,172 @@ +// 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 +// +// http://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. + +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Thrift.Protocol; +using Thrift.Protocol.Entities; +using Thrift.Protocol.Utilities; + +namespace Thrift.Tests.Protocols +{ + [TestClass] + public class TJSONProtocolHelperTests + { + [TestMethod] + public void GetTypeNameForTypeId_Test() + { + // input/output + var sets = new List> + { + new Tuple(TType.Bool, TJSONProtocolConstants.TypeNames.NameBool), + new Tuple(TType.Byte, TJSONProtocolConstants.TypeNames.NameByte), + new Tuple(TType.I16, TJSONProtocolConstants.TypeNames.NameI16), + new Tuple(TType.I32, TJSONProtocolConstants.TypeNames.NameI32), + new Tuple(TType.I64, TJSONProtocolConstants.TypeNames.NameI64), + new Tuple(TType.Double, TJSONProtocolConstants.TypeNames.NameDouble), + new Tuple(TType.String, TJSONProtocolConstants.TypeNames.NameString), + new Tuple(TType.Struct, TJSONProtocolConstants.TypeNames.NameStruct), + new Tuple(TType.Map, TJSONProtocolConstants.TypeNames.NameMap), + new Tuple(TType.Set, TJSONProtocolConstants.TypeNames.NameSet), + new Tuple(TType.List, TJSONProtocolConstants.TypeNames.NameList), + }; + + foreach (var t in sets) + { + Assert.IsTrue(TJSONProtocolHelper.GetTypeNameForTypeId(t.Item1) == t.Item2, $"Wrong mapping of TypeName {t.Item2} to TType: {t.Item1}"); + } + } + + [TestMethod] + [ExpectedException(typeof(TProtocolException))] + public void GetTypeNameForTypeId_TStop_Test() + { + TJSONProtocolHelper.GetTypeNameForTypeId(TType.Stop); + } + + [TestMethod] + [ExpectedException(typeof(TProtocolException))] + public void GetTypeNameForTypeId_NonExistingTType_Test() + { + TJSONProtocolHelper.GetTypeNameForTypeId((TType)100); + } + + [TestMethod] + public void GetTypeIdForTypeName_Test() + { + // input/output + var sets = new List> + { + new Tuple(TType.Bool, TJSONProtocolConstants.TypeNames.NameBool), + new Tuple(TType.Byte, TJSONProtocolConstants.TypeNames.NameByte), + new Tuple(TType.I16, TJSONProtocolConstants.TypeNames.NameI16), + new Tuple(TType.I32, TJSONProtocolConstants.TypeNames.NameI32), + new Tuple(TType.I64, TJSONProtocolConstants.TypeNames.NameI64), + new Tuple(TType.Double, TJSONProtocolConstants.TypeNames.NameDouble), + new Tuple(TType.String, TJSONProtocolConstants.TypeNames.NameString), + new Tuple(TType.Struct, TJSONProtocolConstants.TypeNames.NameStruct), + new Tuple(TType.Map, TJSONProtocolConstants.TypeNames.NameMap), + new Tuple(TType.Set, TJSONProtocolConstants.TypeNames.NameSet), + new Tuple(TType.List, TJSONProtocolConstants.TypeNames.NameList), + }; + + foreach (var t in sets) + { + Assert.IsTrue(TJSONProtocolHelper.GetTypeIdForTypeName(t.Item2) == t.Item1, $"Wrong mapping of TypeName {t.Item2} to TType: {t.Item1}"); + } + } + + [TestMethod] + [ExpectedException(typeof(TProtocolException))] + public void GetTypeIdForTypeName_TStopTypeName_Test() + { + TJSONProtocolHelper.GetTypeIdForTypeName(new []{(byte)TType.Stop, (byte)TType.Stop}); + } + + [TestMethod] + [ExpectedException(typeof(TProtocolException))] + public void GetTypeIdForTypeName_NonExistingTypeName_Test() + { + TJSONProtocolHelper.GetTypeIdForTypeName(new byte[]{100}); + } + + [TestMethod] + [ExpectedException(typeof(TProtocolException))] + public void GetTypeIdForTypeName_EmptyName_Test() + { + TJSONProtocolHelper.GetTypeIdForTypeName(new byte[] {}); + } + + [TestMethod] + public void IsJsonNumeric_Test() + { + // input/output + var correctJsonNumeric = "+-.0123456789Ee"; + var incorrectJsonNumeric = "AaBcDd/*\\"; + + var sets = correctJsonNumeric.Select(ch => new Tuple((byte) ch, true)).ToList(); + sets.AddRange(incorrectJsonNumeric.Select(ch => new Tuple((byte) ch, false))); + + foreach (var t in sets) + { + Assert.IsTrue(TJSONProtocolHelper.IsJsonNumeric(t.Item1) == t.Item2, $"Wrong mapping of Char {t.Item1} to bool: {t.Item2}"); + } + } + + [TestMethod] + public void ToHexVal_Test() + { + // input/output + var chars = "0123456789abcdef"; + var expectedHexValues = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; + + var sets = chars.Select((ch, i) => new Tuple(ch, expectedHexValues[i])).ToList(); + + foreach (var t in sets) + { + var actualResult = TJSONProtocolHelper.ToHexVal((byte)t.Item1); + Assert.IsTrue(actualResult == t.Item2, $"Wrong mapping of char byte {t.Item1} to it expected hex value: {t.Item2}. Actual hex value: {actualResult}"); + } + } + + [TestMethod] + [ExpectedException(typeof(TProtocolException))] + public void ToHexVal_WrongInputChar_Test() + { + TJSONProtocolHelper.ToHexVal((byte)'s'); + } + + [TestMethod] + public void ToHexChar_Test() + { + // input/output + var hexValues = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; + var expectedChars = "0123456789abcdef"; + + + var sets = hexValues.Select((hv, i) => new Tuple(hv, expectedChars[i])).ToList(); + + foreach (var t in sets) + { + var actualResult = TJSONProtocolHelper.ToHexChar(t.Item1); + Assert.IsTrue(actualResult == t.Item2, $"Wrong mapping of hex value {t.Item1} to it expected char: {t.Item2}. Actual hex value: {actualResult}"); + } + } + } +} \ No newline at end of file -- cgit v1.2.3