1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 12:32:20 +02:00

Added VarArgs

This commit is contained in:
Brian Fiete 2021-01-22 04:58:08 -08:00
parent fd1d9644f7
commit 9ccdf7282e
8 changed files with 228 additions and 26 deletions

View file

@ -6,7 +6,6 @@ Unlocked = ["corlib"]
StartupProject = "Tests"
[Configs.Debug.Win64]
BfOptimizationLevel = "O0"
IntermediateType = "ObjectAndIRCode"
[Configs.Test.Win64]

View file

@ -1,8 +1,10 @@
#pragma warning disable 168
using System;
namespace Tests
{
class VarArgs
class VarArgsTests
{
#if BF_PLATFORM_WINDOWS
[CLink, Import("msvcrt.dll")]
@ -11,14 +13,75 @@ namespace Tests
#endif
public static extern int32 sprintf(char8* dest, char8* fmt, ...);
#if BF_PLATFORM_WINDOWS
[CLink, Import("msvcrt.dll")]
#else
[CLink]
#endif
public static extern int32 vsprintf(char8* dest, char8* fmt, VarArgs varArgs);
public static (int, int, int) MethodA(...)
{
VarArgs vaArgs = .();
vaArgs.Start!();
int val = vaArgs.Get!<int>();
int val2 = vaArgs.Get!<int>();
int val3 = (int)vaArgs.Get!<double>();
vaArgs.End!();
return (val, val2, val3);
}
public static (int, int, int) MethodB(int a, ...)
{
VarArgs vaArgs = .();
vaArgs.Start!();
int val = vaArgs.Get!<int>();
int val2 = vaArgs.Get!<int>();
int val3 = (int)vaArgs.Get!<double>();
vaArgs.End!();
return (val, val2, val3);
}
public static (int, int, int) MethodC(int a, ...)
{
uint8* data = scope uint8[a]*;
VarArgs vaArgs = .();
vaArgs.Start!();
int val = vaArgs.Get!<int>();
int val2 = vaArgs.Get!<int>();
int val3 = (int)vaArgs.Get!<double>();
vaArgs.End!();
return (val, val2, val3);
}
public static int32 SPrintF(char8* dest, char8* fmt, ...)
{
VarArgs vaArgs = .();
vaArgs.Start!();
int32 result = vsprintf(dest, fmt, vaArgs);
vaArgs.End!();
return result;
}
[Test]
public static void TestBasics()
{
char8[256] cStr;
sprintf(&cStr, "Test %d %0.1f %0.1f", 123, 1.2f, 2.3f);
String str = scope .(&cStr);
Test.Assert(str == "Test 123 1.2 2.3");
SPrintF(&cStr, "Test %d %0.1f %0.1f", 223, 2.2f, 3.3f);
str.Set(StringView(&cStr));
Test.Assert(str == "Test 223 2.2 3.3");
Test.Assert(MethodA( 12, 23, 123.0f) == (12, 23, 123));
Test.Assert(MethodB( 9, 22, 33, 223.0f) == (22, 33, 223));
Test.Assert(MethodC(11, 32, 43, 323.0f) == (32, 43, 323));
}
}
}