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);
}