-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1065 from gircore/improve-long-handling
- Loading branch information
Showing
12 changed files
with
271 additions
and
2 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/Generation/Generator/Renderer/Internal/Field/Converter/Long.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,25 @@ | ||
namespace Generator.Renderer.Internal.Field; | ||
|
||
internal class Long : FieldConverter | ||
{ | ||
public bool Supports(GirModel.Field field) | ||
{ | ||
return field.AnyTypeOrCallback.TryPickT0(out var anyType, out _) && anyType.Is<GirModel.Long>(); | ||
} | ||
|
||
public RenderableField Convert(GirModel.Field field) | ||
{ | ||
return new RenderableField( | ||
Name: Model.Field.GetName(field), | ||
Attribute: null, | ||
NullableTypeName: GetNullableTypeName(field) | ||
); | ||
} | ||
|
||
private static string GetNullableTypeName(GirModel.Field field) | ||
{ | ||
return field.IsPointer | ||
? Model.Type.Pointer | ||
: "CLong"; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Generation/Generator/Renderer/Internal/Field/Converter/UnsignedLong.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,25 @@ | ||
namespace Generator.Renderer.Internal.Field; | ||
|
||
internal class UnsignedLong : FieldConverter | ||
{ | ||
public bool Supports(GirModel.Field field) | ||
{ | ||
return field.AnyTypeOrCallback.TryPickT0(out var anyType, out _) && anyType.Is<GirModel.UnsignedLong>(); | ||
} | ||
|
||
public RenderableField Convert(GirModel.Field field) | ||
{ | ||
return new RenderableField( | ||
Name: Model.Field.GetName(field), | ||
Attribute: null, | ||
NullableTypeName: GetNullableTypeName(field) | ||
); | ||
} | ||
|
||
private static string GetNullableTypeName(GirModel.Field field) | ||
{ | ||
return field.IsPointer | ||
? Model.Type.Pointer | ||
: "CULong"; | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
src/Generation/Generator/Renderer/Public/Field/Converter/Long.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,36 @@ | ||
namespace Generator.Renderer.Public.Field; | ||
|
||
internal class Long : FieldConverter | ||
{ | ||
public bool Supports(GirModel.Field field) | ||
{ | ||
return field.AnyTypeOrCallback.TryPickT0(out var anyType, out _) && anyType.Is<GirModel.Long>(); | ||
} | ||
|
||
public RenderableField Convert(GirModel.Field field) | ||
{ | ||
return new RenderableField( | ||
Name: Model.Field.GetName(field), | ||
NullableTypeName: GetNullableTypeName(field), | ||
SetExpression: SetExpression, | ||
GetExpression: GetExpression | ||
); | ||
} | ||
|
||
private static string GetNullableTypeName(GirModel.Field field) | ||
{ | ||
return field.IsPointer | ||
? Model.Type.Pointer | ||
: Model.Type.GetName(field.AnyTypeOrCallback.AsT0.AsT0); | ||
} | ||
|
||
private static string SetExpression(GirModel.Record record, GirModel.Field field) | ||
{ | ||
return $"Handle.Set{Model.Field.GetName(field)}(new CLong((nint)value))"; | ||
} | ||
|
||
private static string GetExpression(GirModel.Record record, GirModel.Field field) | ||
{ | ||
return $"Handle.Get{Model.Field.GetName(field)}().Value"; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Generation/Generator/Renderer/Public/Field/Converter/UnsignedLong.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,36 @@ | ||
namespace Generator.Renderer.Public.Field; | ||
|
||
internal class UnsignedLong : FieldConverter | ||
{ | ||
public bool Supports(GirModel.Field field) | ||
{ | ||
return field.AnyTypeOrCallback.TryPickT0(out var anyType, out _) && anyType.Is<GirModel.UnsignedLong>(); | ||
} | ||
|
||
public RenderableField Convert(GirModel.Field field) | ||
{ | ||
return new RenderableField( | ||
Name: Model.Field.GetName(field), | ||
NullableTypeName: GetNullableTypeName(field), | ||
SetExpression: SetExpression, | ||
GetExpression: GetExpression | ||
); | ||
} | ||
|
||
private static string GetNullableTypeName(GirModel.Field field) | ||
{ | ||
return field.IsPointer | ||
? Model.Type.Pointer | ||
: Model.Type.GetName(field.AnyTypeOrCallback.AsT0.AsT0); | ||
} | ||
|
||
private static string SetExpression(GirModel.Record record, GirModel.Field field) | ||
{ | ||
return $"Handle.Set{Model.Field.GetName(field)}(new CULong((nuint)value))"; | ||
} | ||
|
||
private static string GetExpression(GirModel.Record record, GirModel.Field field) | ||
{ | ||
return $"Handle.Get{Model.Field.GetName(field)}().Value"; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "girtest-long-record-tester.h" | ||
|
||
/** | ||
* GirTestLongRecordTester: | ||
* | ||
* Test records with long fields | ||
*/ | ||
|
||
gsize girtest_long_record_tester_get_sizeof_l(GirTestLongRecordTester* record) | ||
{ | ||
return sizeof(record->l); | ||
} | ||
|
||
gsize girtest_long_record_tester_get_sizeof_ul(GirTestLongRecordTester* record) | ||
{ | ||
return sizeof(record->ul); | ||
} |
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,17 @@ | ||
#pragma once | ||
|
||
#include <glib-object.h> | ||
|
||
G_BEGIN_DECLS | ||
|
||
typedef struct _GirTestLongRecordTester GirTestLongRecordTester; | ||
|
||
struct _GirTestLongRecordTester | ||
{ | ||
long l; | ||
unsigned long ul; | ||
}; | ||
|
||
gsize girtest_long_record_tester_get_sizeof_l(GirTestLongRecordTester* record); | ||
gsize girtest_long_record_tester_get_sizeof_ul(GirTestLongRecordTester* record); | ||
G_END_DECLS |
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
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,106 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace GirTest.Tests; | ||
|
||
[TestClass, TestCategory("BindingTest")] | ||
public class LongRecordTest : Test | ||
{ | ||
[TestMethod] | ||
public void ShouldUseCLongCULongInInternalStructData() | ||
{ | ||
var data = new GirTest.Internal.LongRecordTesterData(); | ||
data.Ul.Should().BeOfType<CULong>(); | ||
data.L.Should().BeOfType<CLong>(); | ||
} | ||
|
||
[TestMethod] | ||
public unsafe void GetSizeOfLShouldBeSizeOfCLong() | ||
{ | ||
var sizeOfCLong = sizeof(CLong); | ||
var obj = new LongRecordTester(); | ||
obj.GetSizeofL().Should().Be((nuint) sizeOfCLong); | ||
} | ||
|
||
[DataTestMethod] | ||
[DataRow(long.MaxValue)] | ||
[DataRow(long.MinValue)] | ||
public void ShouldHandleMaxMinLongValue(long value) | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || !Environment.Is64BitOperatingSystem) | ||
Assert.Inconclusive("Only supported on 64 Bit Unix operating Systems"); | ||
|
||
var obj = new LongRecordTester { L = value }; | ||
obj.L.Should().Be(value); | ||
} | ||
|
||
[DataTestMethod] | ||
[DataRow(int.MaxValue)] | ||
[DataRow(int.MinValue)] | ||
public void ShouldHandleMaxMinIntValue(int value) | ||
{ | ||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Environment.Is64BitOperatingSystem) | ||
Assert.Inconclusive("Only supported on windows or 32 bit unix operating Systems"); | ||
|
||
var obj = new LongRecordTester { L = value }; | ||
obj.L.Should().Be(value); | ||
} | ||
|
||
[DataTestMethod] | ||
[DataRow(long.MaxValue)] | ||
[DataRow(long.MinValue)] | ||
public void ShouldThrowIfValueExceedsIntegerSize(long value) | ||
{ | ||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Environment.Is64BitOperatingSystem) | ||
Assert.Inconclusive("Only supported on windows or 32 bit unix operating Systems"); | ||
|
||
var obj = new LongRecordTester(); | ||
var act = () => obj.L = value; | ||
act.Should().Throw<OverflowException>(); | ||
} | ||
|
||
[TestMethod] | ||
public unsafe void GetSizeOfULShouldBeSizeOfCULong() | ||
{ | ||
var sizeOfCULong = sizeof(CULong); | ||
var obj = new LongRecordTester(); | ||
obj.GetSizeofUl().Should().Be((nuint) sizeOfCULong); | ||
} | ||
|
||
[DataTestMethod] | ||
[DataRow(ulong.MaxValue)] | ||
[DataRow(ulong.MinValue)] | ||
public void ShouldHandleMaxMinULongValue(ulong value) | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || !Environment.Is64BitOperatingSystem) | ||
Assert.Inconclusive("Only supported on 64 Bit Unix operating Systems"); | ||
|
||
var obj = new LongRecordTester { Ul = value }; | ||
obj.Ul.Should().Be(value); | ||
} | ||
|
||
[DataTestMethod] | ||
[DataRow(uint.MaxValue)] | ||
[DataRow(uint.MinValue)] | ||
public void ShouldHandleMaxMinUIntValue(uint value) | ||
{ | ||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Environment.Is64BitOperatingSystem) | ||
Assert.Inconclusive("Only supported on windows or 32 bit unix operating Systems"); | ||
|
||
var obj = new LongRecordTester { Ul = value }; | ||
obj.Ul.Should().Be(value); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldThrowIfValueExceedsUnsignedIntegerSize() | ||
{ | ||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Environment.Is64BitOperatingSystem) | ||
Assert.Inconclusive("Only supported on windows or 32 bit unix operating Systems"); | ||
|
||
var obj = new LongRecordTester(); | ||
var act = () => obj.Ul = ulong.MaxValue; | ||
act.Should().Throw<OverflowException>(); | ||
} | ||
} |