next iteration

This commit is contained in:
Miha Kralj
2025-11-26 20:17:01 -08:00
parent 33ffd3a37a
commit 1c8f514756
27 changed files with 1391 additions and 10 deletions
+51
View File
@@ -0,0 +1,51 @@
using System;
using Xunit;
using QuanTAlib;
namespace QuanTAlib.Tests
{
public class TValueTests
{
[Fact]
public void Constructor_SetsPropertiesCorrectly()
{
long time = DateTime.UtcNow.Ticks;
double value = 123.45;
var tValue = new TValue(time, value);
Assert.Equal(time, tValue.Time);
Assert.Equal(value, tValue.Value);
}
[Fact]
public void AsDateTime_ReturnsCorrectDateTime()
{
DateTime dt = new DateTime(2023, 1, 1, 12, 0, 0, DateTimeKind.Utc);
long ticks = dt.Ticks;
var tValue = new TValue(ticks, 100.0);
Assert.Equal(dt, tValue.AsDateTime);
}
[Fact]
public void ToString_FormatsCorrectly()
{
DateTime dt = new DateTime(2023, 1, 1, 12, 0, 0, DateTimeKind.Utc);
var tValue = new TValue(dt.Ticks, 123.456);
string result = tValue.ToString();
Assert.Contains(dt.ToString("yyyy-MM-dd HH:mm:ss"), result);
Assert.Contains("123.46", result); // Default formatting usually 2 decimals or similar
}
[Fact]
public void ImplicitConversion_ToDouble()
{
var tValue = new TValue(DateTime.UtcNow.Ticks, 42.0);
double val = tValue;
Assert.Equal(42.0, val);
}
}
}
+57
View File
@@ -0,0 +1,57 @@
using System.Runtime.CompilerServices;
namespace QuanTAlib;
/// <summary>
/// A lightweight struct representing a time-value pair.
/// Pure data type: 16 bytes (long + double).
/// </summary>
[SkipLocalsInit]
public readonly struct TValue : IEquatable<TValue>
{
/// <summary>
/// Time in ticks (UTC).
/// </summary>
public readonly long Time;
/// <summary>
/// The value.
/// </summary>
public readonly double Value;
/// <summary>
/// Convenience property to get DateTime from Ticks.
/// </summary>
public DateTime AsDateTime => new(Time, DateTimeKind.Utc);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TValue(long time, double value)
{
Time = time;
Value = value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TValue(DateTime time, double value)
{
Time = time.Ticks;
Value = value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator double(TValue tv) => tv.Value;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator DateTime(TValue tv) => new(tv.Time, DateTimeKind.Utc);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override string ToString() => $"[{AsDateTime:yyyy-MM-dd HH:mm:ss}, {Value:F2}]";
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(TValue other) => Time == other.Time && Value == other.Value;
public override bool Equals(object? obj) => obj is TValue other && Equals(other);
public override int GetHashCode() => HashCode.Combine(Time, Value);
public static bool operator ==(TValue left, TValue right) => left.Equals(right);
public static bool operator !=(TValue left, TValue right) => !left.Equals(right);
}