Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
SwayStar123 committed Jun 18, 2024
1 parent e30497c commit 9a46b8e
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
14 changes: 14 additions & 0 deletions sway-lib-std/src/result.sw
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,17 @@ impl<T, E> Result<T, E> {
// - `ok(self) -> Option<T>`
// - `err(self) -> Option<E>`
}

impl<T, E> Eq for Result<T, E>
where
T: Eq,
E: Eq,
{
fn eq(self, other: Self) -> bool {
match (self, other) {
(Self::Ok(a), Self::Ok(b)) => a == b,
(Self::Err(a), Self::Err(b)) => a == b,
_ => false,
}
}
}
114 changes: 114 additions & 0 deletions test/src/in_language_tests/test_programs/result_inline_tests/src/eq.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
library;

#[test]
fn test_eq_u64_u64() {
let a: Result<u64, u64> = Ok(0);
let b: Result<u64, u64> = Ok(0);
assert_eq(a, b);

let a: Result<u64, u64> = Ok(1);
let b: Result<u64, u64> = Ok(1);
assert_eq(a, b);

let a: Result<u64, u64> = Ok(42);
let b: Result<u64, u64> = Ok(42);
assert_eq(a, b);

let a: Result<u64, u64> = Ok(u64::max());
let b: Result<u64, u64> = Ok(u64::max());
assert_eq(a, b);
}

#[test]
fn test_neq_u64_u64() {
// Ok
let a: Result<u64, u64> = Ok(0);
let b: Result<u64, u64> = Ok(1);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(0);
let b: Result<u64, u64> = Ok(42);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(0);
let b: Result<u64, u64> = Ok(u64::max());
assert_ne(a, b);

let a: Result<u64, u64> = Ok(1);
let b: Result<u64, u64> = Ok(0);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(42);
let b: Result<u64, u64> = Ok(0);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(u64::max());
let b: Result<u64, u64> = Ok(0);
assert_ne(a, b);

// Err
let a: Result<u64, u64> = Err(0);
let b: Result<u64, u64> = Err(1);
assert_ne(a, b);

let a: Result<u64, u64> = Err(0);
let b: Result<u64, u64> = Err(42);
assert_ne(a, b);

let a: Result<u64, u64> = Err(0);
let b: Result<u64, u64> = Err(u64::max());
assert_ne(a, b);

let a: Result<u64, u64> = Err(1);
let b: Result<u64, u64> = Err(0);
assert_ne(a, b);

let a: Result<u64, u64> = Err(42);
let b: Result<u64, u64> = Err(0);
assert_ne(a, b);

let a: Result<u64, u64> = Err(u64::max());
let b: Result<u64, u64> = Err(0);
assert_ne(a, b);

// Ok-Err
let a: Result<u64, u64> = Ok(0);
let b: Result<u64, u64> = Err(0);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(1);
let b: Result<u64, u64> = Err(1);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(42);
let b: Result<u64, u64> = Err(42);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(u64::max());
let b: Result<u64, u64> = Err(u64::max());
assert_ne(a, b);

let a: Result<u64, u64> = Ok(0);
let b: Result<u64, u64> = Err(1);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(0);
let b: Result<u64, u64> = Err(42);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(0);
let b: Result<u64, u64> = Err(u64::max());
assert_ne(a, b);

let a: Result<u64, u64> = Ok(1);
let b: Result<u64, u64> = Err(0);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(42);
let b: Result<u64, u64> = Err(0);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(u64::max());
let b: Result<u64, u64> = Err(0);
assert_ne(a, b);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
library;

mod eq;

#[test]
fn result_is_ok() {
use std::bytes::Bytes;
Expand Down

0 comments on commit 9a46b8e

Please sign in to comment.