Skip to content

Commit a902fba

Browse files
[CBOR] Make Half Read/Write methods public and add unit tests (#38946)
* Make Cbor Half methods public and add unit tests * address feedback
1 parent a814b3e commit a902fba

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

src/libraries/System.Formats.Cbor/ref/System.Formats.Cbor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public void ReadEndArray() { }
4444
public void ReadEndIndefiniteLengthByteString() { }
4545
public void ReadEndIndefiniteLengthTextString() { }
4646
public void ReadEndMap() { }
47+
public System.Half ReadHalf() { throw null; }
4748
public int ReadInt32() { throw null; }
4849
public long ReadInt64() { throw null; }
4950
public void ReadNull() { }
@@ -144,6 +145,7 @@ public void WriteEndArray() { }
144145
public void WriteEndIndefiniteLengthByteString() { }
145146
public void WriteEndIndefiniteLengthTextString() { }
146147
public void WriteEndMap() { }
148+
public void WriteHalf(System.Half value) { }
147149
public void WriteInt32(int value) { }
148150
public void WriteInt64(long value) { }
149151
public void WriteNull() { }

src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.Simple.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public partial class CborReader
2121
/// there was an unexpected end of CBOR encoding data. -or-
2222
/// the next value uses a CBOR encoding that is not valid under the current conformance mode.
2323
/// </exception>
24-
internal Half ReadHalf()
24+
public Half ReadHalf()
2525
{
2626
CborInitialByte header = PeekInitialByte(expectedType: CborMajorType.Simple);
2727
ReadOnlySpan<byte> buffer = GetRemainingBytes();

src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Writer/CborWriter.Simple.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public partial class CborWriter
1919
/// The major type of the encoded value is not permitted in the parent data item. -or-
2020
/// The written data is not accepted under the current conformance mode
2121
/// </exception>
22-
internal void WriteHalf(Half value)
22+
public void WriteHalf(Half value)
2323
{
2424
EnsureWriteCapacity(1 + HalfHelpers.SizeOfHalf);
2525
WriteInitialByte(new CborInitialByte(CborMajorType.Simple, CborAdditionalInfo.Additional16BitData));

src/libraries/System.Formats.Cbor/tests/Reader/CborReaderTests.Simple.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@ public partial class CborReaderTests
1111
// Data points taken from https://tools.ietf.org/html/rfc7049#appendix-A
1212
// Additional pairs generated using http://cbor.me/
1313

14+
[Theory]
15+
[InlineData(0.0, "f90000")]
16+
[InlineData(-0.0, "f98000")]
17+
[InlineData(1.0, "f93c00")]
18+
[InlineData(1.5, "f93e00")]
19+
[InlineData(65504.0, "f97bff")]
20+
[InlineData(5.960464477539063e-8, "f90001")]
21+
[InlineData(0.00006103515625, "f90400")]
22+
[InlineData(-4.0, "f9c400")]
23+
[InlineData(double.PositiveInfinity, "f97c00")]
24+
[InlineData(double.NaN, "f97e00")]
25+
[InlineData(double.NegativeInfinity, "f9fc00")]
26+
public static void ReadHalf_SingleValue_HappyPath(float expectedResult, string hexEncoding)
27+
{
28+
byte[] encoding = hexEncoding.HexToByteArray();
29+
var reader = new CborReader(encoding);
30+
Assert.Equal(CborReaderState.HalfPrecisionFloat, reader.PeekState());
31+
Half actualResult = reader.ReadHalf();
32+
AssertHelpers.Equal((Half)expectedResult, actualResult);
33+
Assert.Equal(CborReaderState.Finished, reader.PeekState());
34+
}
35+
1436
[Theory]
1537
[InlineData(100000.0, "fa47c35000")]
1638
[InlineData(3.4028234663852886e+38, "fa7f7fffff")]
@@ -225,6 +247,25 @@ public static void ReadNull_InvalidTypes_ShouldThrowInvalidOperationException(st
225247
Assert.Equal(encoding.Length, reader.BytesRemaining);
226248
}
227249

250+
[Theory]
251+
[InlineData("01")] // integer
252+
[InlineData("40")] // empty text string
253+
[InlineData("60")] // empty byte string
254+
[InlineData("80")] // []
255+
[InlineData("a0")] // {}
256+
[InlineData("f6")] // null
257+
[InlineData("f4")] // false
258+
[InlineData("c202")] // tagged value
259+
[InlineData("fa47c35000")] // single-precision float encoding
260+
[InlineData("fb7ff0000000000000")] // double-precision float encoding
261+
public static void ReadHalf_InvalidTypes_ShouldThrowInvalidOperationException(string hexEncoding)
262+
{
263+
byte[] encoding = hexEncoding.HexToByteArray();
264+
var reader = new CborReader(encoding);
265+
Assert.Throws<InvalidOperationException>(() => reader.ReadHalf());
266+
Assert.Equal(encoding.Length, reader.BytesRemaining);
267+
}
268+
228269
[Theory]
229270
[InlineData("01")] // integer
230271
[InlineData("40")] // empty text string
@@ -259,5 +300,21 @@ public static void ReadDouble_InvalidTypes_ShouldThrowInvalidOperationException(
259300
Assert.Throws<InvalidOperationException>(() => reader.ReadDouble());
260301
Assert.Equal(encoding.Length, reader.BytesRemaining);
261302
}
303+
304+
public static class AssertHelpers
305+
{
306+
// temporary workaround for xunit's lack of support for Half equality assertions
307+
public static void Equal(Half expected, Half actual)
308+
{
309+
if (Half.IsNaN(expected))
310+
{
311+
Assert.True(Half.IsNaN(actual), $"Expected: {expected}\nActual: {actual}");
312+
}
313+
else
314+
{
315+
Assert.Equal(expected, actual);
316+
}
317+
}
318+
}
262319
}
263320
}

src/libraries/System.Formats.Cbor/tests/Writer/CborWriterTests.Simple.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@ public partial class CborWriterTests
1111
// Data points taken from https://tools.ietf.org/html/rfc7049#appendix-A
1212
// Additional pairs generated using http://cbor.me/
1313

14+
[Theory]
15+
[InlineData(0.0, "f90000")]
16+
[InlineData(-0.0, "f98000")]
17+
[InlineData(1.0, "f93c00")]
18+
[InlineData(1.5, "f93e00")]
19+
[InlineData(65504.0, "f97bff")]
20+
[InlineData(5.960464477539063e-8, "f90001")]
21+
[InlineData(0.00006103515625, "f90400")]
22+
[InlineData(-4.0, "f9c400")]
23+
[InlineData(float.PositiveInfinity, "f97c00")]
24+
[InlineData(float.NaN, "f9fe00")]
25+
[InlineData(float.NegativeInfinity, "f9fc00")]
26+
public static void WriteHalf_SingleValue_HappyPath(float input, string hexExpectedEncoding)
27+
{
28+
byte[] expectedEncoding = hexExpectedEncoding.HexToByteArray();
29+
var writer = new CborWriter();
30+
writer.WriteHalf((Half)input);
31+
AssertHelper.HexEqual(expectedEncoding, writer.Encode());
32+
}
33+
1434
[Theory]
1535
[InlineData(100000.0, "fa47c35000")]
1636
[InlineData(3.4028234663852886e+38, "fa7f7fffff")]

0 commit comments

Comments
 (0)