Skip to content

Commit

Permalink
Add a test for Byte.
Browse files Browse the repository at this point in the history
Cast to sbyte first.
  • Loading branch information
wasabii committed Aug 20, 2024
1 parent db3f5da commit cf132f2
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 7 deletions.
2 changes: 1 addition & 1 deletion IKVM.deps.targets
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<ItemGroup>
<PackageReference Include="IKVM.ByteCode" Version="9.1.1" />
<PackageReference Include="IKVM.ByteCode" Version="9.1.3" />
</ItemGroup>

<Choose>
Expand Down
3 changes: 1 addition & 2 deletions src/IKVM.Java.Extensions/java/util/CollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;

namespace java.util
{
Expand Down
33 changes: 33 additions & 0 deletions src/IKVM.Java.Tests/java/lang/ByteTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ikvm.tests.java.java.lang;

import java.lang.*;

@cli.Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute.Annotation()
public class ByteTests {

@cli.Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute.Annotation()
public void canCastMinMax() throws Throwable {
if (!String.valueOf(Byte.MAX_VALUE).equals("127"))
throw new Exception();
if (!String.valueOf(Byte.MIN_VALUE).equals("-128"))
throw new Exception();
if (!String.valueOf((short)Byte.MAX_VALUE).equals("127"))
throw new Exception();
if (!String.valueOf((short)Byte.MIN_VALUE).equals("-128"))
throw new Exception();
if (!String.valueOf((int)Byte.MAX_VALUE).equals("127"))
throw new Exception();
if (!String.valueOf((int)Byte.MIN_VALUE).equals("-128"))
throw new Exception();
if (!String.valueOf((long)Byte.MAX_VALUE).equals("127"))
throw new Exception();
if (!String.valueOf((long)Byte.MIN_VALUE).equals("-128"))
throw new Exception();

if (!Integer.valueOf(Byte.MAX_VALUE).equals(Integer.valueOf(127)))
throw new Exception();
if (!Integer.valueOf(Byte.MIN_VALUE).equals(Integer.valueOf(-128)))
throw new Exception();
}

}
5 changes: 2 additions & 3 deletions src/IKVM.Runtime/StubGen/StubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ void AddMethods(ClassFileBuilder builder, RuntimeJavaType type, bool includeNonP
/// <param name="type"></param>
/// <param name="includeNonPublicMembers"></param>
/// <param name="includeSerialVersionUID"></param>
/// <exception cref="Exception"></exception>
void AddFields(ClassFileBuilder builder, RuntimeJavaType type, bool includeNonPublicMembers, bool includeSerialVersionUID)
{
var hasSerialVersionUID = false;
Expand Down Expand Up @@ -1078,7 +1077,7 @@ void EncodeElementValue(ClassFileBuilder builder, ref ElementValueEncoder encode

if (value is byte b)
{
encoder.Byte(builder.Constants.GetOrAddInteger((sbyte)b));
encoder.Byte(builder.Constants.GetOrAddInteger(unchecked((sbyte)b)));
return;
}

Expand Down Expand Up @@ -1388,7 +1387,7 @@ void EncodeElementValue(ClassFileBuilder builder, ref ElementValueEncoder encode
// typed argument of byte type holds BYTE
if (value.ArgumentType == context.Types.Byte)
{
encoder.Byte(builder.Constants.GetOrAddInteger((sbyte)(byte)value.Value));
encoder.Byte(builder.Constants.GetOrAddInteger(unchecked((sbyte)(byte)value.Value)));
return;
}

Expand Down
78 changes: 78 additions & 0 deletions src/IKVM.Tests/Java/java/lang/ByteTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.IO;
using System.Linq;

using FluentAssertions;

using IKVM.ByteCode;
using IKVM.ByteCode.Decoding;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace IKVM.Tests.Java.java.lang
{

[TestClass]
public class ByteTests
{

[TestMethod]
public void TestMinMax()
{
unchecked((sbyte)global::java.lang.Byte.MIN_VALUE).Should().Be(-128);
unchecked((sbyte)global::java.lang.Byte.MAX_VALUE).Should().Be(127);
}

[TestMethod]
public void TestMinMaxReflection()
{
((sbyte)((global::java.lang.Class)typeof(global::java.lang.Byte)).getField("MIN_VALUE").getByte(null)).Should().Be(-128);
((sbyte)((global::java.lang.Class)typeof(global::java.lang.Byte)).getField("MAX_VALUE").getByte(null)).Should().Be(127);
}

[TestMethod]
public void TestStubFile()
{
using var res = ((global::java.lang.Class)typeof(global::java.lang.Byte)).getResourceAsStream("Byte.class");
var mem = new MemoryStream();
var buf = new byte[1024];
var len = 0;
while ((len = res.read(buf)) > 0)
mem.Write(buf, 0, len);

mem.Position = 0;

using var cls = ClassFile.Read(mem);

foreach (var field in cls.Fields)
{
if (cls.Constants.Get(field.Name).Value == "MIN_VALUE")
{
field.AccessFlags.Should().HaveFlag(AccessFlag.Static);
field.AccessFlags.Should().HaveFlag(AccessFlag.Final);
cls.Constants.Get(field.Descriptor).Value.Should().Be("B");
var c = field.Attributes.First(i => cls.Constants.Get(i.Name).Value == AttributeName.ConstantValue);
var h = ((ConstantValueAttribute)c).Value;
var z = cls.Constants.Get(h);
z.Kind.Should().Be(ConstantKind.Integer);
((IntegerConstant)z).Value.Should().Be(-128);
continue;
}

if (cls.Constants.Get(field.Name).Value == "MAX_VALUE")
{
field.AccessFlags.Should().HaveFlag(AccessFlag.Static);
field.AccessFlags.Should().HaveFlag(AccessFlag.Final);
cls.Constants.Get(field.Descriptor).Value.Should().Be("B");
var c = field.Attributes.First(i => cls.Constants.Get(i.Name).Value == AttributeName.ConstantValue);
var h = ((ConstantValueAttribute)c).Value;
var z = cls.Constants.Get(h);
z.Kind.Should().Be(ConstantKind.Integer);
((IntegerConstant)z).Value.Should().Be(127);
continue;
}
}
}

}

}
2 changes: 1 addition & 1 deletion src/IKVM.Util/IKVM.Util.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageReference Include="System.IO.Compression" Version="4.3.0" />
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="IKVM.ByteCode" Version="9.1.1" />
<PackageReference Include="IKVM.ByteCode" Version="9.1.3" />
</ItemGroup>

</Project>

0 comments on commit cf132f2

Please sign in to comment.