Skip to content

Commit

Permalink
Merge branch 'master' into support-scrolling-properly
Browse files Browse the repository at this point in the history
  • Loading branch information
quajak authored Jan 2, 2024
2 parents c96eea5 + 2d1f688 commit f12ca25
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 35 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2023, CosmosOS, COSMOS Project
Copyright (c) 2024, CosmosOS, COSMOS Project
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
4 changes: 2 additions & 2 deletions Tests/Cosmos.TestRunner.Core/Engine.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,10 @@ private static string GetCosmosUserkitFolder()

private void MakeIso(string objectFile, string isoFile)
{
IsoMaker.Generate(objectFile, isoFile);
string response = IsoMaker.Generate(objectFile, isoFile);
if (!File.Exists(isoFile))
{
throw new Exception("Error building iso");
throw new Exception($"Error building iso: {response}");
}
}
}
Expand Down
28 changes: 21 additions & 7 deletions Tests/Kernels/Cosmos.Compiler.Tests.Bcl.System/System/Int32Test.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;

using System.Linq;
using Cosmos.Common.Extensions;
using Cosmos.TestRunner;

namespace Cosmos.Compiler.Tests.Bcl.System
Expand Down Expand Up @@ -37,13 +38,26 @@ public static void Execute()
// actually the Hash Code of an Int32 is the same value
Assert.IsTrue((resultAsInt == value), "Int32.GetHashCode() doesn't work");

#if false
// Now let's try ToString() again but printed in hex (this test fails for now!)
result = value.ToString("X2");
expectedResult = "0x7FFFFFFF";
// Now let's try ToString() again but printed in hex
result = value.ToString("X");
expectedResult = "7FFFFFFF";

Assert.IsTrue((result == expectedResult), "Int32.ToString(X) brings incorrect result.");

// Ensure value is not overrided
Assert.IsTrue((value != 0), "Int32.ToString(X) overrides the value of the variable.");

// Hex with padding
value = 255;
result = value.ToString("X4");
expectedResult = "00FF";
Assert.IsTrue((result == expectedResult), "Int32.ToString(X4) brings incorrect result.");

Assert.IsTrue((result == expectedResult), "Int32.ToString(X2) doesn't work");
#endif
// Test Decimal format wit padding
value = 10;
expectedResult = "0010";
result = value.ToString("D4");
Assert.IsTrue((result == expectedResult), "Int32.ToString(D4) brings incorrect result.");

// basic bit operations

Expand Down
45 changes: 45 additions & 0 deletions Tests/Kernels/Cosmos.Compiler.Tests.TypeSystem/Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using IL2CPU.API.Attribs;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Sys = Cosmos.System;

namespace Cosmos.Compiler.Tests.TypeSystem
Expand Down Expand Up @@ -305,6 +306,7 @@ protected override void Run()
Assert.IsTrue(c != null, "Anonymous types are created correctly");
Assert.IsTrue(c.i == 1 && c.n == "Test", "Anonymous types have correct values");

TestPackedStruct();
TestVTablesImpl();
TestGarbageCollectorMethods();
TestGarbageCollector();
Expand Down Expand Up @@ -339,5 +341,48 @@ private static void TestReflection()
Assert.AreEqual("Int32", Type.GetType("System.Int32, System.Private.CoreLib").Name, "GetType works on Int32 with shortened assembly qualified name");
Assert.AreEqual("Int32", Type.GetType("System.Int32").Name, "GetType works on Int32 with shortened assembly qualified name");
}

PackedStruct GetPackedStruct()
{
return new PackedStruct()
{
a = 1,
b = 2,
c = 3,
d = 4,
e = 5,
f = 6
};
}

PackedStruct Re { get; set; }
private void TestPackedStruct()
{
Re = GetPackedStruct();

Assert.AreEqual(1, Re.a, "Correctly returned first field of struct");
Assert.AreEqual("1", Re.a.ToString(), "Correctly returned first field of struct as string");
Assert.AreEqual(2, Re.b, "Correctly returned second field of struct");
Assert.AreEqual("2", Re.b.ToString(), "Correctly returned second field of struct as string");
Assert.AreEqual(3, Re.c, "Correctly returned third field of struct");
Assert.AreEqual("3", Re.c.ToString(), "Correctly returned third field of struct as string");
Assert.AreEqual(4, Re.d, "Correctly returned fourth field of struct");
Assert.AreEqual("4", Re.d.ToString(), "Correctly returned fourth field of struct as string");
Assert.AreEqual(5, Re.e, "Correctly returned fifth field of struct");
Assert.AreEqual("5", Re.e.ToString(), "Correctly returned fifth field of struct as string");
Assert.AreEqual(6, Re.f, "Correctly returned sixth field of struct");
Assert.AreEqual("6", Re.f.ToString(), "Correctly returned sixth field of struct as string");
}
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct PackedStruct
{
public ushort a;
public uint b;
public ulong c;
public ushort d;
public ushort e;
public ulong f;
}
}
61 changes: 36 additions & 25 deletions source/Cosmos.System2_Plugs/System/Int32Impl.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text;
using Cosmos.Common;
using IL2CPU.API.Attribs;

Expand All @@ -13,46 +14,56 @@ public static string ToString(ref int aThis)

public static string ToString(ref int aThis, string format)
{
if (format.Equals("X"))
if (string.IsNullOrEmpty(format))
{
string result = "";
return "0";
}

if(aThis == 0)
{
result = "0";
}
StringBuilder sb = new();

while (aThis != 0)
{
if (aThis % 16 < 10)
switch (format[0])
{
case 'X':
if (aThis == 0)
{
result = aThis % 16 + result;
sb.Append('0');
break;
}
else
{
string temp = "";

switch (aThis % 16)
int value = aThis;

while (value != 0)
{
int remainder = value % 16;
if (remainder < 10)
{
case 10: temp = "A"; break;
case 11: temp = "B"; break;
case 12: temp = "C"; break;
case 13: temp = "D"; break;
case 14: temp = "E"; break;
case 15: temp = "F"; break;
sb.Insert(0, remainder);
}
else
{
char temp = (char)('A' + (remainder - 10));
sb.Insert(0, temp);
}

result = temp + result;
value /= 16;
}
break;
case 'D':
sb.Append(aThis);
break;
default:
return aThis.ToString();
}

aThis /= 16;
}
var result = sb.ToString();

return result;
if (format.Length > 1)
{
return int.TryParse(format.AsSpan(1), out int number) ? result.PadLeft(number, '0') : aThis.ToString();
}
else
{
return aThis.ToString();
return result;
}
}

Expand Down

0 comments on commit f12ca25

Please sign in to comment.