-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a lot of attributes. Added some tests.
- Loading branch information
Showing
14 changed files
with
1,262 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using FluentAssertions; | ||
|
||
using IKVM.ByteCode.Buffers; | ||
using IKVM.ByteCode.Parsing; | ||
using IKVM.ByteCode.Reading; | ||
using IKVM.ByteCode.Writing; | ||
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace IKVM.ByteCode.Tests.Writing | ||
{ | ||
|
||
[TestClass] | ||
public class ClassBuilderTests | ||
{ | ||
|
||
[TestMethod] | ||
public void CanBuildSimpleClass() | ||
{ | ||
var b = new ClassBuilder(new ClassFormatVersion(53, 0), AccessFlag.ACC_PUBLIC, "TestClass", "java/lang/Object"); | ||
var f = b.AddField(AccessFlag.ACC_PUBLIC, "_field", "Z", new AttributeBuilder(b)); | ||
var m = b.AddMethod(AccessFlag.ACC_PUBLIC, "method", "()Z", new AttributeBuilder(b)); | ||
|
||
var z = new BlobBuilder(); | ||
b.Serialize(z); | ||
var a = z.ToArray(); | ||
|
||
var cls = ClassReader.Read(a); | ||
cls.AccessFlags.Should().Be(AccessFlag.ACC_PUBLIC); | ||
cls.This.Name.Value.Should().Be("TestClass"); | ||
cls.Super.Name.Value.Should().Be("java/lang/Object"); | ||
cls.Fields.Should().HaveCount(1); | ||
cls.Fields[0].AccessFlags.Should().Be(AccessFlag.ACC_PUBLIC); | ||
cls.Fields[0].Name.Value.Should().Be("_field"); | ||
cls.Fields[0].Descriptor.Value.Should().Be("Z"); | ||
cls.Methods.Should().HaveCount(1); | ||
cls.Methods[0].Name.Value.Should().Be("method"); | ||
cls.Methods[0].Descriptor.Value.Should().Be("()Z"); | ||
} | ||
|
||
} | ||
|
||
} |
228 changes: 228 additions & 0 deletions
228
src/IKVM.ByteCode.Tests/Writing/ConstantBuilderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
using System; | ||
using System.Buffers; | ||
using System.Collections.Generic; | ||
|
||
using FluentAssertions; | ||
|
||
using IKVM.ByteCode.Buffers; | ||
using IKVM.ByteCode.Parsing; | ||
using IKVM.ByteCode.Text; | ||
using IKVM.ByteCode.Writing; | ||
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace IKVM.ByteCode.Tests.Writing | ||
{ | ||
|
||
[TestClass] | ||
public class ConstantBuilderTests | ||
{ | ||
|
||
[TestMethod] | ||
public void CanEncodeUtf8ConstantSE10() | ||
{ | ||
var cp = new ConstantBuilder(new ClassFormatVersion(0, 1)); | ||
cp.AddUtf8Constant("TEST").Value.Should().Be(1); | ||
|
||
// output to array | ||
var _blob = new BlobBuilder(); | ||
cp.Serialize(_blob); | ||
var blob = _blob.ToArray(); | ||
|
||
// read constant pool | ||
var rd = new ClassFormatReader(blob); | ||
rd.TryReadU2(out var constant_pool_count).Should().BeTrue(); | ||
constant_pool_count.Should().Be(2); | ||
rd.TryReadU1(out var tag).Should().BeTrue(); | ||
tag.Should().Be((byte)ConstantTag.Utf8); | ||
rd.TryReadU2(out var length).Should().BeTrue(); | ||
length.Should().Be(4); | ||
rd.TryReadManyU1(4, out var text).Should().BeTrue(); | ||
text.Length.Should().Be(4); | ||
|
||
// contents of string should decode to TEST | ||
var _text = new byte[4]; | ||
text.CopyTo(_text); | ||
MUTF8Encoding.GetMUTF8(1).GetString(_text).Should().Be("TEST"); | ||
} | ||
|
||
[TestMethod] | ||
public void CanEncodeUtf8Constant() | ||
{ | ||
var cp = new ConstantBuilder(new ClassFormatVersion(0, 48)); | ||
cp.AddUtf8Constant("TEST").Value.Should().Be(1); | ||
|
||
// output to array | ||
var _blob = new BlobBuilder(); | ||
cp.Serialize(_blob); | ||
var blob = _blob.ToArray(); | ||
|
||
// read constant pool | ||
var rd = new ClassFormatReader(blob); | ||
rd.TryReadU2(out var constant_pool_count).Should().BeTrue(); | ||
constant_pool_count.Should().Be(2); | ||
rd.TryReadU1(out var tag).Should().BeTrue(); | ||
tag.Should().Be((byte)ConstantTag.Utf8); | ||
rd.TryReadU2(out var length).Should().BeTrue(); | ||
length.Should().Be(4); | ||
rd.TryReadManyU1(4, out var text).Should().BeTrue(); | ||
text.Length.Should().Be(4); | ||
|
||
// contents of string should decode to TEST | ||
var _text = new byte[4]; | ||
text.CopyTo(_text); | ||
MUTF8Encoding.GetMUTF8(48).GetString(_text).Should().Be("TEST"); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldNotEncodeDuplicateUtf8Values() | ||
{ | ||
var cp = new ConstantBuilder(new ClassFormatVersion(0, 48)); | ||
cp.GetOrAddUtf8Constant("TEST").Value.Should().Be(1); | ||
cp.GetOrAddUtf8Constant("TEST").Value.Should().Be(1); | ||
|
||
// output to array | ||
var _blob = new BlobBuilder(); | ||
cp.Serialize(_blob); | ||
var blob = _blob.ToArray(); | ||
|
||
// read constant pool | ||
var rd = new ClassFormatReader(blob); | ||
rd.TryReadU2(out var constant_pool_count).Should().BeTrue(); | ||
constant_pool_count.Should().Be(2); | ||
|
||
// ignore the rest | ||
} | ||
|
||
[TestMethod] | ||
public void CanEncodeTwoDistinctUtf8Values() | ||
{ | ||
var cp = new ConstantBuilder(new ClassFormatVersion(0, 48)); | ||
cp.GetOrAddUtf8Constant("TEST1").Value.Should().Be(1); | ||
cp.GetOrAddUtf8Constant("TEST2").Value.Should().Be(2); | ||
|
||
// output to array | ||
var _blob = new BlobBuilder(); | ||
cp.Serialize(_blob); | ||
var blob = _blob.ToArray(); | ||
|
||
// read constant pool | ||
var rd = new ClassFormatReader(blob); | ||
rd.TryReadU2(out var constant_pool_count).Should().BeTrue(); | ||
constant_pool_count.Should().Be(3); | ||
|
||
for (int i = 1; i < 2; i++) | ||
{ | ||
rd.TryReadU1(out var tag).Should().BeTrue(); | ||
tag.Should().Be((byte)ConstantTag.Utf8); | ||
rd.TryReadU2(out var length).Should().BeTrue(); | ||
length.Should().Be(5); | ||
rd.TryReadManyU1(5, out var text).Should().BeTrue(); | ||
text.Length.Should().Be(5); | ||
|
||
// contents of string should decode to TEST | ||
var _text = new byte[5]; | ||
text.CopyTo(_text); | ||
MUTF8Encoding.GetMUTF8(48).GetString(_text).Should().Be("TEST" + i); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void CanEncodeIntegerConstant() | ||
{ | ||
var cp = new ConstantBuilder(new ClassFormatVersion(0, 48)); | ||
cp.AddIntegerConstant(65536).Value.Should().Be(1); | ||
|
||
// output to array | ||
var _blob = new BlobBuilder(); | ||
cp.Serialize(_blob); | ||
var blob = _blob.ToArray(); | ||
|
||
// read constant pool | ||
var rd = new ClassFormatReader(blob); | ||
rd.TryReadU2(out var constant_pool_count).Should().BeTrue(); | ||
constant_pool_count.Should().Be(2); | ||
rd.TryReadU1(out var tag).Should().BeTrue(); | ||
tag.Should().Be((byte)ConstantTag.Integer); | ||
rd.TryReadU4(out var value).Should().BeTrue(); | ||
value.Should().Be(65536); | ||
} | ||
|
||
[TestMethod] | ||
public unsafe void CanEncodeFloatConstant() | ||
{ | ||
var cp = new ConstantBuilder(new ClassFormatVersion(0, 48)); | ||
cp.AddFloatConstant(float.MaxValue - 1).Value.Should().Be(1); | ||
|
||
// output to array | ||
var _blob = new BlobBuilder(); | ||
cp.Serialize(_blob); | ||
var blob = _blob.ToArray(); | ||
|
||
// read constant pool | ||
var rd = new ClassFormatReader(blob); | ||
rd.TryReadU2(out var constant_pool_count).Should().BeTrue(); | ||
constant_pool_count.Should().Be(2); | ||
rd.TryReadU1(out var tag).Should().BeTrue(); | ||
tag.Should().Be((byte)ConstantTag.Float); | ||
rd.TryReadU4(out var value).Should().BeTrue(); | ||
(*(float*)&value).Should().Be(float.MaxValue - 1); | ||
} | ||
|
||
[TestMethod] | ||
public void CanEncodeLongConstant() | ||
{ | ||
var v = (long)int.MaxValue + 256; | ||
var h = (uint)(v >> 32); | ||
var l = (uint)v; | ||
|
||
var cp = new ConstantBuilder(new ClassFormatVersion(0, 48)); | ||
cp.AddLongConstant(v).Value.Should().Be(1); | ||
|
||
// output to array | ||
var _blob = new BlobBuilder(); | ||
cp.Serialize(_blob); | ||
var blob = _blob.ToArray(); | ||
|
||
// read constant pool | ||
var rd = new ClassFormatReader(blob); | ||
rd.TryReadU2(out var constant_pool_count).Should().BeTrue(); | ||
constant_pool_count.Should().Be(3); | ||
rd.TryReadU1(out var tag).Should().BeTrue(); | ||
tag.Should().Be((byte)ConstantTag.Long); | ||
rd.TryReadU4(out var hv).Should().BeTrue(); | ||
hv.Should().Be(h); | ||
rd.TryReadU4(out var lv).Should().BeTrue(); | ||
lv.Should().Be(l); | ||
} | ||
|
||
[TestMethod] | ||
public unsafe void CanEncodeDoubleConstant() | ||
{ | ||
var v = (double)float.MaxValue + 256; | ||
var h = (uint)((*(long*)&v) >> 32); | ||
var l = (uint)(*(long*)&v); | ||
|
||
var cp = new ConstantBuilder(new ClassFormatVersion(0, 48)); | ||
cp.AddDoubleConstant(v).Value.Should().Be(1); | ||
|
||
// output to array | ||
var _blob = new BlobBuilder(); | ||
cp.Serialize(_blob); | ||
var blob = _blob.ToArray(); | ||
|
||
// read constant pool | ||
var rd = new ClassFormatReader(blob); | ||
rd.TryReadU2(out var constant_pool_count).Should().BeTrue(); | ||
constant_pool_count.Should().Be(3); | ||
rd.TryReadU1(out var tag).Should().BeTrue(); | ||
tag.Should().Be((byte)ConstantTag.Double); | ||
rd.TryReadU4(out var hv).Should().BeTrue(); | ||
hv.Should().Be(h); | ||
rd.TryReadU4(out var lv).Should().BeTrue(); | ||
lv.Should().Be(l); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.