Pair and Triple Compare w/ Unit Tests (#2359)

* Pair and Triple Compare w/ Unit Tests
* scope creep myself by adding date-time eq op
* make Pair and Triple printable
* Update releasenotes. Restrict equals on tuples to when underlying type supports `==`. Remove unnecessary Time.eq.

---------

Co-authored-by: Christoffer Lerno <christoffer@aegik.com>
This commit is contained in:
Zack Puhl
2025-08-02 17:11:27 -04:00
committed by GitHub
parent e707190539
commit 2a47cc2ca9
7 changed files with 160 additions and 3 deletions

View File

@@ -0,0 +1,71 @@
module tuple_test;
struct TestStruct
{
String a;
int b;
}
fn bool TestStruct.eq(self, TestStruct other) @operator(==)
=> self.a == other.a && self.b == other.b;
module tuple_test @test;
import std::collections::pair;
import std::collections::triple;
fn void pair_make_and_unpack()
{
Pair { ulong, String } x = { 0x8034534, "some string" };
assert(x.first == 0x8034534);
assert(x.second == "some string");
ulong a;
String b;
x.unpack(&a, &b);
assert(a == 0x8034534);
assert(b == "some string");
}
fn void pair_compare()
{
Pair { TestStruct, String } x = { {"left", -123}, "right" };
Pair { TestStruct, String } y = { {"left", -123}, "right" };
Pair { TestStruct, String } z = { {"left", 4096}, "right" };
assert(x == y);
assert(x != z);
}
fn void triple_make_and_unpack()
{
int myval = 7;
Triple { ulong, String, int* } x = { 0xA_DEAD_C0DE, "yet another string", &myval };
assert(x.first == 0xA_DEAD_C0DE);
assert(x.second == "yet another string");
assert(x.third == &myval);
ulong a;
String b;
int* c;
x.unpack(&a, &b, &c);
assert(a == 0xA_DEAD_C0DE);
assert(b == "yet another string");
assert(c == &myval);
}
fn void triple_compare()
{
Triple { TestStruct, String, String } x = { {"in", 42}, "left", "right" };
Triple { TestStruct, String, String } y = { {"in", 42}, "left", "right" };
Triple { TestStruct, String, String } z = { {"in", 42}, "up", "down" };
assert(x == y);
assert(x != z);
}

View File

@@ -49,6 +49,44 @@ fn void test_parse_and_add()
test::eq(x.day, 4);
}
fn void datetime_eq()
{
DateTime d = datetime::from_date(1973, APRIL, 27, 12, 23, 55, 34);
DateTime d2 = d;
assert(d == d2);
d2 = d2.add_days(6);
assert(d != d2);
d2 = d2.add_days(-5);
assert(d != d2);
d2 = d2.add_days(-1);
assert(d == d2);
}
fn void tzdatetime_eq()
{
int offset1_hours = 7;
int offset2_hours = -1;
TzDateTime d1 = datetime::from_date_tz(2022, OCTOBER, 15, 9, 07, 45, gmt_offset: offset1_hours * 3600);
TzDateTime d2 = datetime::from_date_tz(2022, OCTOBER, 15, 1, 07, 45, gmt_offset: offset2_hours * 3600);
assert(d1 == d2);
d2 = d2.add_years(30);
assert(d1 != d2);
offset1_hours = 12;
offset2_hours = -10;
d1 = datetime::from_date_tz(2022, OCTOBER, 15, 20, 15, 31, gmt_offset: offset1_hours * 3600);
d2 = datetime::from_date_tz(2022, OCTOBER, 14, 22, 15, 31, gmt_offset: offset2_hours * 3600);
assert(d1 == d2);
}
fn void test_timezone()
{
int offset_hours = 7;

View File

@@ -13,6 +13,20 @@ fn void time_diff()
test::eq(t, t2);
}
fn void time_eq()
{
Time t = time::now();
Time t2 = t;
assert(t == t2);
t2 += time::US;
assert(t != t2);
t2 -= time::US;
assert(t == t2);
}
fn void clock_diff()
{
Clock c = clock::now();