From b8efdfb27fbae5abe7e116bb135a90454b29acea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sat, 5 Jul 2025 07:21:35 +0200 Subject: [PATCH 001/243] created Rust initial folder --- Cargo.lock | 7 +++++++ Cargo.toml | 6 ++++++ src/main.rs | 3 +++ 3 files changed, 16 insertions(+) create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..047cff1 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "lorem" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..19456f9 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "lorem" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e0b5e27 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello world!"); +} From 45efbdb1ddd081671e19d5c7de8a307f02907d6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sat, 5 Jul 2025 07:24:12 +0200 Subject: [PATCH 002/243] add module and struct --- src/main.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main.rs b/src/main.rs index e0b5e27..4d16761 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,10 @@ +mod rust_big_int { + struct Internals { + positive: bool, + numbers: Vec, + } +} + fn main() { println!("Hello world!"); } From e909ee7a762d811119065d660b1f229bcf1d525c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sat, 5 Jul 2025 08:03:28 +0200 Subject: [PATCH 003/243] remove main fn --- src/main.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4d16761..691c467 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,14 @@ mod rust_big_int { } } -fn main() { - println!("Hello world!"); +#[cfg(test)] +mod tests { + use crate::BigInt; + + #[test] + fn default() { + let def = BigInt::default(); + assert_eq!(def.positive, true); + assert_eq!(def.numbers, [0].to_vec()); + } } From a7a239a35f57309fbaf995fa83f4a3df6cda371a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sat, 5 Jul 2025 08:04:28 +0200 Subject: [PATCH 004/243] =?UTF-8?q?rename=20rust=5Fbig=5Fint=20->=C2=A0Big?= =?UTF-8?q?Int?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 691c467..48e1a34 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,14 @@ -mod rust_big_int { - struct Internals { - positive: bool, - numbers: Vec, +struct BigInt { + positive: bool, + numbers: Vec, +} + +impl Default for BigInt { + fn default() -> Self { + BigInt { + positive: true, + numbers: [0].to_vec(), + } } } From 3cdafda83b18ba238f83ebfd1b875643c973e72d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sat, 5 Jul 2025 08:05:16 +0200 Subject: [PATCH 005/243] minor dispute with rust --- src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 48e1a34..41af60d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,6 @@ impl Default for BigInt { #[cfg(test)] mod tests { - use crate::BigInt; #[test] fn default() { From ba2e8533a0a50fa978ba9be938bc58db9be92b58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sat, 5 Jul 2025 08:08:13 +0200 Subject: [PATCH 006/243] add main at the request of Rust --- src/main.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main.rs b/src/main.rs index 41af60d..cef7a18 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,3 +22,5 @@ mod tests { assert_eq!(def.numbers, [0].to_vec()); } } + +fn main() {} From 0260051a59771b5d0fdc268a47d9924b543d2135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sat, 5 Jul 2025 08:09:12 +0200 Subject: [PATCH 007/243] return of use crate --- src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.rs b/src/main.rs index cef7a18..95ac642 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ impl Default for BigInt { #[cfg(test)] mod tests { + use crate::BigInt; #[test] fn default() { From 0008345b97aa7896f50875a0619b6fffd91bdc11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 7 Jul 2025 23:03:10 +0200 Subject: [PATCH 008/243] minor changes for in implementation --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 95ac642..7c1348d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,13 @@ struct BigInt { positive: bool, - numbers: Vec, + numbers: Vec, } impl Default for BigInt { fn default() -> Self { BigInt { positive: true, - numbers: [0].to_vec(), + numbers: vec![0], } } } From 3830f91108505e74bac71e4aa48af56e77b2e72a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 8 Jul 2025 00:07:45 +0200 Subject: [PATCH 009/243] adds BigInt::new() --- src/main.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main.rs b/src/main.rs index 7c1348d..ad0ba52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,12 @@ impl Default for BigInt { } } +impl BigInt { + fn new() -> BigInt { + BigInt::default() + } +} + #[cfg(test)] mod tests { use crate::BigInt; From efbeefc618a076e7d0a89e8770d41f616bcf4513 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 9 Jul 2025 12:17:30 +0200 Subject: [PATCH 010/243] adds #[derive(Clone)] --- src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.rs b/src/main.rs index ad0ba52..c8c8368 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +#[derive(Clone)] struct BigInt { positive: bool, numbers: Vec, From d198aaa94303059cb3c1755aa1ee95f38f47ecf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 9 Jul 2025 17:21:06 +0200 Subject: [PATCH 011/243] Adds tests for BigInt::new() --- src/main.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main.rs b/src/main.rs index c8c8368..45b4f78 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,13 @@ mod tests { assert_eq!(def.positive, true); assert_eq!(def.numbers, [0].to_vec()); } + + #[test] + fn new() { + let new = BigInt::new(); + assert_eq!(new.positive, true); + assert_eq!(new.numbers, [0].to_vec()); + } } fn main() {} From cc6a55ea7c8bc5a0a925b85fa90e709d75c02481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 9 Jul 2025 17:21:46 +0200 Subject: [PATCH 012/243] Adds tests for BigInt::from() --- src/main.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main.rs b/src/main.rs index 45b4f78..c357d3d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,6 +36,22 @@ mod tests { assert_eq!(new.positive, true); assert_eq!(new.numbers, [0].to_vec()); } + + #[test] + fn from() { + let x = BigInt::from(20); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [2, 0].to_vec()); + let x = BigInt::from(-20); + assert_eq!(x.positive, false); + assert_eq!(x.numbers, [2, 0].to_vec()); + let x = BigInt::from(-320020000981234567890); + assert_eq!(x.positive, false); + assert_eq!( + x.numbers, + [3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0].to_vec() + ); + } } fn main() {} From 5f79ea130813306fb77ea62813f3444ec457298f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 9 Jul 2025 17:22:15 +0200 Subject: [PATCH 013/243] Adds tests for BigInt::from_str --- src/main.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/main.rs b/src/main.rs index c357d3d..cf5bc07 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,6 +52,53 @@ mod tests { [3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0].to_vec() ); } + + #[test] + fn from_string_numbers() { + let x = BigInt::from_str("20"); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [2, 0].to_vec()); + let x = BigInt::from_str("666"); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [6, 6, 6].to_vec()); + let x = BigInt::from_str("-20"); + assert_eq!(x.positive, false); + assert_eq!(x.numbers, [2, 0].to_vec()); + let x = BigInt::from_str("-320020000981234567890"); + assert_eq!(x.positive, false); + assert_eq!( + x.numbers, + [3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0].to_vec() + ); + let x = BigInt::from_str("-0"); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [0].to_vec()); + } + + #[test] + fn from_string_words_from_str() { + let x = BigInt::from_str("twenty"); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [2, 0].to_vec()); + let x = BigInt::from_str("minus twenty four"); + assert_eq!(x.positive, false); + assert_eq!(x.numbers, [2, 4].to_vec()); + let x = BigInt::from_str("two milion five hundred fifty thousand twenty one"); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [2, 5, 5, 0, 0, 2, 1].to_vec()); + let x = BigInt::from_str("minus two milion one"); + assert_eq!(x.positive, false); + assert_eq!(x.numbers, [2, 0, 0, 0, 0, 0, 1].to_vec()); + let x = BigInt::from_str("zero"); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [0].to_vec()); + let x = BigInt::from_str("onse"); + assert!(x.is_err()); + let x = BigInt::from_str("twenty thousand thousand"); + assert!(x.is_err()); + let x = BigInt::from_str("twenty thousand hundred"); + assert!(x.is_err()); + } } fn main() {} From fefcc66b1e083d978b160b5a4320695a6944ab78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 9 Jul 2025 17:23:01 +0200 Subject: [PATCH 014/243] Adds tests for BigInt::try_from --- src/main.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main.rs b/src/main.rs index cf5bc07..f518b0e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -99,6 +99,28 @@ mod tests { let x = BigInt::from_str("twenty thousand hundred"); assert!(x.is_err()); } + + #[test] + fn from_string_words_try_from() { + let x = BigInt::try_from("twenty"); + assert_eq!(x.unwrap().positive, true); + assert_eq!(x.unwrap().numbers, [2, 0].to_vec()); + let x = BigInt::try_from("minus twenty four"); + assert_eq!(x.unwrap().positive, false); + assert_eq!(x.unwrap().numbers, [2, 4].to_vec()); + let x = BigInt::try_from("two milion five hundred fifty thousand twenty one"); + assert_eq!(x.unwrap().positive, true); + assert_eq!(x.unwrap().numbers, [2, 5, 5, 0, 0, 2, 1].to_vec()); + let x = BigInt::try_from("minus two milion one"); + assert_eq!(x.unwrap().positive, false); + assert_eq!(x.unwrap().numbers, [2, 0, 0, 0, 0, 0, 1].to_vec()); + let x = BigInt::try_from("onse"); + assert!(x.is_err()); + let x = BigInt::try_from("twenty thousand thousand"); + assert!(x.is_err()); + let x = BigInt::try_from("twenty thousand hundred"); + assert!(x.is_err()); + } } fn main() {} From fa8a98f4fa77cbcc9c64f3f0d4b24a864ddf777b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 9 Jul 2025 17:23:24 +0200 Subject: [PATCH 015/243] Adds tests for Display trait --- src/main.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main.rs b/src/main.rs index f518b0e..6fbab25 100644 --- a/src/main.rs +++ b/src/main.rs @@ -121,6 +121,25 @@ mod tests { let x = BigInt::try_from("twenty thousand hundred"); assert!(x.is_err()); } + + #[test] + fn display() { + let x = BigInt::from(1003); + assert_eq!(format!("Number is: {x}"), "Number is: 1003"); + let x = BigInt::from(0); + assert_eq!(format!("Number is: {x}"), "Number is: 0"); + let x = BigInt::from(-100); + assert_eq!(format!("Number is: {x}"), "Number is: -100"); + let x = BigInt::from(-0); + assert_eq!(format!("Number is: {x}"), "Number is: -0"); + let x = BigInt::from(-1); + assert_eq!(format!("Number is: {x}"), "Number is: -1"); + let x = BigInt::from(320020000981234567890); + assert_eq!( + format!("Number is: {x}"), + "Number is: 320020000981234567890" + ); + } } fn main() {} From 4e2672cba01ed2307d1bc351bce91083004975be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 9 Jul 2025 17:42:02 +0200 Subject: [PATCH 016/243] Adds tests for debug trait --- src/main.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main.rs b/src/main.rs index 6fbab25..8025e0a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -140,6 +140,25 @@ mod tests { "Number is: 320020000981234567890" ); } + + #[test] + fn debug() { + let x = BigInt::from(1003); + assert_eq!(format!("Number is: {x:?}"), "Number is: +1003"); + let x = BigInt::from(0); + assert_eq!(format!("Number is: {x:?}"), "Number is: +0"); + let x = BigInt::from(-100); + assert_eq!(format!("Number is: {x:?}"), "Number is: -100"); + let x = BigInt::from(-0); + assert_eq!(format!("Number is: {x:?}"), "Number is: -0"); + let x = BigInt::from(-1); + assert_eq!(format!("Number is: {x:?}"), "Number is: -1"); + let x = BigInt::from(320020000981234567890); + assert_eq!( + format!("Number is: {x:?}"), + "Number is: +320020000981234567890" + ); + } } fn main() {} From 9f359f4f1b9242086d054ad29a5b1baea9064425 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 9 Jul 2025 17:42:15 +0200 Subject: [PATCH 017/243] Adds tests for BigInt::to_words() --- src/main.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main.rs b/src/main.rs index 8025e0a..07a9738 100644 --- a/src/main.rs +++ b/src/main.rs @@ -159,6 +159,21 @@ mod tests { "Number is: +320020000981234567890" ); } + + #[test] + fn to_words() { + let x = BigInt::from(20); + assert_eq!(x.to_words(), "two zero"); + let x = BigInt::from(-24); + assert_eq!(x.to_words(), "minus two four"); + let x = BigInt::from(2550021); + assert_eq!(x.to_words(), "two five five zero zero two one"); + let x = BigInt::from(-2000000000001); + assert_eq!( + x.to_words(), + "minus two zero zero zero zero zero zero zero zero zero zero zero one" + ); + } } fn main() {} From 5e910d4cb2701bd1151e632a1f95a37de4ebcf86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 9 Jul 2025 20:47:36 +0200 Subject: [PATCH 018/243] Adds tests for equal trait --- src/main.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main.rs b/src/main.rs index 07a9738..89ad29b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -174,6 +174,28 @@ mod tests { "minus two zero zero zero zero zero zero zero zero zero zero zero one" ); } + + #[test] + fn equal() { + let x = BigInt::from(10); + let y = BigInt::from(10); + assert!(x == y); + let x = BigInt::from(101010); + let y = BigInt::from(101010); + assert!(x == y); + let x = BigInt::from(0); + let y = BigInt::from(0); + assert!(x == y); + let x = BigInt::from(10); + let y = BigInt::from(-10); + assert!(x != y); + let x = BigInt::from(11); + let y = BigInt::from(10); + assert!(x != y); + let x = BigInt::from(-0); + let y = BigInt::from(0); + assert!(x == y); + } } fn main() {} From e9aae77b07e8f7cfe8e299f9152e73e6bf806fdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 9 Jul 2025 21:39:31 +0200 Subject: [PATCH 019/243] Adds more tests for BigInt::from_str --- src/main.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main.rs b/src/main.rs index 89ad29b..83a83d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -100,6 +100,31 @@ mod tests { assert!(x.is_err()); } + #[test] + fn from_string_words_from_str_digits() { + let x = BigInt::from_str("two zero"); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [2, 0].to_vec()); + let x = BigInt::from_str("minus two four"); + assert_eq!(x.positive, false); + assert_eq!(x.numbers, [2, 4].to_vec()); + let x = BigInt::from_str("two five five zero zero two one"); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [2, 5, 5, 0, 0, 2, 1].to_vec()); + let x = BigInt::from_str("minus two zero zero zero zero zero one"); + assert_eq!(x.positive, false); + assert_eq!(x.numbers, [2, 0, 0, 0, 0, 0, 1].to_vec()); + let x = BigInt::from_str("zero"); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [0].to_vec()); + let x = BigInt::from_str("onse"); + assert!(x.is_err()); + let x = BigInt::from_str("twenty thousand thousand"); + assert!(x.is_err()); + let x = BigInt::from_str("twenty thousand hundred"); + assert!(x.is_err()); + } + #[test] fn from_string_words_try_from() { let x = BigInt::try_from("twenty"); From 674b372a1a994c6931bc31bb25fc6a183d2e6739 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 9 Jul 2025 21:39:57 +0200 Subject: [PATCH 020/243] Adds tests for greater than trait --- src/main.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main.rs b/src/main.rs index 83a83d3..34233b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -221,6 +221,31 @@ mod tests { let y = BigInt::from(0); assert!(x == y); } + + #[test] + fn greater() { + let x = BigInt::from(15); + let y = BigInt::from(10); + assert!(x > y); + let x = BigInt::from(10); + let y = BigInt::from(10); + assert!(!(x > y)); + let x = BigInt::from(10); + let y = BigInt::from(10); + assert!(x >= y); + let x = BigInt::from(101010); + let y = BigInt::from(101010); + assert!(x >= y); + let x = BigInt::from(0); + let y = BigInt::from(0); + assert!(!(x > y)); + let x = BigInt::from(10); + let y = BigInt::from(-10); + assert!(x > y); + let x = BigInt::from(11); + let y = BigInt::from(10); + assert!(x > y); + } } fn main() {} From df44cfacf0c629a13e12b920c7c6e1738dd17f78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 9 Jul 2025 21:56:10 +0200 Subject: [PATCH 021/243] more tests for BigInt::greater_then --- src/main.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 34233b0..ba92408 100644 --- a/src/main.rs +++ b/src/main.rs @@ -224,9 +224,12 @@ mod tests { #[test] fn greater() { - let x = BigInt::from(15); + let x: BigInt = BigInt::from(15); let y = BigInt::from(10); assert!(x > y); + let x: BigInt = BigInt::from(8); + let y = BigInt::from(7); + assert!(x > y); let x = BigInt::from(10); let y = BigInt::from(10); assert!(!(x > y)); From 2884bef4f02752bca2bdc16f925f20eedc746f72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 9 Jul 2025 21:56:33 +0200 Subject: [PATCH 022/243] add tests for BigInt::less_then --- src/main.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/main.rs b/src/main.rs index ba92408..e09a0c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -249,6 +249,34 @@ mod tests { let y = BigInt::from(10); assert!(x > y); } + + #[test] + fn lesser() { + let x: BigInt = BigInt::from(0); + let y = BigInt::from(10); + assert!(x < y); + let x: BigInt = BigInt::from(8); + let y = BigInt::from(9); + assert!(x < y); + let x = BigInt::from(10); + let y = BigInt::from(10); + assert!(!(x < y)); + let x = BigInt::from(10); + let y = BigInt::from(10); + assert!(x <= y); + let x = BigInt::from(99999999999); + let y = BigInt::from(99999999999); + assert!(x <= y); + let x = BigInt::from(0); + let y = BigInt::from(0); + assert!(!(x < y)); + let x = BigInt::from(-10); + let y = BigInt::from(10); + assert!(x < y); + let x = BigInt::from(11); + let y = BigInt::from(99999999999); + assert!(x < y); + } } fn main() {} From 49df7f03ef34ea25573bd15e6bfe37fd53298d37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 9 Jul 2025 22:44:14 +0200 Subject: [PATCH 023/243] add tests for add trait --- src/main.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/main.rs b/src/main.rs index e09a0c0..768c011 100644 --- a/src/main.rs +++ b/src/main.rs @@ -277,6 +277,48 @@ mod tests { let y = BigInt::from(99999999999); assert!(x < y); } + + #[test] + fn add() { + let mut x = BigInt::from(10); + let y = BigInt::from(10); + let z = x + y; + x += y; + assert_eq!(z, 20); + assert_eq!(z, x); + + let x = BigInt::from(101010); + let y = BigInt::from(101010); + let z = x + y; + x += y; + assert_eq!(z, 202020); + assert_eq!(z, x); + + let x = BigInt::from(0); + let y = BigInt::from(0); + let z = x + y; + x += y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let x = BigInt::from(10); + let y = BigInt::from(-10); + let z = x + y; + assert_eq!(z, 0); + assert!(x != y); + + let x = BigInt::from(11); + let y = BigInt::from(10); + let z = x + y; + assert_eq!(z, 21); + assert!(x != y); + + let x = BigInt::from(-0); + let y = BigInt::from(0); + let z = x + y; + assert_eq!(z, 0); + assert!(x == y); + } } fn main() {} From f0dd65175d8249530c1c16357509148d733d0674 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 9 Jul 2025 23:00:17 +0200 Subject: [PATCH 024/243] add mut for add tests --- src/main.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 768c011..242c09d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -280,40 +280,40 @@ mod tests { #[test] fn add() { - let mut x = BigInt::from(10); + let mut x: BigInt = BigInt::from(10); let y = BigInt::from(10); let z = x + y; x += y; assert_eq!(z, 20); assert_eq!(z, x); - let x = BigInt::from(101010); + let mut x = BigInt::from(101010); let y = BigInt::from(101010); let z = x + y; x += y; assert_eq!(z, 202020); assert_eq!(z, x); - let x = BigInt::from(0); + let mut x = BigInt::from(0); let y = BigInt::from(0); let z = x + y; x += y; assert_eq!(z, 0); assert_eq!(z, x); - let x = BigInt::from(10); + let mut x = BigInt::from(10); let y = BigInt::from(-10); let z = x + y; assert_eq!(z, 0); assert!(x != y); - let x = BigInt::from(11); + let mut x = BigInt::from(11); let y = BigInt::from(10); let z = x + y; assert_eq!(z, 21); assert!(x != y); - let x = BigInt::from(-0); + let mut x = BigInt::from(-0); let y = BigInt::from(0); let z = x + y; assert_eq!(z, 0); From e2aef3225da3fea1f8719e5f055409f20d039f77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 9 Jul 2025 23:01:44 +0200 Subject: [PATCH 025/243] add forgotten += tests --- src/main.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main.rs b/src/main.rs index 242c09d..8e7fb7f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -304,18 +304,21 @@ mod tests { let mut x = BigInt::from(10); let y = BigInt::from(-10); let z = x + y; + x += y; assert_eq!(z, 0); assert!(x != y); let mut x = BigInt::from(11); let y = BigInt::from(10); let z = x + y; + x += y; assert_eq!(z, 21); assert!(x != y); let mut x = BigInt::from(-0); let y = BigInt::from(0); let z = x + y; + x += y; assert_eq!(z, 0); assert!(x == y); } From ce397f883f8440ce55fafcdde20ecf50dad92228 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 9 Jul 2025 23:05:54 +0200 Subject: [PATCH 026/243] add more add tests --- src/main.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main.rs b/src/main.rs index 8e7fb7f..5f3b83d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -321,6 +321,21 @@ mod tests { x += y; assert_eq!(z, 0); assert!(x == y); + + let mut x = BigInt::from(6); + let y = BigInt::from(4); + let z = x + y; + x += y; + assert_eq!(z, 10); + assert!(x == y); + + let mut x = BigInt::from(-15); + let y = BigInt::from(-4); + let z = x + y; + x += y; + assert_eq!(z, -19); + assert!(x == y); + } } } From 7338ada4d9014e90633e1836dc5dcc7ef9021a49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 9 Jul 2025 23:06:24 +0200 Subject: [PATCH 027/243] add tests for sub trait --- src/main.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/main.rs b/src/main.rs index 5f3b83d..56e1ab0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -336,6 +336,71 @@ mod tests { assert_eq!(z, -19); assert!(x == y); } + + #[test] + fn sub() { + let mut x: BigInt = BigInt::from(10); + let y = BigInt::from(10); + let z = x - y; + x -= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x = BigInt::from(101010); + let y = BigInt::from(10); + let z = x - y; + x -= y; + assert_eq!(z, 101000); + assert_eq!(z, x); + + let mut x = BigInt::from(0); + let y = BigInt::from(0); + let z = x - y; + x -= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x = BigInt::from(10); + let y = BigInt::from(-10); + let z = x - y; + x -= y; + assert_eq!(z, 20); + assert!(x != y); + + let mut x = BigInt::from(-10); + let y = BigInt::from(10); + let z = x - y; + x -= y; + assert_eq!(z, -20); + assert!(x != y); + + let mut x = BigInt::from(11); + let y = BigInt::from(10); + let z = x - y; + x -= y; + assert_eq!(z, 1); + assert!(x != y); + + let mut x = BigInt::from(-0); + let y = BigInt::from(0); + let z = x - y; + x -= y; + assert_eq!(z, 0); + assert!(x == y); + + let mut x = BigInt::from(6); + let y = BigInt::from(4); + let z = x - y; + x -= y; + assert_eq!(z, 2); + assert!(x == y); + + let mut x = BigInt::from(-15); + let y = BigInt::from(-4); + let z = x - y; + x -= y; + assert_eq!(z, -11); + assert!(x == y); } } From 72714e33dd7dcf691ddc31b5c8f3baeb64cbff8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 9 Jul 2025 23:57:55 +0200 Subject: [PATCH 028/243] add tests for neg trait --- src/main.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main.rs b/src/main.rs index 56e1ab0..244c432 100644 --- a/src/main.rs +++ b/src/main.rs @@ -200,6 +200,22 @@ mod tests { ); } + #[test] + fn negation() { + let mut x = BigInt::from(1); + assert_eq!(x.positive, true); + assert_eq!(x.to_string(), "1"); + x = !x; + assert_eq!(x.positive, false); + assert_eq!(x.to_string(), "-1"); + let mut x = BigInt::from(-22); + assert_eq!(x.positive, false); + assert_eq!(x.to_string(), "-22"); + x = !x; + assert_eq!(x.positive, true); + assert_eq!(x.to_string(), "22"); + } + #[test] fn equal() { let x = BigInt::from(10); From 45779e5e40562dce24c8a361bb265b122f8001dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 10 Jul 2025 00:13:29 +0200 Subject: [PATCH 029/243] adds tests for mul trait --- src/main.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/main.rs b/src/main.rs index 244c432..5b0a73b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -418,6 +418,72 @@ mod tests { assert_eq!(z, -11); assert!(x == y); } + + #[test] + fn mul() { + let mut x: BigInt = BigInt::from(10); + let y = BigInt::from(10); + let z = x * y; + x *= y; + assert_eq!(z, 100); + assert_eq!(z, x); + + let mut x = BigInt::from(101); + let y = BigInt::from(101); + let z = x * y; + x *= y; + assert_eq!(z, 10201); + assert_eq!(z, x); + + let mut x = BigInt::from(0); + let y = BigInt::from(0); + let z = x * y; + x *= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x = BigInt::from(20100000100); + let y = BigInt::from(0); + let z = x * y; + x *= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x = BigInt::from(10); + let y = BigInt::from(-10); + let z = x * y; + x *= y; + assert_eq!(z, -100); + assert!(x != y); + + let mut x = BigInt::from(11); + let y = BigInt::from(10); + let z = x * y; + x *= y; + assert_eq!(z, 110); + assert!(x != y); + + let mut x = BigInt::from(-0); + let y = BigInt::from(0); + let z = x * y; + x *= y; + assert_eq!(z, 0); + assert!(x == y); + + let mut x = BigInt::from(6); + let y = BigInt::from(4); + let z = x * y; + x *= y; + assert_eq!(z, 24); + assert!(x == y); + + let mut x = BigInt::from(-15); + let y = BigInt::from(-4); + let z = x * y; + x *= y; + assert_eq!(z, 60); + assert!(x == y); + } } fn main() {} From 9155bd3922528a3de2638d74c9f7dcc94ee0474b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 10 Jul 2025 00:18:14 +0200 Subject: [PATCH 030/243] adds tests for div trait --- src/main.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/src/main.rs b/src/main.rs index 5b0a73b..72b5813 100644 --- a/src/main.rs +++ b/src/main.rs @@ -484,6 +484,79 @@ mod tests { assert_eq!(z, 60); assert!(x == y); } + + #[test] + fn div() { + let mut x: BigInt = BigInt::from(10000); + let y = BigInt::from(10); + let z = x / y; + x /= y; + assert_eq!(z, 1000); + assert_eq!(z, x); + + let mut x = BigInt::from(101); + let y = BigInt::from(101); + let z = x / y; + x /= y; + assert_eq!(z, 1); + assert_eq!(z, x); + + let mut x = BigInt::from(0); + let y = BigInt::from(2); + let z = x / y; + x /= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x = BigInt::from(0); + let y = BigInt::from(0); + let z = x / y; + x /= y; + assert_eq!(z, BigInt::default()); + assert_eq!(z, x); + + let mut x = BigInt::from(10000); + let y = BigInt::from(0); + let z = x / y; + x /= y; + assert_eq!(z, BigInt::default()); + assert_eq!(z, x); + + let mut x = BigInt::from(20100000100); + let y = BigInt::from(200); + let z = x / y; + x /= y; + assert_eq!(z, 100500); + assert_eq!(z, x); + + let mut x = BigInt::from(10); + let y = BigInt::from(-10); + let z = x / y; + x /= y; + assert_eq!(z, -1); + assert!(x != y); + + let mut x = BigInt::from(110); + let y = BigInt::from(10); + let z = x / y; + x /= y; + assert_eq!(z, 11); + assert!(x != y); + + let mut x = BigInt::from(6); + let y = BigInt::from(2); + let z = x / y; + x /= y; + assert_eq!(z, 3); + assert!(x == y); + + let mut x = BigInt::from(-15); + let y = BigInt::from(-5); + let z = x / y; + x /= y; + assert_eq!(z, 3); + assert!(x == y); + } } fn main() {} From 3f3392feea2f43ab61a761659b6c4189f73430fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 10 Jul 2025 00:23:44 +0200 Subject: [PATCH 031/243] adds tests for reminder trait --- src/main.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/main.rs b/src/main.rs index 72b5813..a50044b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -557,6 +557,58 @@ mod tests { assert_eq!(z, 3); assert!(x == y); } + + #[test] + fn reminder() { + let mut x: BigInt = BigInt::from(10000); + let y = BigInt::from(10); + let z = x % y; + x %= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x: BigInt = BigInt::from(10); + let y = BigInt::from(0); + let z = x % y; + x %= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x: BigInt = BigInt::from(10); + let y = BigInt::from(7); + let z = x % y; + x %= y; + assert_eq!(z, 3); + assert_eq!(z, x); + + let mut x: BigInt = BigInt::from(10000); + let y = BigInt::from(10); + let z = x % y; + x %= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x: BigInt = BigInt::from(-104); + let y = BigInt::from(10); + let z = x % y; + x %= y; + assert_eq!(z, 4); + assert_eq!(z, x); + + let mut x: BigInt = BigInt::from(24); + let y = BigInt::from(3); + let z = x % y; + x %= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x: BigInt = BigInt::from(33); + let y = BigInt::from(7); + let z = x % y; + x %= y; + assert_eq!(z, 5); + assert_eq!(z, x); + } } fn main() {} From 8bd21e299c3fa707b7a1fc40db538baec932a4f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 10 Jul 2025 12:45:50 +0200 Subject: [PATCH 032/243] adds tests for binary trait --- src/main.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main.rs b/src/main.rs index a50044b..c4ae413 100644 --- a/src/main.rs +++ b/src/main.rs @@ -609,6 +609,22 @@ mod tests { assert_eq!(z, 5); assert_eq!(z, x); } + + #[test] + fn binary() { + let x = BigInt::from(4); + assert_eq!(format!("{x:b}"), "100"); + let x = BigInt::from(4); + assert_eq!(format!("{x:#b}"), "0b100"); + let x = BigInt::from(4); + assert_eq!(format!("{x:010b}"), "0000000100"); + let x = BigInt::from(10); + assert_eq!(format!("{x:#110b}"), "0b11111010"); + let x = BigInt::from(172); + assert_eq!(format!("{x:b}"), "10101100"); + let x = BigInt::from(17220003931); + assert_eq!(format!("{x:#b}"), "0b10000000010011001000110100001011011"); + } } fn main() {} From 7199a02c9b3886382d8b273d25624c90b5780ee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 10 Jul 2025 13:01:58 +0200 Subject: [PATCH 033/243] add tests for bit_and trait --- src/main.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/main.rs b/src/main.rs index c4ae413..9dced49 100644 --- a/src/main.rs +++ b/src/main.rs @@ -625,6 +625,44 @@ mod tests { let x = BigInt::from(17220003931); assert_eq!(format!("{x:#b}"), "0b10000000010011001000110100001011011"); } + + #[test] + fn bit_and() { + let mut x = BigInt::from(4); //100 + let y = BigInt::from(12); //1100 + let z = x & y; //0100 + x &= y; + assert_eq!(x, z); + assert_eq!(z, 4); + + let mut x = BigInt::from(10); //1010 + let y = BigInt::from(13); //1101 + let z = x & y; //1000 + x &= y; + assert_eq!(x, z); + assert_eq!(z, 8); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(223); //11011111 + let z = x & y; //10001100 + x &= y; + assert_eq!(x, z); + assert_eq!(z, 140); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(1); //1 + let z = x & y; //0 + x &= y; + assert_eq!(x, z); + assert_eq!(z, 0); + + let mut x = BigInt::from(173); //10101101 + let y = BigInt::from(1); //1 + let z = x & y; //1 + x &= y; + assert_eq!(x, z); + assert_eq!(z, 1); + } } fn main() {} From 10b143daba1eba8d730c2e2f3c8c661cb8f689a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 10 Jul 2025 13:02:39 +0200 Subject: [PATCH 034/243] add tests for bit_or trait --- src/main.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/main.rs b/src/main.rs index 9dced49..ba41fb8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -663,6 +663,44 @@ mod tests { assert_eq!(x, z); assert_eq!(z, 1); } + + #[test] + fn bit_or() { + let mut x = BigInt::from(4); //100 + let y = BigInt::from(12); //1100 + let z = x | y; //1100 + x |= y; + assert_eq!(x, z); + assert_eq!(z, 12); + + let mut x = BigInt::from(10); //1010 + let y = BigInt::from(13); //1101 + let z = x | y; //1111 + x |= y; + assert_eq!(x, z); + assert_eq!(z, 15); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(223); //11011111 + let z = x | y; //11111111 + x |= y; + assert_eq!(x, z); + assert_eq!(z, 255); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(1); //1 + let z = x | y; //10101101 + x |= y; + assert_eq!(x, z); + assert_eq!(z, 173); + + let mut x = BigInt::from(173); //10101101 + let y = BigInt::from(1); //1 + let z = x | y; //10101101 + x |= y; + assert_eq!(x, z); + assert_eq!(z, 173); + } } fn main() {} From 57c8adaaa63adb6d6c20480b6c7140be3bd60726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 10 Jul 2025 13:05:53 +0200 Subject: [PATCH 035/243] add tests for bit_xor --- src/main.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/main.rs b/src/main.rs index ba41fb8..68ed162 100644 --- a/src/main.rs +++ b/src/main.rs @@ -701,6 +701,44 @@ mod tests { assert_eq!(x, z); assert_eq!(z, 173); } + + #[test] + fn bit_xor() { + let mut x = BigInt::from(4); //100 + let y = BigInt::from(12); //1100 + let z = x ^ y; //1000 + x ^= y; + assert_eq!(x, z); + assert_eq!(z, 8); + + let mut x = BigInt::from(10); //1010 + let y = BigInt::from(13); //1101 + let z = x ^ y; //0111 + x ^= y; + assert_eq!(x, z); + assert_eq!(z, 7); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(223); //11011111 + let z = x ^ y; //01110011 + x ^= y; + assert_eq!(x, z); + assert_eq!(z, 115); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(1); //1 + let z = x ^ y; //10101101 + x ^= y; + assert_eq!(x, z); + assert_eq!(z, 173); + + let mut x = BigInt::from(173); //10101101 + let y = BigInt::from(1); //1 + let z = x ^ y; //10101100 + x ^= y; + assert_eq!(x, z); + assert_eq!(z, 172); + } } fn main() {} From b58ddec0531f86dd91a09d5c0ca94af6c082d27b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 10 Jul 2025 13:09:58 +0200 Subject: [PATCH 036/243] add tests for bit_shift_left trait --- src/main.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/main.rs b/src/main.rs index 68ed162..772d594 100644 --- a/src/main.rs +++ b/src/main.rs @@ -739,6 +739,44 @@ mod tests { assert_eq!(x, z); assert_eq!(z, 172); } + + #[test] + fn bit_shift_left() { + let mut x = BigInt::from(4); //100 + let y = BigInt::from(2); + let z = x << y; //10000 + x <<= y; + assert_eq!(x, z); + assert_eq!(z, 16); + + let mut x = BigInt::from(10); //1010 + let y = BigInt::from(1); + let z = x << y; //10100 + x <<= y; + assert_eq!(x, z); + assert_eq!(z, 20); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(0); + let z = x << y; + x <<= y; + assert_eq!(x, z); + assert_eq!(z, 172); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(10); + let z = x << y; //101011000000000000 + x <<= y; + assert_eq!(x, z); + assert_eq!(z, 176128); + + let mut x = BigInt::from(173); //10101101 + let y = BigInt::from(1); + let z = x << y; //101011010 + x <<= y; + assert_eq!(x, z); + assert_eq!(z, 346); + } } fn main() {} From dcf8a5a5ba90e10bc08120572f4c6171b29faa9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 10 Jul 2025 13:14:26 +0200 Subject: [PATCH 037/243] add tests bit_shift_right trait --- src/main.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/main.rs b/src/main.rs index 772d594..2aa635c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -777,6 +777,44 @@ mod tests { assert_eq!(x, z); assert_eq!(z, 346); } + + #[test] + fn bit_shift_right() { + let mut x = BigInt::from(4); //100 + let y = BigInt::from(2); + let z = x >> y; //1 + x >>= y; + assert_eq!(x, z); + assert_eq!(z, 1); + + let mut x = BigInt::from(10); //1010 + let y = BigInt::from(1); + let z = x >> y; //101 + x >>= y; + assert_eq!(x, z); + assert_eq!(z, 5); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(0); + let z = x >> y; //10101100 + x >>= y; + assert_eq!(x, z); + assert_eq!(z, 172); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(10); + let z = x >> y; //0 + x >>= y; + assert_eq!(x, z); + assert_eq!(z, 0); + + let mut x = BigInt::from(173); //10101101 + let y = BigInt::from(1); + let z = x >> y; //1010110 + x >>= y; + assert_eq!(x, z); + assert_eq!(z, 86); + } } fn main() {} From b760833455822f1a555cd10621738d394c5605e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 10 Jul 2025 14:41:00 +0200 Subject: [PATCH 038/243] add tests for hex format --- src/main.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main.rs b/src/main.rs index 2aa635c..713a7d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -815,6 +815,20 @@ mod tests { assert_eq!(x, z); assert_eq!(z, 86); } + + #[test] + fn hexadecimal() { + let x = BigInt::from(4); + assert_eq!(format!("{x:X}"), "4"); + let x = BigInt::from(16); + assert_eq!(format!("{x:#X}"), "0xF"); + let x = BigInt::from(10); + assert_eq!(format!("{x:#X}"), "0x10"); + let x = BigInt::from(172); + assert_eq!(format!("{x:X}"), "AC"); + let x = BigInt::from(17220003931); + assert_eq!(format!("{x:#X}"), "0x40264685B"); + } } fn main() {} From 18ec1f0866f1ae36b9cf0c6224f81e4df3df81fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 10 Jul 2025 21:09:56 +0200 Subject: [PATCH 039/243] add tests from ProgTest --- src/main.rs | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/src/main.rs b/src/main.rs index 713a7d0..c263507 100644 --- a/src/main.rs +++ b/src/main.rs @@ -829,6 +829,99 @@ mod tests { let x = BigInt::from(17220003931); assert_eq!(format!("{x:#X}"), "0x40264685B"); } + + #[test] + fn progtest_tests() { + let mut a = BigInt::new(); + let mut b = BigInt::new(); + a = BigInt::from(10); + a += BigInt::from(20); + assert_eq!(a, 30); + a *= BigInt::from(5); + assert_eq!(a, 150); + b = a + BigInt::from(3); + assert_eq!(b, 153); + b = a * BigInt::from(7); + assert_eq!(b, 1050); + assert_eq!(a, 150); + assert_eq!(format!("{a:X}"), "96"); + + a = BigInt::from(10); + a += BigInt::from(-20); + assert_eq!(a, -10); + a *= BigInt::from(5); + assert_eq!(a, -50); + b = a + BigInt::from(73); + assert_eq!(b, 23); + b = a * BigInt::from(-7); + assert_eq!(b, 350); + assert_eq!(a, -50); + assert_eq!(format!("{a:X}"), "-32") + + a = BigInt::from(12345678901234567890); + a += BigInt::from(-99999999999999999999); + assert_eq!(a, -87654321098765432109); + a *= BigInt::from(54321987654321987654); + assert_eq!(a,-4761556948575111126880627366067073182286); + a *= BigInt::from(0); + assert_eq!(a, 0); + a = BigInt::from(10); + b = a + BigInt::from_str("400"); + assert_eq!(b, "410"); + b = a * BigInt::from_str("15"); + assert_eq!(b, "150"); + assert_eq!(a, "10"); + assert_eq!(format!("{a:X}"), "A"); + + b = BigInt::from_str("1234"); + assert_eq!(format!("{b}"), "1234"); + assert!(BigInt::from_str(" 12 34").is_err()); + assert!(BigInt::from_str("999z").is_err()); + assert!(BigInt::from_str("abcd").is_err()); + assert!(BigInt::from_str("-xyz").is_err()); + assert!(BigInt::from_str(":").is_err()); + assert!(BigInt::from_str("%").is_err()); + assert!(BigInt::from_str("- 758").is_err()); + a = BigInt::from(42); + assert_eq!(a, 42); + + a = BigInt::from(73786976294838206464); + assert_eq!(a, 73786976294838206464); + assert_eq!(format!("{a:X}"), "40000000000000000"); + assert!(a < 1361129467683753853853498429727072845824); + assert!(a <= 1361129467683753853853498429727072845824); + assert!(!(a > 1361129467683753853853498429727072845824)); + assert!(!(a >= 1361129467683753853853498429727072845824)); + assert!(!(a == 1361129467683753853853498429727072845824)); + assert!(a != 1361129467683753853853498429727072845824); + assert!(!(a < 73786976294838206464)); + assert!(a <= 73786976294838206464); + assert!(!(a > 73786976294838206464)); + assert!(a >= 73786976294838206464); + assert!(a == 73786976294838206464); + assert!(!(a != 73786976294838206464)); + assert!(a < 73786976294838206465); + assert!(a <= 73786976294838206465); + assert!(!(a > 73786976294838206465)); + assert!(!(a >= 73786976294838206465)); + assert!(!(a == 73786976294838206465)); + assert!(a != 73786976294838206465); + a = BigInt::from_str("2147483648"); + assert!(!(a < -2147483648)); + assert!(!(a <= -2147483648)); + assert!(a > -2147483648); + assert!(a >= -2147483648); + assert!(!(a == -2147483648)); + assert!(a != -2147483648); + a = BigInt::from_str("-12345678"); + assert!(!(a < -87654321)); + assert!(!(a <= -87654321)); + assert!(a > -87654321); + assert!(a >= -87654321); + assert!(!(a == -87654321)); + assert!(a != -87654321); + + } } fn main() {} From 68dc3fd595b5b6fa1059b2b4762fb02b68f95f49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 10 Jul 2025 21:10:09 +0200 Subject: [PATCH 040/243] change of package name --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 047cff1..b0d4221 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,5 +3,5 @@ version = 3 [[package]] -name = "lorem" +name = "BigInt" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 19456f9..a43cb48 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "lorem" +name = "BigInt" version = "0.1.0" edition = "2021" From 8d40310631f01e25c2f899a01507107d787b2e89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Fri, 11 Jul 2025 00:28:59 +0200 Subject: [PATCH 041/243] add unwrap to tests --- src/main.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index c263507..c422907 100644 --- a/src/main.rs +++ b/src/main.rs @@ -102,19 +102,19 @@ mod tests { #[test] fn from_string_words_from_str_digits() { - let x = BigInt::from_str("two zero"); + let x = BigInt::from_str("two zero").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [2, 0].to_vec()); - let x = BigInt::from_str("minus two four"); + let x = BigInt::from_str("minus two four").unwrap(); assert_eq!(x.positive, false); assert_eq!(x.numbers, [2, 4].to_vec()); - let x = BigInt::from_str("two five five zero zero two one"); + let x = BigInt::from_str("two five five zero zero two one").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [2, 5, 5, 0, 0, 2, 1].to_vec()); - let x = BigInt::from_str("minus two zero zero zero zero zero one"); + let x = BigInt::from_str("minus two zero zero zero zero zero one").unwrap(); assert_eq!(x.positive, false); assert_eq!(x.numbers, [2, 0, 0, 0, 0, 0, 1].to_vec()); - let x = BigInt::from_str("zero"); + let x = BigInt::from_str("zero").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [0].to_vec()); let x = BigInt::from_str("onse"); From 502e10f8e66fa1a688c7aec74c2041a46fb7842e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Fri, 11 Jul 2025 00:29:13 +0200 Subject: [PATCH 042/243] remove try_from tests --- src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index c422907..4b396e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -126,7 +126,6 @@ mod tests { } #[test] - fn from_string_words_try_from() { let x = BigInt::try_from("twenty"); assert_eq!(x.unwrap().positive, true); assert_eq!(x.unwrap().numbers, [2, 0].to_vec()); From f5a3f6f0693fd47e59eb7d521537e7303d035686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Fri, 11 Jul 2025 00:29:46 +0200 Subject: [PATCH 043/243] remove try_from tests --- src/main.rs | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4b396e4..501a73d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -125,27 +125,6 @@ mod tests { assert!(x.is_err()); } - #[test] - let x = BigInt::try_from("twenty"); - assert_eq!(x.unwrap().positive, true); - assert_eq!(x.unwrap().numbers, [2, 0].to_vec()); - let x = BigInt::try_from("minus twenty four"); - assert_eq!(x.unwrap().positive, false); - assert_eq!(x.unwrap().numbers, [2, 4].to_vec()); - let x = BigInt::try_from("two milion five hundred fifty thousand twenty one"); - assert_eq!(x.unwrap().positive, true); - assert_eq!(x.unwrap().numbers, [2, 5, 5, 0, 0, 2, 1].to_vec()); - let x = BigInt::try_from("minus two milion one"); - assert_eq!(x.unwrap().positive, false); - assert_eq!(x.unwrap().numbers, [2, 0, 0, 0, 0, 0, 1].to_vec()); - let x = BigInt::try_from("onse"); - assert!(x.is_err()); - let x = BigInt::try_from("twenty thousand thousand"); - assert!(x.is_err()); - let x = BigInt::try_from("twenty thousand hundred"); - assert!(x.is_err()); - } - #[test] fn display() { let x = BigInt::from(1003); From 4a0bd503d8235f9dbf83501fd94527c998273762 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Fri, 11 Jul 2025 23:24:50 +0200 Subject: [PATCH 044/243] minor changes in tests --- src/main.rs | 264 ++++++++++++++++++++++++++-------------------------- 1 file changed, 133 insertions(+), 131 deletions(-) diff --git a/src/main.rs b/src/main.rs index 501a73d..ca60460 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,7 +45,7 @@ mod tests { let x = BigInt::from(-20); assert_eq!(x.positive, false); assert_eq!(x.numbers, [2, 0].to_vec()); - let x = BigInt::from(-320020000981234567890); + let x = BigInt::from(-320020000981234567890i128); assert_eq!(x.positive, false); assert_eq!( x.numbers, @@ -55,41 +55,41 @@ mod tests { #[test] fn from_string_numbers() { - let x = BigInt::from_str("20"); + let x = BigInt::from_str("20").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [2, 0].to_vec()); - let x = BigInt::from_str("666"); + let x = BigInt::from_str("666").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [6, 6, 6].to_vec()); - let x = BigInt::from_str("-20"); + let x = BigInt::from_str("-20").unwrap(); assert_eq!(x.positive, false); assert_eq!(x.numbers, [2, 0].to_vec()); - let x = BigInt::from_str("-320020000981234567890"); + let x = BigInt::from_str("-320020000981234567890").unwrap(); assert_eq!(x.positive, false); assert_eq!( x.numbers, [3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0].to_vec() ); - let x = BigInt::from_str("-0"); + let x = BigInt::from_str("-0").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [0].to_vec()); } #[test] fn from_string_words_from_str() { - let x = BigInt::from_str("twenty"); + let x = BigInt::from_str("twenty").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [2, 0].to_vec()); - let x = BigInt::from_str("minus twenty four"); + let x = BigInt::from_str("minus twenty four").unwrap(); assert_eq!(x.positive, false); assert_eq!(x.numbers, [2, 4].to_vec()); - let x = BigInt::from_str("two milion five hundred fifty thousand twenty one"); + let x = BigInt::from_str("two milion five hundred fifty thousand twenty one").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [2, 5, 5, 0, 0, 2, 1].to_vec()); - let x = BigInt::from_str("minus two milion one"); + let x = BigInt::from_str("minus two milion one").unwrap(); assert_eq!(x.positive, false); assert_eq!(x.numbers, [2, 0, 0, 0, 0, 0, 1].to_vec()); - let x = BigInt::from_str("zero"); + let x = BigInt::from_str("zero").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [0].to_vec()); let x = BigInt::from_str("onse"); @@ -137,7 +137,7 @@ mod tests { assert_eq!(format!("Number is: {x}"), "Number is: -0"); let x = BigInt::from(-1); assert_eq!(format!("Number is: {x}"), "Number is: -1"); - let x = BigInt::from(320020000981234567890); + let x = BigInt::from(320020000981234567890i128); assert_eq!( format!("Number is: {x}"), "Number is: 320020000981234567890" @@ -156,7 +156,7 @@ mod tests { assert_eq!(format!("Number is: {x:?}"), "Number is: -0"); let x = BigInt::from(-1); assert_eq!(format!("Number is: {x:?}"), "Number is: -1"); - let x = BigInt::from(320020000981234567890); + let x = BigInt::from(320020000981234567890i128); assert_eq!( format!("Number is: {x:?}"), "Number is: +320020000981234567890" @@ -171,7 +171,7 @@ mod tests { assert_eq!(x.to_words(), "minus two four"); let x = BigInt::from(2550021); assert_eq!(x.to_words(), "two five five zero zero two one"); - let x = BigInt::from(-2000000000001); + let x = BigInt::from(-2000000000001i128); assert_eq!( x.to_words(), "minus two zero zero zero zero zero zero zero zero zero zero zero one" @@ -186,6 +186,9 @@ mod tests { x = !x; assert_eq!(x.positive, false); assert_eq!(x.to_string(), "-1"); + x = !x; + assert_eq!(x.positive, true); + assert_eq!(x.to_string(), "1"); let mut x = BigInt::from(-22); assert_eq!(x.positive, false); assert_eq!(x.to_string(), "-22"); @@ -258,8 +261,8 @@ mod tests { let x = BigInt::from(10); let y = BigInt::from(10); assert!(x <= y); - let x = BigInt::from(99999999999); - let y = BigInt::from(99999999999); + let x = BigInt::from(99999999999i64); + let y = BigInt::from(99999999999i128); assert!(x <= y); let x = BigInt::from(0); let y = BigInt::from(0); @@ -268,7 +271,7 @@ mod tests { let y = BigInt::from(10); assert!(x < y); let x = BigInt::from(11); - let y = BigInt::from(99999999999); + let y = BigInt::from(99999999999u64); assert!(x < y); } @@ -276,313 +279,313 @@ mod tests { fn add() { let mut x: BigInt = BigInt::from(10); let y = BigInt::from(10); - let z = x + y; + let z = x.clone() + y.clone(); x += y; assert_eq!(z, 20); assert_eq!(z, x); let mut x = BigInt::from(101010); let y = BigInt::from(101010); - let z = x + y; + let z = x.clone() + y.clone(); x += y; assert_eq!(z, 202020); assert_eq!(z, x); let mut x = BigInt::from(0); let y = BigInt::from(0); - let z = x + y; + let z = x.clone() + y.clone(); x += y; assert_eq!(z, 0); assert_eq!(z, x); let mut x = BigInt::from(10); let y = BigInt::from(-10); - let z = x + y; + let z = x.clone() + y.clone(); x += y; assert_eq!(z, 0); - assert!(x != y); + assert_ne!(x, y); let mut x = BigInt::from(11); let y = BigInt::from(10); - let z = x + y; + let z = x.clone() + y.clone(); x += y; assert_eq!(z, 21); - assert!(x != y); + assert_ne!(x, y); let mut x = BigInt::from(-0); let y = BigInt::from(0); - let z = x + y; + let z = x.clone() + y.clone(); x += y; assert_eq!(z, 0); - assert!(x == y); + assert_eq!(x, y); let mut x = BigInt::from(6); let y = BigInt::from(4); - let z = x + y; + let z = x.clone() + y.clone(); x += y; assert_eq!(z, 10); - assert!(x == y); + assert_eq!(x, y); let mut x = BigInt::from(-15); let y = BigInt::from(-4); - let z = x + y; + let z = x.clone() + y.clone(); x += y; assert_eq!(z, -19); - assert!(x == y); + assert_eq!(x, y); } #[test] fn sub() { let mut x: BigInt = BigInt::from(10); let y = BigInt::from(10); - let z = x - y; + let z = x.clone() - y.clone(); x -= y; assert_eq!(z, 0); assert_eq!(z, x); let mut x = BigInt::from(101010); let y = BigInt::from(10); - let z = x - y; + let z = x.clone() - y.clone(); x -= y; assert_eq!(z, 101000); assert_eq!(z, x); let mut x = BigInt::from(0); let y = BigInt::from(0); - let z = x - y; + let z = x.clone() - y.clone(); x -= y; assert_eq!(z, 0); assert_eq!(z, x); let mut x = BigInt::from(10); let y = BigInt::from(-10); - let z = x - y; + let z = x.clone() - y.clone(); x -= y; assert_eq!(z, 20); - assert!(x != y); + assert_ne!(x, y); let mut x = BigInt::from(-10); let y = BigInt::from(10); - let z = x - y; + let z = x.clone() - y.clone(); x -= y; assert_eq!(z, -20); - assert!(x != y); + assert_ne!(x, y); let mut x = BigInt::from(11); let y = BigInt::from(10); - let z = x - y; + let z = x.clone() - y.clone(); x -= y; assert_eq!(z, 1); - assert!(x != y); + assert_ne!(x, y); let mut x = BigInt::from(-0); let y = BigInt::from(0); - let z = x - y; + let z = x.clone() - y.clone(); x -= y; assert_eq!(z, 0); - assert!(x == y); + assert_eq!(x, y); let mut x = BigInt::from(6); let y = BigInt::from(4); - let z = x - y; + let z = x.clone() - y.clone(); x -= y; assert_eq!(z, 2); - assert!(x == y); + assert_eq!(x, y); let mut x = BigInt::from(-15); let y = BigInt::from(-4); - let z = x - y; + let z = x.clone() - y.clone(); x -= y; assert_eq!(z, -11); - assert!(x == y); + assert_eq!(x, y); } #[test] fn mul() { let mut x: BigInt = BigInt::from(10); let y = BigInt::from(10); - let z = x * y; + let z: BigInt = x.clone() * y.clone(); x *= y; assert_eq!(z, 100); assert_eq!(z, x); let mut x = BigInt::from(101); let y = BigInt::from(101); - let z = x * y; + let z = x.clone() * y.clone(); x *= y; assert_eq!(z, 10201); assert_eq!(z, x); let mut x = BigInt::from(0); let y = BigInt::from(0); - let z = x * y; + let z: BigInt = x.clone() * y.clone(); x *= y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x = BigInt::from(20100000100); + let mut x = BigInt::from(20100000100u64); let y = BigInt::from(0); - let z = x * y; + let z: BigInt = x.clone() * y.clone(); x *= y; assert_eq!(z, 0); assert_eq!(z, x); let mut x = BigInt::from(10); let y = BigInt::from(-10); - let z = x * y; + let z: BigInt = x.clone() * y.clone(); x *= y; assert_eq!(z, -100); - assert!(x != y); + assert_ne!(x, y); let mut x = BigInt::from(11); let y = BigInt::from(10); - let z = x * y; + let z: BigInt = x.clone() * y.clone(); x *= y; assert_eq!(z, 110); - assert!(x != y); + assert_ne!(x, y); let mut x = BigInt::from(-0); let y = BigInt::from(0); - let z = x * y; + let z: BigInt = x.clone() * y.clone(); x *= y; assert_eq!(z, 0); - assert!(x == y); + assert_eq!(x, y); let mut x = BigInt::from(6); let y = BigInt::from(4); - let z = x * y; + let z: BigInt = x.clone() * y.clone(); x *= y; assert_eq!(z, 24); - assert!(x == y); + assert_eq!(x, y); let mut x = BigInt::from(-15); let y = BigInt::from(-4); - let z = x * y; + let z: BigInt = x.clone() * y.clone(); x *= y; assert_eq!(z, 60); - assert!(x == y); + assert_eq!(x, y); } #[test] fn div() { let mut x: BigInt = BigInt::from(10000); let y = BigInt::from(10); - let z = x / y; + let z = x.clone() / y.clone(); x /= y; assert_eq!(z, 1000); assert_eq!(z, x); let mut x = BigInt::from(101); let y = BigInt::from(101); - let z = x / y; + let z = x.clone() / y.clone(); x /= y; assert_eq!(z, 1); assert_eq!(z, x); let mut x = BigInt::from(0); let y = BigInt::from(2); - let z = x / y; + let z = x.clone() / y.clone(); x /= y; assert_eq!(z, 0); assert_eq!(z, x); let mut x = BigInt::from(0); let y = BigInt::from(0); - let z = x / y; + let z = x.clone() / y.clone(); x /= y; assert_eq!(z, BigInt::default()); assert_eq!(z, x); let mut x = BigInt::from(10000); let y = BigInt::from(0); - let z = x / y; + let z = x.clone() / y.clone(); x /= y; assert_eq!(z, BigInt::default()); assert_eq!(z, x); - let mut x = BigInt::from(20100000100); + let mut x = BigInt::from(20100000100u64); let y = BigInt::from(200); - let z = x / y; + let z = x.clone() / y.clone(); x /= y; assert_eq!(z, 100500); assert_eq!(z, x); let mut x = BigInt::from(10); let y = BigInt::from(-10); - let z = x / y; + let z = x.clone() / y.clone(); x /= y; assert_eq!(z, -1); - assert!(x != y); + assert_ne!(x, y); let mut x = BigInt::from(110); let y = BigInt::from(10); - let z = x / y; + let z = x.clone() / y.clone(); x /= y; assert_eq!(z, 11); - assert!(x != y); + assert_ne!(x, y); let mut x = BigInt::from(6); let y = BigInt::from(2); - let z = x / y; + let z = x.clone() / y.clone(); x /= y; assert_eq!(z, 3); - assert!(x == y); + assert_eq!(x, y); let mut x = BigInt::from(-15); let y = BigInt::from(-5); - let z = x / y; + let z = x.clone() / y.clone(); x /= y; assert_eq!(z, 3); - assert!(x == y); + assert_eq!(x, y); } #[test] fn reminder() { let mut x: BigInt = BigInt::from(10000); let y = BigInt::from(10); - let z = x % y; + let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 0); assert_eq!(z, x); let mut x: BigInt = BigInt::from(10); let y = BigInt::from(0); - let z = x % y; + let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 0); assert_eq!(z, x); let mut x: BigInt = BigInt::from(10); let y = BigInt::from(7); - let z = x % y; + let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 3); assert_eq!(z, x); let mut x: BigInt = BigInt::from(10000); let y = BigInt::from(10); - let z = x % y; + let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 0); assert_eq!(z, x); let mut x: BigInt = BigInt::from(-104); let y = BigInt::from(10); - let z = x % y; + let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 4); assert_eq!(z, x); let mut x: BigInt = BigInt::from(24); let y = BigInt::from(3); - let z = x % y; + let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 0); assert_eq!(z, x); let mut x: BigInt = BigInt::from(33); let y = BigInt::from(7); - let z = x % y; + let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 5); assert_eq!(z, x); @@ -600,7 +603,7 @@ mod tests { assert_eq!(format!("{x:#110b}"), "0b11111010"); let x = BigInt::from(172); assert_eq!(format!("{x:b}"), "10101100"); - let x = BigInt::from(17220003931); + let x = BigInt::from(17220003931i64); assert_eq!(format!("{x:#b}"), "0b10000000010011001000110100001011011"); } @@ -608,35 +611,35 @@ mod tests { fn bit_and() { let mut x = BigInt::from(4); //100 let y = BigInt::from(12); //1100 - let z = x & y; //0100 + let z = x.clone() & y.clone(); //0100 x &= y; assert_eq!(x, z); assert_eq!(z, 4); let mut x = BigInt::from(10); //1010 let y = BigInt::from(13); //1101 - let z = x & y; //1000 + let z = x.clone() & y.clone(); //1000 x &= y; assert_eq!(x, z); assert_eq!(z, 8); let mut x = BigInt::from(172); //10101100 let y = BigInt::from(223); //11011111 - let z = x & y; //10001100 + let z = x.clone() & y.clone(); //10001100 x &= y; assert_eq!(x, z); assert_eq!(z, 140); let mut x = BigInt::from(172); //10101100 let y = BigInt::from(1); //1 - let z = x & y; //0 + let z = x.clone() & y.clone(); //0 x &= y; assert_eq!(x, z); assert_eq!(z, 0); let mut x = BigInt::from(173); //10101101 let y = BigInt::from(1); //1 - let z = x & y; //1 + let z = x.clone() & y.clone(); //1 x &= y; assert_eq!(x, z); assert_eq!(z, 1); @@ -646,35 +649,35 @@ mod tests { fn bit_or() { let mut x = BigInt::from(4); //100 let y = BigInt::from(12); //1100 - let z = x | y; //1100 + let z = x.clone() | y.clone(); //1100 x |= y; assert_eq!(x, z); assert_eq!(z, 12); let mut x = BigInt::from(10); //1010 let y = BigInt::from(13); //1101 - let z = x | y; //1111 + let z = x.clone() | y.clone(); //1111 x |= y; assert_eq!(x, z); assert_eq!(z, 15); let mut x = BigInt::from(172); //10101100 let y = BigInt::from(223); //11011111 - let z = x | y; //11111111 + let z = x.clone() | y.clone(); //11111111 x |= y; assert_eq!(x, z); assert_eq!(z, 255); let mut x = BigInt::from(172); //10101100 let y = BigInt::from(1); //1 - let z = x | y; //10101101 + let z = x.clone() | y.clone(); //10101101 x |= y; assert_eq!(x, z); assert_eq!(z, 173); let mut x = BigInt::from(173); //10101101 let y = BigInt::from(1); //1 - let z = x | y; //10101101 + let z = x.clone() | y.clone(); //10101101 x |= y; assert_eq!(x, z); assert_eq!(z, 173); @@ -684,35 +687,35 @@ mod tests { fn bit_xor() { let mut x = BigInt::from(4); //100 let y = BigInt::from(12); //1100 - let z = x ^ y; //1000 + let z = x.clone() ^ y.clone(); //1000 x ^= y; assert_eq!(x, z); assert_eq!(z, 8); let mut x = BigInt::from(10); //1010 let y = BigInt::from(13); //1101 - let z = x ^ y; //0111 + let z = x.clone() ^ y.clone(); //0111 x ^= y; assert_eq!(x, z); assert_eq!(z, 7); let mut x = BigInt::from(172); //10101100 let y = BigInt::from(223); //11011111 - let z = x ^ y; //01110011 + let z = x.clone() ^ y.clone(); //01110011 x ^= y; assert_eq!(x, z); assert_eq!(z, 115); let mut x = BigInt::from(172); //10101100 let y = BigInt::from(1); //1 - let z = x ^ y; //10101101 + let z = x.clone() ^ y.clone(); //10101101 x ^= y; assert_eq!(x, z); assert_eq!(z, 173); let mut x = BigInt::from(173); //10101101 let y = BigInt::from(1); //1 - let z = x ^ y; //10101100 + let z = x.clone() ^ y.clone(); //10101100 x ^= y; assert_eq!(x, z); assert_eq!(z, 172); @@ -722,35 +725,35 @@ mod tests { fn bit_shift_left() { let mut x = BigInt::from(4); //100 let y = BigInt::from(2); - let z = x << y; //10000 + let z = x.clone() << y.clone(); //10000 x <<= y; assert_eq!(x, z); assert_eq!(z, 16); let mut x = BigInt::from(10); //1010 let y = BigInt::from(1); - let z = x << y; //10100 + let z = x.clone() << y.clone(); //10100 x <<= y; assert_eq!(x, z); assert_eq!(z, 20); let mut x = BigInt::from(172); //10101100 let y = BigInt::from(0); - let z = x << y; + let z = x.clone() << y.clone(); x <<= y; assert_eq!(x, z); assert_eq!(z, 172); let mut x = BigInt::from(172); //10101100 let y = BigInt::from(10); - let z = x << y; //101011000000000000 + let z = x.clone() << y.clone(); //101011000000000000 x <<= y; assert_eq!(x, z); assert_eq!(z, 176128); let mut x = BigInt::from(173); //10101101 let y = BigInt::from(1); - let z = x << y; //101011010 + let z = x.clone() << y.clone(); //101011010 x <<= y; assert_eq!(x, z); assert_eq!(z, 346); @@ -760,35 +763,35 @@ mod tests { fn bit_shift_right() { let mut x = BigInt::from(4); //100 let y = BigInt::from(2); - let z = x >> y; //1 + let z = x.clone() >> y.clone(); //1 x >>= y; assert_eq!(x, z); assert_eq!(z, 1); let mut x = BigInt::from(10); //1010 let y = BigInt::from(1); - let z = x >> y; //101 + let z = x.clone() >> y.clone(); //101 x >>= y; assert_eq!(x, z); assert_eq!(z, 5); let mut x = BigInt::from(172); //10101100 let y = BigInt::from(0); - let z = x >> y; //10101100 + let z = x.clone() >> y.clone(); //10101100 x >>= y; assert_eq!(x, z); assert_eq!(z, 172); let mut x = BigInt::from(172); //10101100 let y = BigInt::from(10); - let z = x >> y; //0 + let z = x.clone() >> y.clone(); //0 x >>= y; assert_eq!(x, z); assert_eq!(z, 0); let mut x = BigInt::from(173); //10101101 let y = BigInt::from(1); - let z = x >> y; //1010110 + let z = x.clone() >> y.clone(); //1010110 x >>= y; assert_eq!(x, z); assert_eq!(z, 86); @@ -804,7 +807,7 @@ mod tests { assert_eq!(format!("{x:#X}"), "0x10"); let x = BigInt::from(172); assert_eq!(format!("{x:X}"), "AC"); - let x = BigInt::from(17220003931); + let x = BigInt::from(17220003931u128); assert_eq!(format!("{x:#X}"), "0x40264685B"); } @@ -817,9 +820,9 @@ mod tests { assert_eq!(a, 30); a *= BigInt::from(5); assert_eq!(a, 150); - b = a + BigInt::from(3); + b = a.clone() + BigInt::from(3); assert_eq!(b, 153); - b = a * BigInt::from(7); + b = a.clone() * BigInt::from(7); assert_eq!(b, 1050); assert_eq!(a, 150); assert_eq!(format!("{a:X}"), "96"); @@ -829,29 +832,29 @@ mod tests { assert_eq!(a, -10); a *= BigInt::from(5); assert_eq!(a, -50); - b = a + BigInt::from(73); + b = a.clone() + BigInt::from(73); assert_eq!(b, 23); - b = a * BigInt::from(-7); + b = a.clone() * BigInt::from(-7); assert_eq!(b, 350); assert_eq!(a, -50); - assert_eq!(format!("{a:X}"), "-32") + assert_eq!(format!("{a:X}"), "-32"); - a = BigInt::from(12345678901234567890); - a += BigInt::from(-99999999999999999999); + a = BigInt::from(12345678901234567890i128); + a += BigInt::from(-99999999999999999999i128); assert_eq!(a, -87654321098765432109); - a *= BigInt::from(54321987654321987654); - assert_eq!(a,-4761556948575111126880627366067073182286); + a *= BigInt::from(54321987654321987654i128); + assert_eq!(a, "-4761556948575111126880627366067073182286"); a *= BigInt::from(0); assert_eq!(a, 0); a = BigInt::from(10); - b = a + BigInt::from_str("400"); + b = a.clone() + 400; assert_eq!(b, "410"); - b = a * BigInt::from_str("15"); + b = a.clone() * BigInt::from_str("15").unwrap_or_default(); assert_eq!(b, "150"); assert_eq!(a, "10"); assert_eq!(format!("{a:X}"), "A"); - b = BigInt::from_str("1234"); + b = BigInt::from_str("1234").unwrap_or_default(); assert_eq!(format!("{b}"), "1234"); assert!(BigInt::from_str(" 12 34").is_err()); assert!(BigInt::from_str("999z").is_err()); @@ -863,15 +866,15 @@ mod tests { a = BigInt::from(42); assert_eq!(a, 42); - a = BigInt::from(73786976294838206464); + a = BigInt::from(73786976294838206464i128); assert_eq!(a, 73786976294838206464); assert_eq!(format!("{a:X}"), "40000000000000000"); - assert!(a < 1361129467683753853853498429727072845824); - assert!(a <= 1361129467683753853853498429727072845824); - assert!(!(a > 1361129467683753853853498429727072845824)); - assert!(!(a >= 1361129467683753853853498429727072845824)); - assert!(!(a == 1361129467683753853853498429727072845824)); - assert!(a != 1361129467683753853853498429727072845824); + assert!(a < "1361129467683753853853498429727072845824"); + assert!(a <= "1361129467683753853853498429727072845824"); + assert!(!(a > "1361129467683753853853498429727072845824")); + assert!(!(a >= "1361129467683753853853498429727072845824")); + assert!(!(a == "1361129467683753853853498429727072845824")); + assert!(a != "1361129467683753853853498429727072845824"); assert!(!(a < 73786976294838206464)); assert!(a <= 73786976294838206464); assert!(!(a > 73786976294838206464)); @@ -884,21 +887,20 @@ mod tests { assert!(!(a >= 73786976294838206465)); assert!(!(a == 73786976294838206465)); assert!(a != 73786976294838206465); - a = BigInt::from_str("2147483648"); + a = BigInt::from_str("2147483648").unwrap_or(BigInt::new()); assert!(!(a < -2147483648)); assert!(!(a <= -2147483648)); assert!(a > -2147483648); assert!(a >= -2147483648); assert!(!(a == -2147483648)); assert!(a != -2147483648); - a = BigInt::from_str("-12345678"); + a = BigInt::from_str("-12345678").unwrap_or(BigInt::new()); assert!(!(a < -87654321)); assert!(!(a <= -87654321)); assert!(a > -87654321); assert!(a >= -87654321); assert!(!(a == -87654321)); assert!(a != -87654321); - } } From 2cb26b59d8f1208ac217b0e49d168e5c42d0f085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Fri, 11 Jul 2025 23:25:11 +0200 Subject: [PATCH 045/243] add use super::* instead of another "use" formulas --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index ca60460..10db17b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,7 @@ impl BigInt { #[cfg(test)] mod tests { - use crate::BigInt; + use super::*; #[test] fn default() { From ccc7f2e14863e17aa69742040c0fd4b5595e4d06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sat, 12 Jul 2025 22:03:46 +0200 Subject: [PATCH 046/243] change of Rust version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index a43cb48..795dd31 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "BigInt" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] From 71236fbf77d686859c9c3950b639435445e9de9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 13 Jul 2025 05:55:34 +0200 Subject: [PATCH 047/243] add tests for lower_hex --- src/main.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main.rs b/src/main.rs index 10db17b..dfbc9cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -809,6 +809,17 @@ mod tests { assert_eq!(format!("{x:X}"), "AC"); let x = BigInt::from(17220003931u128); assert_eq!(format!("{x:#X}"), "0x40264685B"); + + let x = BigInt::from(4); + assert_eq!(format!("{x:x}"), "4"); + let x = BigInt::from(16); + assert_eq!(format!("{x:#x}"), "0xf"); + let x = BigInt::from(10); + assert_eq!(format!("{x:#x}"), "0x10"); + let x = BigInt::from(172); + assert_eq!(format!("{x:x}"), "ac"); + let x = BigInt::from(17220003931u128); + assert_eq!(format!("{x:#x}"), "0x40264685b"); } #[test] From 0af2c599f24d200cd85ab9318d5d731db12c89e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 13 Jul 2025 05:55:53 +0200 Subject: [PATCH 048/243] add dependencies --- Cargo.lock | 20 +++++++++++++++++++- Cargo.toml | 1 + src/main.rs | 13 +++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index b0d4221..d0c01de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,7 +1,25 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "BigInt" version = "0.1.0" +dependencies = [ + "num-traits", +] + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] diff --git a/Cargo.toml b/Cargo.toml index 795dd31..b253c26 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2024" [dependencies] +num-traits = { version = "0.2.19", features = ["i128"] } diff --git a/src/main.rs b/src/main.rs index dfbc9cc..61ff93d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,17 @@ #[derive(Clone)] +use num_traits::PrimInt; +use std::{ + cmp::Ordering, + ffi::OsString, + fmt::{self, Binary, Display, LowerHex, UpperHex}, + ops::{ + Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, + DivAssign, Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, + SubAssign, + }, + str::FromStr, + string, +}; struct BigInt { positive: bool, numbers: Vec, From d2319e5398e11af8e363506d21fc4c5e346b9fc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 13 Jul 2025 05:56:09 +0200 Subject: [PATCH 049/243] move of #derive --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 61ff93d..de40399 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -#[derive(Clone)] use num_traits::PrimInt; use std::{ cmp::Ordering, @@ -12,6 +11,7 @@ use std::{ str::FromStr, string, }; +#[derive(Clone, Eq)] struct BigInt { positive: bool, numbers: Vec, From 3eb5acab33bd873342f0fccbb54c41a579e34bdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 13 Jul 2025 05:56:35 +0200 Subject: [PATCH 050/243] minor formatting changes --- src/main.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index de40399..1370e5b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,7 +62,10 @@ mod tests { assert_eq!(x.positive, false); assert_eq!( x.numbers, - [3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0].to_vec() + [ + 3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 + ] + .to_vec() ); } @@ -81,7 +84,10 @@ mod tests { assert_eq!(x.positive, false); assert_eq!( x.numbers, - [3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0].to_vec() + [ + 3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 + ] + .to_vec() ); let x = BigInt::from_str("-0").unwrap(); assert_eq!(x.positive, true); From 98a8fb7c42f183b22d4bd2e2d595e9db76b85655 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 13 Jul 2025 05:58:58 +0200 Subject: [PATCH 051/243] add declarations of traits --- src/main.rs | 342 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 342 insertions(+) diff --git a/src/main.rs b/src/main.rs index 1370e5b..5e065ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,12 @@ use std::{ str::FromStr, string, }; + +#[derive(Debug)] +enum BigIntError { + NaN, +} + #[derive(Clone, Eq)] struct BigInt { positive: bool, @@ -26,10 +32,346 @@ impl Default for BigInt { } } +impl FromStr for BigInt { + type Err = BigIntError; + fn from_str(s: &str) -> Result { + todo!() + } +} + +impl From for BigInt +where + T: PrimInt, +{ + fn from(value: T) -> Self { + todo!() + } +} + +impl Display for BigInt { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + todo!() + } +} + +impl fmt::Debug for BigInt { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + todo!() + } +} + +impl Binary for BigInt { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + todo!() + } +} + +impl UpperHex for BigInt { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + todo!() + } +} + +impl LowerHex for BigInt { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + todo!() + } +} + +impl PartialEq for BigInt { + fn eq(&self, other: &i128) -> bool { + todo!() + } + fn ne(&self, other: &i128) -> bool { + todo!() + } +} + +impl PartialEq<&str> for BigInt { + fn eq(&self, other: &&str) -> bool { + todo!() + } + fn ne(&self, other: &&str) -> bool { + todo!() + } +} + +impl PartialEq for BigInt { + fn eq(&self, other: &BigInt) -> bool { + todo!() + } + fn ne(&self, other: &Self) -> bool { + todo!() + } +} + +impl Neg for BigInt { + type Output = Self; + fn neg(self) -> Self::Output { + todo!() + } +} + +impl Not for BigInt { + type Output = Self; + fn not(self) -> Self::Output { + todo!() + } +} + +impl PartialOrd for BigInt { + fn ge(&self, other: &Self) -> bool { + todo!() + } + fn gt(&self, other: &Self) -> bool { + todo!() + } + fn le(&self, other: &Self) -> bool { + todo!() + } + fn lt(&self, other: &Self) -> bool { + todo!() + } + fn partial_cmp(&self, other: &Self) -> Option { + todo!() + } +} + +impl PartialOrd for BigInt { + fn ge(&self, other: &i128) -> bool { + todo!() + } + fn gt(&self, other: &i128) -> bool { + todo!() + } + fn le(&self, other: &i128) -> bool { + todo!() + } + fn lt(&self, other: &i128) -> bool { + todo!() + } + fn partial_cmp(&self, other: &i128) -> Option { + todo!() + } +} + +impl PartialOrd<&str> for BigInt { + fn ge(&self, other: &&str) -> bool { + todo!() + } + fn gt(&self, other: &&str) -> bool { + todo!() + } + fn le(&self, other: &&str) -> bool { + todo!() + } + fn lt(&self, other: &&str) -> bool { + todo!() + } + fn partial_cmp(&self, other: &&str) -> Option { + todo!() + } +} + +impl Add for BigInt +where + T: Into, +{ + type Output = Self; + fn add(self, rhs: T) -> Self::Output { + todo!() + } +} + +impl AddAssign for BigInt +where + T: Into, +{ + fn add_assign(&mut self, rhs: T) { + todo!() + } +} + +impl Sub for BigInt +where + T: Into, +{ + type Output = Self; + + fn sub(self, rhs: T) -> Self::Output { + todo!() + } +} + +impl SubAssign for BigInt +where + T: Into, +{ + fn sub_assign(&mut self, rhs: T) { + todo!() + } +} + +impl Mul for BigInt +where + T: Into, +{ + type Output = Self; + fn mul(self, rhs: T) -> Self::Output { + todo!() + } +} + +impl MulAssign for BigInt +where + T: Into, +{ + fn mul_assign(&mut self, rhs: T) { + todo!() + } +} + +impl Div for BigInt +where + T: Into, +{ + type Output = Self; + fn div(self, rhs: T) -> Self::Output { + todo!() + } +} + +impl DivAssign for BigInt +where + T: Into, +{ + fn div_assign(&mut self, rhs: T) { + todo!() + } +} + +impl Rem for BigInt +where + T: Into, +{ + type Output = Self; + fn rem(self, rhs: T) -> Self::Output { + todo!() + } +} + +impl RemAssign for BigInt +where + T: Into, +{ + fn rem_assign(&mut self, rhs: T) { + todo!() + } +} + +impl BitAnd for BigInt +where + T: Into, +{ + type Output = Self; + fn bitand(self, rhs: T) -> Self::Output { + todo!() + } +} + +impl BitAndAssign for BigInt +where + T: Into, +{ + fn bitand_assign(&mut self, rhs: T) { + todo!() + } +} + +impl BitOr for BigInt +where + T: Into, +{ + type Output = Self; + fn bitor(self, rhs: T) -> Self::Output { + todo!() + } +} + +impl BitOrAssign for BigInt +where + T: Into, +{ + fn bitor_assign(&mut self, rhs: T) { + todo!() + } +} + +impl BitXor for BigInt +where + T: Into, +{ + type Output = Self; + fn bitxor(self, rhs: T) -> Self::Output { + todo!() + } +} + +impl BitXorAssign for BigInt +where + T: Into, +{ + fn bitxor_assign(&mut self, rhs: T) { + todo!() + } +} + +impl Shl for BigInt +where + T: Into, +{ + type Output = Self; + fn shl(self, rhs: T) -> Self::Output { + todo!() + } +} + +impl ShlAssign for BigInt +where + T: Into, +{ + fn shl_assign(&mut self, rhs: T) { + todo!() + } +} + +impl Shr for BigInt +where + T: Into, +{ + type Output = Self; + fn shr(self, rhs: T) -> Self::Output { + todo!() + } +} + +impl ShrAssign for BigInt +where + T: Into, +{ + fn shr_assign(&mut self, rhs: T) { + todo!() + } +} + impl BigInt { fn new() -> BigInt { BigInt::default() } + + fn to_words(&self) -> String { + todo!() + } } #[cfg(test)] From 66cb2a205a0753060e7db22975d461430a919c33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 13 Jul 2025 06:06:10 +0200 Subject: [PATCH 052/243] minor changes to silence warnings --- src/main.rs | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5e065ae..d23b89b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -100,7 +100,7 @@ impl PartialEq for BigInt { fn eq(&self, other: &BigInt) -> bool { todo!() } - fn ne(&self, other: &Self) -> bool { + fn ne(&self, other: &BigInt) -> bool { todo!() } } @@ -662,35 +662,35 @@ mod tests { let mut x = BigInt::from(10); let y = BigInt::from(-10); let z = x.clone() + y.clone(); - x += y; + x += y.clone(); assert_eq!(z, 0); assert_ne!(x, y); let mut x = BigInt::from(11); let y = BigInt::from(10); let z = x.clone() + y.clone(); - x += y; + x += y.clone(); assert_eq!(z, 21); assert_ne!(x, y); let mut x = BigInt::from(-0); let y = BigInt::from(0); let z = x.clone() + y.clone(); - x += y; + x += y.clone(); assert_eq!(z, 0); assert_eq!(x, y); let mut x = BigInt::from(6); let y = BigInt::from(4); let z = x.clone() + y.clone(); - x += y; + x += y.clone(); assert_eq!(z, 10); assert_eq!(x, y); let mut x = BigInt::from(-15); let y = BigInt::from(-4); let z = x.clone() + y.clone(); - x += y; + x += y.clone(); assert_eq!(z, -19); assert_eq!(x, y); } @@ -721,42 +721,42 @@ mod tests { let mut x = BigInt::from(10); let y = BigInt::from(-10); let z = x.clone() - y.clone(); - x -= y; + x -= y.clone(); assert_eq!(z, 20); assert_ne!(x, y); let mut x = BigInt::from(-10); let y = BigInt::from(10); let z = x.clone() - y.clone(); - x -= y; + x -= y.clone(); assert_eq!(z, -20); assert_ne!(x, y); let mut x = BigInt::from(11); let y = BigInt::from(10); let z = x.clone() - y.clone(); - x -= y; + x -= y.clone(); assert_eq!(z, 1); assert_ne!(x, y); let mut x = BigInt::from(-0); let y = BigInt::from(0); let z = x.clone() - y.clone(); - x -= y; + x -= y.clone(); assert_eq!(z, 0); assert_eq!(x, y); let mut x = BigInt::from(6); let y = BigInt::from(4); let z = x.clone() - y.clone(); - x -= y; + x -= y.clone(); assert_eq!(z, 2); assert_eq!(x, y); let mut x = BigInt::from(-15); let y = BigInt::from(-4); let z = x.clone() - y.clone(); - x -= y; + x -= y.clone(); assert_eq!(z, -11); assert_eq!(x, y); } @@ -794,35 +794,35 @@ mod tests { let mut x = BigInt::from(10); let y = BigInt::from(-10); let z: BigInt = x.clone() * y.clone(); - x *= y; + x *= y.clone(); assert_eq!(z, -100); assert_ne!(x, y); let mut x = BigInt::from(11); let y = BigInt::from(10); let z: BigInt = x.clone() * y.clone(); - x *= y; + x *= y.clone(); assert_eq!(z, 110); assert_ne!(x, y); let mut x = BigInt::from(-0); let y = BigInt::from(0); let z: BigInt = x.clone() * y.clone(); - x *= y; + x *= y.clone(); assert_eq!(z, 0); assert_eq!(x, y); let mut x = BigInt::from(6); let y = BigInt::from(4); let z: BigInt = x.clone() * y.clone(); - x *= y; + x *= y.clone(); assert_eq!(z, 24); assert_eq!(x, y); let mut x = BigInt::from(-15); let y = BigInt::from(-4); let z: BigInt = x.clone() * y.clone(); - x *= y; + x *= y.clone(); assert_eq!(z, 60); assert_eq!(x, y); } @@ -874,28 +874,28 @@ mod tests { let mut x = BigInt::from(10); let y = BigInt::from(-10); let z = x.clone() / y.clone(); - x /= y; + x /= y.clone(); assert_eq!(z, -1); assert_ne!(x, y); let mut x = BigInt::from(110); let y = BigInt::from(10); let z = x.clone() / y.clone(); - x /= y; + x /= y.clone(); assert_eq!(z, 11); assert_ne!(x, y); let mut x = BigInt::from(6); let y = BigInt::from(2); let z = x.clone() / y.clone(); - x /= y; + x /= y.clone(); assert_eq!(z, 3); assert_eq!(x, y); let mut x = BigInt::from(-15); let y = BigInt::from(-5); let z = x.clone() / y.clone(); - x /= y; + x /= y.clone(); assert_eq!(z, 3); assert_eq!(x, y); } From e80a26cce1c64d5431ffed374a6722691d710efc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 13 Jul 2025 11:12:25 +0200 Subject: [PATCH 053/243] remove unused imports --- src/main.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index d23b89b..edc5b98 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ use num_traits::PrimInt; use std::{ cmp::Ordering, - ffi::OsString, fmt::{self, Binary, Display, LowerHex, UpperHex}, ops::{ Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, @@ -9,7 +8,6 @@ use std::{ SubAssign, }, str::FromStr, - string, }; #[derive(Debug)] From 4eae729ecf62444e5cafb00c63c2ae9c6bb0f125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 13 Jul 2025 12:49:16 +0200 Subject: [PATCH 054/243] add implementation of from --- src/main.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index edc5b98..1dda73b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use std::{ SubAssign, }, str::FromStr, + u8, }; #[derive(Debug)] @@ -42,7 +43,28 @@ where T: PrimInt, { fn from(value: T) -> Self { - todo!() + if value.is_zero() { + return BigInt::default(); + } + + let mut val = value; + let mut ret_vec = Vec::new(); + let mut positive = true; + + if val < T::zero() { + positive = false; + val = val * T::from(-1).unwrap(); + } + + while !val.is_zero() { + ret_vec.insert(0, (val % T::from(10).unwrap()).to_u8().unwrap()); + val = val / T::from(10).unwrap(); + } + + BigInt { + positive: positive, + numbers: ret_vec, + } } } From 8152c430779d561bfaac25d8477b832fbc0b72ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 13 Jul 2025 13:02:23 +0200 Subject: [PATCH 055/243] change of naming for better consistency --- src/main.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1dda73b..495e097 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,22 +48,22 @@ where } let mut val = value; - let mut ret_vec = Vec::new(); - let mut positive = true; + let mut fin_vec = Vec::new(); + let mut fin_pos = true; if val < T::zero() { - positive = false; + fin_pos = false; val = val * T::from(-1).unwrap(); } while !val.is_zero() { - ret_vec.insert(0, (val % T::from(10).unwrap()).to_u8().unwrap()); + fin_vec.insert(0, (val % T::from(10).unwrap()).to_u8().unwrap()); val = val / T::from(10).unwrap(); } BigInt { - positive: positive, - numbers: ret_vec, + positive: fin_pos, + numbers: fin_vec, } } } From 63c3f5dc8413c08d9839cb0b8443d908a88e3934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 13 Jul 2025 18:38:32 +0200 Subject: [PATCH 056/243] add comments into From trait --- src/main.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main.rs b/src/main.rs index 495e097..61c8e91 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,6 +43,7 @@ where T: PrimInt, { fn from(value: T) -> Self { + //zero edgecase if value.is_zero() { return BigInt::default(); } @@ -51,16 +52,19 @@ where let mut fin_vec = Vec::new(); let mut fin_pos = true; + //negative number if val < T::zero() { fin_pos = false; val = val * T::from(-1).unwrap(); } + //transformation of digits while !val.is_zero() { fin_vec.insert(0, (val % T::from(10).unwrap()).to_u8().unwrap()); val = val / T::from(10).unwrap(); } + //return value BigInt { positive: fin_pos, numbers: fin_vec, From 56c6c6eaa74c8366889230bd6b0c838bb47dc819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 13 Jul 2025 18:38:40 +0200 Subject: [PATCH 057/243] more tests --- src/main.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main.rs b/src/main.rs index 61c8e91..c6d3380 100644 --- a/src/main.rs +++ b/src/main.rs @@ -433,6 +433,12 @@ mod tests { ] .to_vec() ); + let x = BigInt::from(0); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [0].to_vec()); + let x = BigInt::from(-0); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [0].to_vec()); } #[test] @@ -458,6 +464,14 @@ mod tests { let x = BigInt::from_str("-0").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [0].to_vec()); + let x = BigInt::from_str("0sw"); + assert!(x.is_err()); + let x = BigInt::from_str("0 2020000"); + assert!(x.is_err()); + let x = BigInt::from_str("--200"); + assert!(x.is_err()); + let x = BigInt::from_str("+2000303"); + assert!(x.is_err()); } #[test] From be85821edd0dc271603c980b80040b3456dd6997 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 13 Jul 2025 18:38:54 +0200 Subject: [PATCH 058/243] change of use section --- src/main.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index c6d3380..7816062 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ -use num_traits::PrimInt; +use num_traits::{PrimInt, ToPrimitive}; use std::{ + char, cmp::Ordering, fmt::{self, Binary, Display, LowerHex, UpperHex}, ops::{ @@ -8,7 +9,8 @@ use std::{ SubAssign, }, str::FromStr, - u8, + sync::Arc, + u8, vec, }; #[derive(Debug)] From 2d0eca82300a58d0788d5163755ce0282df3ec0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 13 Jul 2025 18:47:20 +0200 Subject: [PATCH 059/243] remove tests for number from string --- src/main.rs | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7816062..d3c29d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -476,31 +476,6 @@ mod tests { assert!(x.is_err()); } - #[test] - fn from_string_words_from_str() { - let x = BigInt::from_str("twenty").unwrap(); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [2, 0].to_vec()); - let x = BigInt::from_str("minus twenty four").unwrap(); - assert_eq!(x.positive, false); - assert_eq!(x.numbers, [2, 4].to_vec()); - let x = BigInt::from_str("two milion five hundred fifty thousand twenty one").unwrap(); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [2, 5, 5, 0, 0, 2, 1].to_vec()); - let x = BigInt::from_str("minus two milion one").unwrap(); - assert_eq!(x.positive, false); - assert_eq!(x.numbers, [2, 0, 0, 0, 0, 0, 1].to_vec()); - let x = BigInt::from_str("zero").unwrap(); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [0].to_vec()); - let x = BigInt::from_str("onse"); - assert!(x.is_err()); - let x = BigInt::from_str("twenty thousand thousand"); - assert!(x.is_err()); - let x = BigInt::from_str("twenty thousand hundred"); - assert!(x.is_err()); - } - #[test] fn from_string_words_from_str_digits() { let x = BigInt::from_str("two zero").unwrap(); From 58f1a30b9553a93770924e92af47117334031cab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 13 Jul 2025 18:58:38 +0200 Subject: [PATCH 060/243] more tests --- src/main.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index d3c29d3..ced6d2e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -474,6 +474,8 @@ mod tests { assert!(x.is_err()); let x = BigInt::from_str("+2000303"); assert!(x.is_err()); + let x = BigInt::from_str("minus20003002"); + assert!(x.is_err()); } #[test] @@ -497,8 +499,11 @@ mod tests { assert!(x.is_err()); let x = BigInt::from_str("twenty thousand thousand"); assert!(x.is_err()); - let x = BigInt::from_str("twenty thousand hundred"); + let x: Result = BigInt::from_str("twenty thousand hundred"); assert!(x.is_err()); + let x = BigInt::from_str("- five four").unwrap(); + assert_eq!(x.positive, false); + assert_eq!(x.numbers, [5, 4].to_vec()); } #[test] From 24633b30a544281db653b0bb98430e6c3abdb422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 13 Jul 2025 19:41:11 +0200 Subject: [PATCH 061/243] change app -> lib --- src/{main.rs => lib.rs} | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) rename src/{main.rs => lib.rs} (99%) diff --git a/src/main.rs b/src/lib.rs similarity index 99% rename from src/main.rs rename to src/lib.rs index ced6d2e..36b1283 100644 --- a/src/main.rs +++ b/src/lib.rs @@ -35,7 +35,7 @@ impl Default for BigInt { impl FromStr for BigInt { type Err = BigIntError; - fn from_str(s: &str) -> Result { + fn from_str(mut num_str: &str) -> Result { todo!() } } @@ -391,13 +391,17 @@ where } impl BigInt { - fn new() -> BigInt { + pub fn new() -> BigInt { BigInt::default() } - fn to_words(&self) -> String { + pub fn to_words(&self) -> String { todo!() } + + fn parseWordDigits(mut num_str: &str) -> Result { + Ok(BigInt::new()) + } } #[cfg(test)] @@ -1295,5 +1299,3 @@ mod tests { assert!(a != -87654321); } } - -fn main() {} From 7a190fa0bde4af3f2805746938e8a204a67f5fd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 13 Jul 2025 22:53:48 +0200 Subject: [PATCH 062/243] add implementation of from_str --- src/lib.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 83 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 36b1283..c32096d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ use num_traits::{PrimInt, ToPrimitive}; use std::{ char, cmp::Ordering, - fmt::{self, Binary, Display, LowerHex, UpperHex}, + fmt::{self, Binary, Display, LowerHex, UpperHex, format}, ops::{ Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign, Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, @@ -33,10 +33,91 @@ impl Default for BigInt { } } +fn word_to_number(word: &str) -> Result { + match word { + "zero" => Ok(0), + "one" => Ok(1), + "two" => Ok(2), + "three" => Ok(3), + "four" => Ok(4), + "five" => Ok(5), + "six" => Ok(6), + "seven" => Ok(7), + "eight" => Ok(8), + "nine" => Ok(9), + _ => Err(BigIntError::NaN), + } +} + +fn parse_word_digits(num_str: String) -> Result { + //create lowercase iterator + let mut parsed = num_str.split_whitespace().map(str::to_lowercase).peekable(); + let mut fin_pos = true; + let mut fin_vec: Vec = Vec::new(); + + //if empty string + if parsed.peek().is_none() { + return Err(BigIntError::NaN); + } + + //positive/negative + if let Some("-" | "minus") = parsed.peek().map(String::as_str) { + fin_pos = false; + parsed.next(); + } + + //loop for translating words to u8 + for word in parsed { + fin_vec.push(word_to_number(&word)?); + } + + //additional check + if fin_vec.is_empty() { + return Err(BigIntError::NaN); + } + + Ok(BigInt { + positive: fin_pos, + numbers: fin_vec, + }) +} + impl FromStr for BigInt { type Err = BigIntError; fn from_str(mut num_str: &str) -> Result { - todo!() + //empty string edgecaase + if num_str.is_empty() { + return Err(BigIntError::NaN); + } + + let mut fin_pos = !(num_str.starts_with('-')); + let mut fin_vec: Vec = Vec::new(); + + //if negative - remove '-' + if !fin_pos { + num_str = num_str.split_at(1).1; + } + + for char in num_str.chars() { + if !char.is_ascii_digit() { + return parse_word_digits(if fin_pos { + num_str.to_string() + } else { + format!("-{}", num_str) + }); + } + + fin_vec.push(char.to_digit(10).unwrap().to_u8().unwrap()); + } + + if fin_vec == vec![0] { + fin_pos = true; + } + + Ok(BigInt { + positive: fin_pos, + numbers: fin_vec, + }) } } @@ -398,10 +479,6 @@ impl BigInt { pub fn to_words(&self) -> String { todo!() } - - fn parseWordDigits(mut num_str: &str) -> Result { - Ok(BigInt::new()) - } } #[cfg(test)] From aef03970c2c67ca3a95c17f719a027335743883f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 13 Jul 2025 22:53:55 +0200 Subject: [PATCH 063/243] more tests --- src/lib.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c32096d..97bbc93 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -561,7 +561,7 @@ mod tests { #[test] fn from_string_words_from_str_digits() { - let x = BigInt::from_str("two zero").unwrap(); + let x = BigInt::from_str("two zero ").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [2, 0].to_vec()); let x = BigInt::from_str("minus two four").unwrap(); @@ -570,7 +570,7 @@ mod tests { let x = BigInt::from_str("two five five zero zero two one").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [2, 5, 5, 0, 0, 2, 1].to_vec()); - let x = BigInt::from_str("minus two zero zero zero zero zero one").unwrap(); + let x = BigInt::from_str("minus two zero zero zero zero zero one").unwrap(); assert_eq!(x.positive, false); assert_eq!(x.numbers, [2, 0, 0, 0, 0, 0, 1].to_vec()); let x = BigInt::from_str("zero").unwrap(); @@ -578,6 +578,8 @@ mod tests { assert_eq!(x.numbers, [0].to_vec()); let x = BigInt::from_str("onse"); assert!(x.is_err()); + let x = BigInt::from_str(" "); + assert!(x.is_err()); let x = BigInt::from_str("twenty thousand thousand"); assert!(x.is_err()); let x: Result = BigInt::from_str("twenty thousand hundred"); From 207356e6ce14392b63be337aa6ef8c1f7ffbce27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 14 Jul 2025 00:13:06 +0200 Subject: [PATCH 064/243] add display trait --- src/lib.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 97bbc93..d93f876 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ use num_traits::{PrimInt, ToPrimitive}; use std::{ char, cmp::Ordering, + f32::DIGITS, fmt::{self, Binary, Display, LowerHex, UpperHex, format}, ops::{ Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, @@ -157,7 +158,14 @@ where impl Display for BigInt { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - todo!() + if !self.positive { + write!(f, "-")?; + } + + for digit in self.numbers.clone() { + write!(f, "{digit}")?; + } + Ok(()) } } From cd25a5fa6c213f4676627bf12d7b6077d95fe23c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 14 Jul 2025 00:13:22 +0200 Subject: [PATCH 065/243] one test fixed --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index d93f876..39306a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -606,7 +606,7 @@ mod tests { let x = BigInt::from(-100); assert_eq!(format!("Number is: {x}"), "Number is: -100"); let x = BigInt::from(-0); - assert_eq!(format!("Number is: {x}"), "Number is: -0"); + assert_eq!(format!("Number is: {x}"), "Number is: 0"); let x = BigInt::from(-1); assert_eq!(format!("Number is: {x}"), "Number is: -1"); let x = BigInt::from(320020000981234567890i128); From 1bb4fdfd6f0738d7a6bc050b30031cc33889a176 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 14 Jul 2025 00:15:12 +0200 Subject: [PATCH 066/243] another test fixed --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 39306a8..82ae8d4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -625,7 +625,7 @@ mod tests { let x = BigInt::from(-100); assert_eq!(format!("Number is: {x:?}"), "Number is: -100"); let x = BigInt::from(-0); - assert_eq!(format!("Number is: {x:?}"), "Number is: -0"); + assert_eq!(format!("Number is: {x:?}"), "Number is: +0"); let x = BigInt::from(-1); assert_eq!(format!("Number is: {x:?}"), "Number is: -1"); let x = BigInt::from(320020000981234567890i128); From b081e1cfb8902d7eec9e8b07321c809b5791c0e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 14 Jul 2025 00:15:35 +0200 Subject: [PATCH 067/243] implement of debug trait --- src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 82ae8d4..21d7dc8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -171,7 +171,11 @@ impl Display for BigInt { impl fmt::Debug for BigInt { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - todo!() + if self.positive { + write!(f, "+")?; + } + + write!(f, "{}", self) } } From be74d35b01621d065563efb7dcdf9a3f07396d8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 14 Jul 2025 00:19:42 +0200 Subject: [PATCH 068/243] add tests for not trait --- src/lib.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 21d7dc8..385a2d4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -673,6 +673,25 @@ mod tests { assert_eq!(x.to_string(), "22"); } + #[test] + fn not() { + let mut x = BigInt::from(1); + assert_eq!(x.positive, true); + assert_eq!(x.to_string(), "1"); + x = -x; + assert_eq!(x.positive, false); + assert_eq!(x.to_string(), "-1"); + x = -x; + assert_eq!(x.positive, true); + assert_eq!(x.to_string(), "1"); + let mut x = BigInt::from(-22); + assert_eq!(x.positive, false); + assert_eq!(x.to_string(), "-22"); + x = -x; + assert_eq!(x.positive, true); + assert_eq!(x.to_string(), "22"); + } + #[test] fn equal() { let x = BigInt::from(10); From 8dd2fc7143ab8a5a1d7e8237d0bed19081a16a55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 14 Jul 2025 00:19:57 +0200 Subject: [PATCH 069/243] add implementation of ! and - traits --- src/lib.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 385a2d4..2cd1e5b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -227,14 +227,15 @@ impl PartialEq for BigInt { impl Neg for BigInt { type Output = Self; fn neg(self) -> Self::Output { - todo!() + !self } } impl Not for BigInt { type Output = Self; - fn not(self) -> Self::Output { - todo!() + fn not(mut self) -> Self::Output { + self.positive = !self.positive; + self } } From 9b2005a34e302657824d9ab23f8913dfa8f275ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 14 Jul 2025 10:27:25 +0200 Subject: [PATCH 070/243] add implementation of to_words --- src/lib.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 2cd1e5b..23d45ed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +use core::num; use num_traits::{PrimInt, ToPrimitive}; use std::{ char, @@ -50,6 +51,23 @@ fn word_to_number(word: &str) -> Result { } } +fn number_to_word(nmr: u8) -> String { + match nmr { + 0 => "zero", + 1 => "one", + 2 => "two", + 3 => "three", + 4 => "four", + 5 => "five", + 6 => "six", + 7 => "seven", + 8 => "eight", + 9 => "nine", + _ => "zero", + } + .to_string() +} + fn parse_word_digits(num_str: String) -> Result { //create lowercase iterator let mut parsed = num_str.split_whitespace().map(str::to_lowercase).peekable(); @@ -490,7 +508,22 @@ impl BigInt { } pub fn to_words(&self) -> String { - todo!() + let mut fin_str = String::new(); + let mut nmr_iter = self.numbers.iter(); + + //print minus or first digit + if !self.positive { + fin_str = "minus".to_string(); + } else { + fin_str = number_to_word(*nmr_iter.next().unwrap_or(&0)); + } + + //print all digits + for num in nmr_iter { + fin_str = format!("{} {}", fin_str, number_to_word(*num)); + } + + fin_str } } From 4870a39c766537a19dbf3e3d66e0f218d27b5e1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 15 Jul 2025 00:19:00 +0200 Subject: [PATCH 071/243] more tests --- src/lib.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 23d45ed..1a34c26 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -748,6 +748,46 @@ mod tests { assert!(x == y); } + #[test] + fn equal_int() { + let x = BigInt::from(10); + assert!(x == 10); + let x = BigInt::from(101010); + assert!(x == 101010); + let x = BigInt::from(101010); + assert!(!(x == 101210)); + let x = BigInt::from(101010); + assert!(!(x == 1)); + let x = BigInt::from(0); + assert!(x == 0); + let x = BigInt::from(10); + assert!(x != -10); + let x = BigInt::from(11); + assert!(x != 10); + let x = BigInt::from(-0); + assert!(x == 0); + } + + #[test] + fn equal_str() { + let x = BigInt::from(10); + assert!(x == "10"); + let x = BigInt::from(101010); + assert!(x == "101010"); + let x = BigInt::from(101010); + assert!(!(x == "101210")); + let x = BigInt::from(101010); + assert!(!(x == "1")); + let x = BigInt::from(0); + assert!(x == "0"); + let x = BigInt::from(10); + assert!(x != "-10"); + let x = BigInt::from(11); + assert!(x != "10"); + let x = BigInt::from(-0); + assert!(x == "0"); + } + #[test] fn greater() { let x: BigInt = BigInt::from(15); From b83e2ef08840e9490e7f6e0f82bfeea34bb1b7b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 15 Jul 2025 00:19:06 +0200 Subject: [PATCH 072/243] more tests --- src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 1a34c26..fa33b47 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -734,6 +734,12 @@ mod tests { let x = BigInt::from(101010); let y = BigInt::from(101010); assert!(x == y); + let x = BigInt::from(101010); + let y = BigInt::from(101210); + assert!(!(x == y)); + let x = BigInt::from(101010); + let y = BigInt::from(1); + assert!(!(x == y)); let x = BigInt::from(0); let y = BigInt::from(0); assert!(x == y); From 854b63fb4a9cf69f8214825574e2e716112a4f3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 15 Jul 2025 00:19:49 +0200 Subject: [PATCH 073/243] remove useless ne trait --- src/lib.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fa33b47..6dc3563 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -219,7 +219,6 @@ impl PartialEq for BigInt { fn eq(&self, other: &i128) -> bool { todo!() } - fn ne(&self, other: &i128) -> bool { todo!() } } @@ -228,8 +227,6 @@ impl PartialEq<&str> for BigInt { fn eq(&self, other: &&str) -> bool { todo!() } - fn ne(&self, other: &&str) -> bool { - todo!() } } @@ -237,8 +234,6 @@ impl PartialEq for BigInt { fn eq(&self, other: &BigInt) -> bool { todo!() } - fn ne(&self, other: &BigInt) -> bool { - todo!() } } From 6ea477e2f236a70b3bcb0f6661a5d6ac8180cd80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 15 Jul 2025 00:47:46 +0200 Subject: [PATCH 074/243] From reimplemented with macros --- src/lib.rs | 99 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 69 insertions(+), 30 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6dc3563..de63636 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -140,40 +140,79 @@ impl FromStr for BigInt { } } -impl From for BigInt -where - T: PrimInt, -{ - fn from(value: T) -> Self { - //zero edgecase - if value.is_zero() { - return BigInt::default(); - } - - let mut val = value; - let mut fin_vec = Vec::new(); - let mut fin_pos = true; - - //negative number - if val < T::zero() { - fin_pos = false; - val = val * T::from(-1).unwrap(); - } - - //transformation of digits - while !val.is_zero() { - fin_vec.insert(0, (val % T::from(10).unwrap()).to_u8().unwrap()); - val = val / T::from(10).unwrap(); - } +macro_rules! from_int { + ($($t:ty),*)=>{ + $( + impl From<$t> for BigInt{ + fn from(value: $t) -> Self { + + //zero edgecase + if value == 0 { + return BigInt::default(); + } + + let mut val = value; + let mut fin_vec = Vec::new(); + let mut fin_pos = !(val < 0); + + //negative number + if val < 0 { + val = -val; + } + + //transformation of digits + while val != 0{ + fin_vec.insert(0, (val % 10).to_u8().unwrap()); + val = val / 10; + } + + //return value + BigInt { + positive: fin_pos, + numbers: fin_vec, + } + + } + } + )* + } +} - //return value - BigInt { - positive: fin_pos, - numbers: fin_vec, - } +macro_rules! from_uint { + ($($t:ty),*)=>{ + $( + impl From<$t> for BigInt{ + fn from(value: $t) -> Self { + + //zero edgecase + if value == 0 { + return BigInt::default(); + } + + let mut val = value; + let mut fin_vec = Vec::new(); + + //transformation of digits + while val != 0{ + fin_vec.insert(0, (val % 10).to_u8().unwrap()); + val = val / 10; + } + + //return value + BigInt { + positive: true, + numbers: fin_vec, + } + + } + } + )* } } +from_int!(i8, i16, i32, i64, i128); +from_uint!(u8, u16, u32, u64, u128); + impl Display for BigInt { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { if !self.positive { From 0c6222dc0e24fb9aa00f5d06a552b576bf6bf483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 15 Jul 2025 00:48:28 +0200 Subject: [PATCH 075/243] eq traits implementation --- src/lib.rs | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index de63636..3096d6f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -254,25 +254,37 @@ impl LowerHex for BigInt { } } -impl PartialEq for BigInt { - fn eq(&self, other: &i128) -> bool { - todo!() - } - todo!() - } +macro_rules! eq_with_int { + ($($t:ty),*) => { + $( + impl PartialEq<$t> for BigInt{ + fn eq(&self, other: &$t) -> bool { + self == &BigInt::from(*other) + } + } + )* + }; } +eq_with_int!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128); + impl PartialEq<&str> for BigInt { fn eq(&self, other: &&str) -> bool { - todo!() - } + let right_side = BigInt::from_str(other); + if right_side.is_err() { + return false; + } + self == &right_side.unwrap() } } impl PartialEq for BigInt { fn eq(&self, other: &BigInt) -> bool { - todo!() - } + if self.positive != other.positive { + false + } else { + self.numbers == other.numbers + } } } From e0d1a9d7a7678d23e243c39c07b89255890c111a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 15 Jul 2025 00:56:15 +0200 Subject: [PATCH 076/243] remove unused imports --- src/lib.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3096d6f..87198b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,18 +1,13 @@ -use core::num; -use num_traits::{PrimInt, ToPrimitive}; +use num_traits::ToPrimitive; use std::{ - char, cmp::Ordering, - f32::DIGITS, - fmt::{self, Binary, Display, LowerHex, UpperHex, format}, + fmt::{self, Binary, Display, LowerHex, UpperHex}, ops::{ Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign, Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign, }, str::FromStr, - sync::Arc, - u8, vec, }; #[derive(Debug)] From 60a95f4f9f84754e504800142d09e6e220a23724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 15 Jul 2025 00:56:33 +0200 Subject: [PATCH 077/243] renamed BigInt -> big_int --- src/lib.rs | 762 ++++++++++++++++++++++++++--------------------------- 1 file changed, 381 insertions(+), 381 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 87198b6..42bf996 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,14 +16,14 @@ enum BigIntError { } #[derive(Clone, Eq)] -struct BigInt { +struct big_int { positive: bool, numbers: Vec, } -impl Default for BigInt { +impl Default for big_int { fn default() -> Self { - BigInt { + big_int { positive: true, numbers: vec![0], } @@ -63,7 +63,7 @@ fn number_to_word(nmr: u8) -> String { .to_string() } -fn parse_word_digits(num_str: String) -> Result { +fn parse_word_digits(num_str: String) -> Result { //create lowercase iterator let mut parsed = num_str.split_whitespace().map(str::to_lowercase).peekable(); let mut fin_pos = true; @@ -90,13 +90,13 @@ fn parse_word_digits(num_str: String) -> Result { return Err(BigIntError::NaN); } - Ok(BigInt { + Ok(big_int { positive: fin_pos, numbers: fin_vec, }) } -impl FromStr for BigInt { +impl FromStr for big_int { type Err = BigIntError; fn from_str(mut num_str: &str) -> Result { //empty string edgecaase @@ -128,7 +128,7 @@ impl FromStr for BigInt { fin_pos = true; } - Ok(BigInt { + Ok(big_int { positive: fin_pos, numbers: fin_vec, }) @@ -138,12 +138,12 @@ impl FromStr for BigInt { macro_rules! from_int { ($($t:ty),*)=>{ $( - impl From<$t> for BigInt{ + impl From<$t> for big_int{ fn from(value: $t) -> Self { //zero edgecase if value == 0 { - return BigInt::default(); + return big_int::default(); } let mut val = value; @@ -162,7 +162,7 @@ macro_rules! from_int { } //return value - BigInt { + big_int { positive: fin_pos, numbers: fin_vec, } @@ -176,12 +176,12 @@ macro_rules! from_int { macro_rules! from_uint { ($($t:ty),*)=>{ $( - impl From<$t> for BigInt{ + impl From<$t> for big_int{ fn from(value: $t) -> Self { //zero edgecase if value == 0 { - return BigInt::default(); + return big_int::default(); } let mut val = value; @@ -194,7 +194,7 @@ macro_rules! from_uint { } //return value - BigInt { + big_int { positive: true, numbers: fin_vec, } @@ -208,7 +208,7 @@ macro_rules! from_uint { from_int!(i8, i16, i32, i64, i128); from_uint!(u8, u16, u32, u64, u128); -impl Display for BigInt { +impl Display for big_int { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { if !self.positive { write!(f, "-")?; @@ -221,7 +221,7 @@ impl Display for BigInt { } } -impl fmt::Debug for BigInt { +impl fmt::Debug for big_int { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if self.positive { write!(f, "+")?; @@ -231,19 +231,19 @@ impl fmt::Debug for BigInt { } } -impl Binary for BigInt { +impl Binary for big_int { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { todo!() } } -impl UpperHex for BigInt { +impl UpperHex for big_int { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { todo!() } } -impl LowerHex for BigInt { +impl LowerHex for big_int { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { todo!() } @@ -252,9 +252,9 @@ impl LowerHex for BigInt { macro_rules! eq_with_int { ($($t:ty),*) => { $( - impl PartialEq<$t> for BigInt{ + impl PartialEq<$t> for big_int{ fn eq(&self, other: &$t) -> bool { - self == &BigInt::from(*other) + self == &big_int::from(*other) } } )* @@ -263,9 +263,9 @@ macro_rules! eq_with_int { eq_with_int!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128); -impl PartialEq<&str> for BigInt { +impl PartialEq<&str> for big_int { fn eq(&self, other: &&str) -> bool { - let right_side = BigInt::from_str(other); + let right_side = big_int::from_str(other); if right_side.is_err() { return false; } @@ -273,8 +273,8 @@ impl PartialEq<&str> for BigInt { } } -impl PartialEq for BigInt { - fn eq(&self, other: &BigInt) -> bool { +impl PartialEq for big_int { + fn eq(&self, other: &big_int) -> bool { if self.positive != other.positive { false } else { @@ -283,14 +283,14 @@ impl PartialEq for BigInt { } } -impl Neg for BigInt { +impl Neg for big_int { type Output = Self; fn neg(self) -> Self::Output { !self } } -impl Not for BigInt { +impl Not for big_int { type Output = Self; fn not(mut self) -> Self::Output { self.positive = !self.positive; @@ -298,7 +298,7 @@ impl Not for BigInt { } } -impl PartialOrd for BigInt { +impl PartialOrd for big_int { fn ge(&self, other: &Self) -> bool { todo!() } @@ -316,7 +316,7 @@ impl PartialOrd for BigInt { } } -impl PartialOrd for BigInt { +impl PartialOrd for big_int { fn ge(&self, other: &i128) -> bool { todo!() } @@ -334,7 +334,7 @@ impl PartialOrd for BigInt { } } -impl PartialOrd<&str> for BigInt { +impl PartialOrd<&str> for big_int { fn ge(&self, other: &&str) -> bool { todo!() } @@ -352,9 +352,9 @@ impl PartialOrd<&str> for BigInt { } } -impl Add for BigInt +impl Add for big_int where - T: Into, + T: Into, { type Output = Self; fn add(self, rhs: T) -> Self::Output { @@ -362,18 +362,18 @@ where } } -impl AddAssign for BigInt +impl AddAssign for big_int where - T: Into, + T: Into, { fn add_assign(&mut self, rhs: T) { todo!() } } -impl Sub for BigInt +impl Sub for big_int where - T: Into, + T: Into, { type Output = Self; @@ -382,18 +382,18 @@ where } } -impl SubAssign for BigInt +impl SubAssign for big_int where - T: Into, + T: Into, { fn sub_assign(&mut self, rhs: T) { todo!() } } -impl Mul for BigInt +impl Mul for big_int where - T: Into, + T: Into, { type Output = Self; fn mul(self, rhs: T) -> Self::Output { @@ -401,18 +401,18 @@ where } } -impl MulAssign for BigInt +impl MulAssign for big_int where - T: Into, + T: Into, { fn mul_assign(&mut self, rhs: T) { todo!() } } -impl Div for BigInt +impl Div for big_int where - T: Into, + T: Into, { type Output = Self; fn div(self, rhs: T) -> Self::Output { @@ -420,18 +420,18 @@ where } } -impl DivAssign for BigInt +impl DivAssign for big_int where - T: Into, + T: Into, { fn div_assign(&mut self, rhs: T) { todo!() } } -impl Rem for BigInt +impl Rem for big_int where - T: Into, + T: Into, { type Output = Self; fn rem(self, rhs: T) -> Self::Output { @@ -439,18 +439,18 @@ where } } -impl RemAssign for BigInt +impl RemAssign for big_int where - T: Into, + T: Into, { fn rem_assign(&mut self, rhs: T) { todo!() } } -impl BitAnd for BigInt +impl BitAnd for big_int where - T: Into, + T: Into, { type Output = Self; fn bitand(self, rhs: T) -> Self::Output { @@ -458,18 +458,18 @@ where } } -impl BitAndAssign for BigInt +impl BitAndAssign for big_int where - T: Into, + T: Into, { fn bitand_assign(&mut self, rhs: T) { todo!() } } -impl BitOr for BigInt +impl BitOr for big_int where - T: Into, + T: Into, { type Output = Self; fn bitor(self, rhs: T) -> Self::Output { @@ -477,18 +477,18 @@ where } } -impl BitOrAssign for BigInt +impl BitOrAssign for big_int where - T: Into, + T: Into, { fn bitor_assign(&mut self, rhs: T) { todo!() } } -impl BitXor for BigInt +impl BitXor for big_int where - T: Into, + T: Into, { type Output = Self; fn bitxor(self, rhs: T) -> Self::Output { @@ -496,18 +496,18 @@ where } } -impl BitXorAssign for BigInt +impl BitXorAssign for big_int where - T: Into, + T: Into, { fn bitxor_assign(&mut self, rhs: T) { todo!() } } -impl Shl for BigInt +impl Shl for big_int where - T: Into, + T: Into, { type Output = Self; fn shl(self, rhs: T) -> Self::Output { @@ -515,18 +515,18 @@ where } } -impl ShlAssign for BigInt +impl ShlAssign for big_int where - T: Into, + T: Into, { fn shl_assign(&mut self, rhs: T) { todo!() } } -impl Shr for BigInt +impl Shr for big_int where - T: Into, + T: Into, { type Output = Self; fn shr(self, rhs: T) -> Self::Output { @@ -534,18 +534,18 @@ where } } -impl ShrAssign for BigInt +impl ShrAssign for big_int where - T: Into, + T: Into, { fn shr_assign(&mut self, rhs: T) { todo!() } } -impl BigInt { - pub fn new() -> BigInt { - BigInt::default() +impl big_int { + pub fn new() -> big_int { + big_int::default() } pub fn to_words(&self) -> String { @@ -574,27 +574,27 @@ mod tests { #[test] fn default() { - let def = BigInt::default(); + let def = big_int::default(); assert_eq!(def.positive, true); assert_eq!(def.numbers, [0].to_vec()); } #[test] fn new() { - let new = BigInt::new(); + let new = big_int::new(); assert_eq!(new.positive, true); assert_eq!(new.numbers, [0].to_vec()); } #[test] fn from() { - let x = BigInt::from(20); + let x = big_int::from(20); assert_eq!(x.positive, true); assert_eq!(x.numbers, [2, 0].to_vec()); - let x = BigInt::from(-20); + let x = big_int::from(-20); assert_eq!(x.positive, false); assert_eq!(x.numbers, [2, 0].to_vec()); - let x = BigInt::from(-320020000981234567890i128); + let x = big_int::from(-320020000981234567890i128); assert_eq!(x.positive, false); assert_eq!( x.numbers, @@ -603,26 +603,26 @@ mod tests { ] .to_vec() ); - let x = BigInt::from(0); + let x = big_int::from(0); assert_eq!(x.positive, true); assert_eq!(x.numbers, [0].to_vec()); - let x = BigInt::from(-0); + let x = big_int::from(-0); assert_eq!(x.positive, true); assert_eq!(x.numbers, [0].to_vec()); } #[test] fn from_string_numbers() { - let x = BigInt::from_str("20").unwrap(); + let x = big_int::from_str("20").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [2, 0].to_vec()); - let x = BigInt::from_str("666").unwrap(); + let x = big_int::from_str("666").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [6, 6, 6].to_vec()); - let x = BigInt::from_str("-20").unwrap(); + let x = big_int::from_str("-20").unwrap(); assert_eq!(x.positive, false); assert_eq!(x.numbers, [2, 0].to_vec()); - let x = BigInt::from_str("-320020000981234567890").unwrap(); + let x = big_int::from_str("-320020000981234567890").unwrap(); assert_eq!(x.positive, false); assert_eq!( x.numbers, @@ -631,64 +631,64 @@ mod tests { ] .to_vec() ); - let x = BigInt::from_str("-0").unwrap(); + let x = big_int::from_str("-0").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [0].to_vec()); - let x = BigInt::from_str("0sw"); + let x = big_int::from_str("0sw"); assert!(x.is_err()); - let x = BigInt::from_str("0 2020000"); + let x = big_int::from_str("0 2020000"); assert!(x.is_err()); - let x = BigInt::from_str("--200"); + let x = big_int::from_str("--200"); assert!(x.is_err()); - let x = BigInt::from_str("+2000303"); + let x = big_int::from_str("+2000303"); assert!(x.is_err()); - let x = BigInt::from_str("minus20003002"); + let x = big_int::from_str("minus20003002"); assert!(x.is_err()); } #[test] fn from_string_words_from_str_digits() { - let x = BigInt::from_str("two zero ").unwrap(); + let x = big_int::from_str("two zero ").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [2, 0].to_vec()); - let x = BigInt::from_str("minus two four").unwrap(); + let x = big_int::from_str("minus two four").unwrap(); assert_eq!(x.positive, false); assert_eq!(x.numbers, [2, 4].to_vec()); - let x = BigInt::from_str("two five five zero zero two one").unwrap(); + let x = big_int::from_str("two five five zero zero two one").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [2, 5, 5, 0, 0, 2, 1].to_vec()); - let x = BigInt::from_str("minus two zero zero zero zero zero one").unwrap(); + let x = big_int::from_str("minus two zero zero zero zero zero one").unwrap(); assert_eq!(x.positive, false); assert_eq!(x.numbers, [2, 0, 0, 0, 0, 0, 1].to_vec()); - let x = BigInt::from_str("zero").unwrap(); + let x = big_int::from_str("zero").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [0].to_vec()); - let x = BigInt::from_str("onse"); + let x = big_int::from_str("onse"); assert!(x.is_err()); - let x = BigInt::from_str(" "); + let x = big_int::from_str(" "); assert!(x.is_err()); - let x = BigInt::from_str("twenty thousand thousand"); + let x = big_int::from_str("twenty thousand thousand"); assert!(x.is_err()); - let x: Result = BigInt::from_str("twenty thousand hundred"); + let x: Result = big_int::from_str("twenty thousand hundred"); assert!(x.is_err()); - let x = BigInt::from_str("- five four").unwrap(); + let x = big_int::from_str("- five four").unwrap(); assert_eq!(x.positive, false); assert_eq!(x.numbers, [5, 4].to_vec()); } #[test] fn display() { - let x = BigInt::from(1003); + let x = big_int::from(1003); assert_eq!(format!("Number is: {x}"), "Number is: 1003"); - let x = BigInt::from(0); + let x = big_int::from(0); assert_eq!(format!("Number is: {x}"), "Number is: 0"); - let x = BigInt::from(-100); + let x = big_int::from(-100); assert_eq!(format!("Number is: {x}"), "Number is: -100"); - let x = BigInt::from(-0); + let x = big_int::from(-0); assert_eq!(format!("Number is: {x}"), "Number is: 0"); - let x = BigInt::from(-1); + let x = big_int::from(-1); assert_eq!(format!("Number is: {x}"), "Number is: -1"); - let x = BigInt::from(320020000981234567890i128); + let x = big_int::from(320020000981234567890i128); assert_eq!( format!("Number is: {x}"), "Number is: 320020000981234567890" @@ -697,17 +697,17 @@ mod tests { #[test] fn debug() { - let x = BigInt::from(1003); + let x = big_int::from(1003); assert_eq!(format!("Number is: {x:?}"), "Number is: +1003"); - let x = BigInt::from(0); + let x = big_int::from(0); assert_eq!(format!("Number is: {x:?}"), "Number is: +0"); - let x = BigInt::from(-100); + let x = big_int::from(-100); assert_eq!(format!("Number is: {x:?}"), "Number is: -100"); - let x = BigInt::from(-0); + let x = big_int::from(-0); assert_eq!(format!("Number is: {x:?}"), "Number is: +0"); - let x = BigInt::from(-1); + let x = big_int::from(-1); assert_eq!(format!("Number is: {x:?}"), "Number is: -1"); - let x = BigInt::from(320020000981234567890i128); + let x = big_int::from(320020000981234567890i128); assert_eq!( format!("Number is: {x:?}"), "Number is: +320020000981234567890" @@ -716,13 +716,13 @@ mod tests { #[test] fn to_words() { - let x = BigInt::from(20); + let x = big_int::from(20); assert_eq!(x.to_words(), "two zero"); - let x = BigInt::from(-24); + let x = big_int::from(-24); assert_eq!(x.to_words(), "minus two four"); - let x = BigInt::from(2550021); + let x = big_int::from(2550021); assert_eq!(x.to_words(), "two five five zero zero two one"); - let x = BigInt::from(-2000000000001i128); + let x = big_int::from(-2000000000001i128); assert_eq!( x.to_words(), "minus two zero zero zero zero zero zero zero zero zero zero zero one" @@ -731,7 +731,7 @@ mod tests { #[test] fn negation() { - let mut x = BigInt::from(1); + let mut x = big_int::from(1); assert_eq!(x.positive, true); assert_eq!(x.to_string(), "1"); x = !x; @@ -740,7 +740,7 @@ mod tests { x = !x; assert_eq!(x.positive, true); assert_eq!(x.to_string(), "1"); - let mut x = BigInt::from(-22); + let mut x = big_int::from(-22); assert_eq!(x.positive, false); assert_eq!(x.to_string(), "-22"); x = !x; @@ -750,7 +750,7 @@ mod tests { #[test] fn not() { - let mut x = BigInt::from(1); + let mut x = big_int::from(1); assert_eq!(x.positive, true); assert_eq!(x.to_string(), "1"); x = -x; @@ -759,7 +759,7 @@ mod tests { x = -x; assert_eq!(x.positive, true); assert_eq!(x.to_string(), "1"); - let mut x = BigInt::from(-22); + let mut x = big_int::from(-22); assert_eq!(x.positive, false); assert_eq!(x.to_string(), "-22"); x = -x; @@ -769,181 +769,181 @@ mod tests { #[test] fn equal() { - let x = BigInt::from(10); - let y = BigInt::from(10); + let x = big_int::from(10); + let y = big_int::from(10); assert!(x == y); - let x = BigInt::from(101010); - let y = BigInt::from(101010); + let x = big_int::from(101010); + let y = big_int::from(101010); assert!(x == y); - let x = BigInt::from(101010); - let y = BigInt::from(101210); + let x = big_int::from(101010); + let y = big_int::from(101210); assert!(!(x == y)); - let x = BigInt::from(101010); - let y = BigInt::from(1); + let x = big_int::from(101010); + let y = big_int::from(1); assert!(!(x == y)); - let x = BigInt::from(0); - let y = BigInt::from(0); + let x = big_int::from(0); + let y = big_int::from(0); assert!(x == y); - let x = BigInt::from(10); - let y = BigInt::from(-10); + let x = big_int::from(10); + let y = big_int::from(-10); assert!(x != y); - let x = BigInt::from(11); - let y = BigInt::from(10); + let x = big_int::from(11); + let y = big_int::from(10); assert!(x != y); - let x = BigInt::from(-0); - let y = BigInt::from(0); + let x = big_int::from(-0); + let y = big_int::from(0); assert!(x == y); } #[test] fn equal_int() { - let x = BigInt::from(10); + let x = big_int::from(10); assert!(x == 10); - let x = BigInt::from(101010); + let x = big_int::from(101010); assert!(x == 101010); - let x = BigInt::from(101010); + let x = big_int::from(101010); assert!(!(x == 101210)); - let x = BigInt::from(101010); + let x = big_int::from(101010); assert!(!(x == 1)); - let x = BigInt::from(0); + let x = big_int::from(0); assert!(x == 0); - let x = BigInt::from(10); + let x = big_int::from(10); assert!(x != -10); - let x = BigInt::from(11); + let x = big_int::from(11); assert!(x != 10); - let x = BigInt::from(-0); + let x = big_int::from(-0); assert!(x == 0); } #[test] fn equal_str() { - let x = BigInt::from(10); + let x = big_int::from(10); assert!(x == "10"); - let x = BigInt::from(101010); + let x = big_int::from(101010); assert!(x == "101010"); - let x = BigInt::from(101010); + let x = big_int::from(101010); assert!(!(x == "101210")); - let x = BigInt::from(101010); + let x = big_int::from(101010); assert!(!(x == "1")); - let x = BigInt::from(0); + let x = big_int::from(0); assert!(x == "0"); - let x = BigInt::from(10); + let x = big_int::from(10); assert!(x != "-10"); - let x = BigInt::from(11); + let x = big_int::from(11); assert!(x != "10"); - let x = BigInt::from(-0); + let x = big_int::from(-0); assert!(x == "0"); } #[test] fn greater() { - let x: BigInt = BigInt::from(15); - let y = BigInt::from(10); + let x: big_int = big_int::from(15); + let y = big_int::from(10); assert!(x > y); - let x: BigInt = BigInt::from(8); - let y = BigInt::from(7); + let x: big_int = big_int::from(8); + let y = big_int::from(7); assert!(x > y); - let x = BigInt::from(10); - let y = BigInt::from(10); + let x = big_int::from(10); + let y = big_int::from(10); assert!(!(x > y)); - let x = BigInt::from(10); - let y = BigInt::from(10); + let x = big_int::from(10); + let y = big_int::from(10); assert!(x >= y); - let x = BigInt::from(101010); - let y = BigInt::from(101010); + let x = big_int::from(101010); + let y = big_int::from(101010); assert!(x >= y); - let x = BigInt::from(0); - let y = BigInt::from(0); + let x = big_int::from(0); + let y = big_int::from(0); assert!(!(x > y)); - let x = BigInt::from(10); - let y = BigInt::from(-10); + let x = big_int::from(10); + let y = big_int::from(-10); assert!(x > y); - let x = BigInt::from(11); - let y = BigInt::from(10); + let x = big_int::from(11); + let y = big_int::from(10); assert!(x > y); } #[test] fn lesser() { - let x: BigInt = BigInt::from(0); - let y = BigInt::from(10); + let x: big_int = big_int::from(0); + let y = big_int::from(10); assert!(x < y); - let x: BigInt = BigInt::from(8); - let y = BigInt::from(9); + let x: big_int = big_int::from(8); + let y = big_int::from(9); assert!(x < y); - let x = BigInt::from(10); - let y = BigInt::from(10); + let x = big_int::from(10); + let y = big_int::from(10); assert!(!(x < y)); - let x = BigInt::from(10); - let y = BigInt::from(10); + let x = big_int::from(10); + let y = big_int::from(10); assert!(x <= y); - let x = BigInt::from(99999999999i64); - let y = BigInt::from(99999999999i128); + let x = big_int::from(99999999999i64); + let y = big_int::from(99999999999i128); assert!(x <= y); - let x = BigInt::from(0); - let y = BigInt::from(0); + let x = big_int::from(0); + let y = big_int::from(0); assert!(!(x < y)); - let x = BigInt::from(-10); - let y = BigInt::from(10); + let x = big_int::from(-10); + let y = big_int::from(10); assert!(x < y); - let x = BigInt::from(11); - let y = BigInt::from(99999999999u64); + let x = big_int::from(11); + let y = big_int::from(99999999999u64); assert!(x < y); } #[test] fn add() { - let mut x: BigInt = BigInt::from(10); - let y = BigInt::from(10); + let mut x: big_int = big_int::from(10); + let y = big_int::from(10); let z = x.clone() + y.clone(); x += y; assert_eq!(z, 20); assert_eq!(z, x); - let mut x = BigInt::from(101010); - let y = BigInt::from(101010); + let mut x = big_int::from(101010); + let y = big_int::from(101010); let z = x.clone() + y.clone(); x += y; assert_eq!(z, 202020); assert_eq!(z, x); - let mut x = BigInt::from(0); - let y = BigInt::from(0); + let mut x = big_int::from(0); + let y = big_int::from(0); let z = x.clone() + y.clone(); x += y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x = BigInt::from(10); - let y = BigInt::from(-10); + let mut x = big_int::from(10); + let y = big_int::from(-10); let z = x.clone() + y.clone(); x += y.clone(); assert_eq!(z, 0); assert_ne!(x, y); - let mut x = BigInt::from(11); - let y = BigInt::from(10); + let mut x = big_int::from(11); + let y = big_int::from(10); let z = x.clone() + y.clone(); x += y.clone(); assert_eq!(z, 21); assert_ne!(x, y); - let mut x = BigInt::from(-0); - let y = BigInt::from(0); + let mut x = big_int::from(-0); + let y = big_int::from(0); let z = x.clone() + y.clone(); x += y.clone(); assert_eq!(z, 0); assert_eq!(x, y); - let mut x = BigInt::from(6); - let y = BigInt::from(4); + let mut x = big_int::from(6); + let y = big_int::from(4); let z = x.clone() + y.clone(); x += y.clone(); assert_eq!(z, 10); assert_eq!(x, y); - let mut x = BigInt::from(-15); - let y = BigInt::from(-4); + let mut x = big_int::from(-15); + let y = big_int::from(-4); let z = x.clone() + y.clone(); x += y.clone(); assert_eq!(z, -19); @@ -952,64 +952,64 @@ mod tests { #[test] fn sub() { - let mut x: BigInt = BigInt::from(10); - let y = BigInt::from(10); + let mut x: big_int = big_int::from(10); + let y = big_int::from(10); let z = x.clone() - y.clone(); x -= y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x = BigInt::from(101010); - let y = BigInt::from(10); + let mut x = big_int::from(101010); + let y = big_int::from(10); let z = x.clone() - y.clone(); x -= y; assert_eq!(z, 101000); assert_eq!(z, x); - let mut x = BigInt::from(0); - let y = BigInt::from(0); + let mut x = big_int::from(0); + let y = big_int::from(0); let z = x.clone() - y.clone(); x -= y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x = BigInt::from(10); - let y = BigInt::from(-10); + let mut x = big_int::from(10); + let y = big_int::from(-10); let z = x.clone() - y.clone(); x -= y.clone(); assert_eq!(z, 20); assert_ne!(x, y); - let mut x = BigInt::from(-10); - let y = BigInt::from(10); + let mut x = big_int::from(-10); + let y = big_int::from(10); let z = x.clone() - y.clone(); x -= y.clone(); assert_eq!(z, -20); assert_ne!(x, y); - let mut x = BigInt::from(11); - let y = BigInt::from(10); + let mut x = big_int::from(11); + let y = big_int::from(10); let z = x.clone() - y.clone(); x -= y.clone(); assert_eq!(z, 1); assert_ne!(x, y); - let mut x = BigInt::from(-0); - let y = BigInt::from(0); + let mut x = big_int::from(-0); + let y = big_int::from(0); let z = x.clone() - y.clone(); x -= y.clone(); assert_eq!(z, 0); assert_eq!(x, y); - let mut x = BigInt::from(6); - let y = BigInt::from(4); + let mut x = big_int::from(6); + let y = big_int::from(4); let z = x.clone() - y.clone(); x -= y.clone(); assert_eq!(z, 2); assert_eq!(x, y); - let mut x = BigInt::from(-15); - let y = BigInt::from(-4); + let mut x = big_int::from(-15); + let y = big_int::from(-4); let z = x.clone() - y.clone(); x -= y.clone(); assert_eq!(z, -11); @@ -1018,65 +1018,65 @@ mod tests { #[test] fn mul() { - let mut x: BigInt = BigInt::from(10); - let y = BigInt::from(10); - let z: BigInt = x.clone() * y.clone(); + let mut x: big_int = big_int::from(10); + let y = big_int::from(10); + let z: big_int = x.clone() * y.clone(); x *= y; assert_eq!(z, 100); assert_eq!(z, x); - let mut x = BigInt::from(101); - let y = BigInt::from(101); + let mut x = big_int::from(101); + let y = big_int::from(101); let z = x.clone() * y.clone(); x *= y; assert_eq!(z, 10201); assert_eq!(z, x); - let mut x = BigInt::from(0); - let y = BigInt::from(0); - let z: BigInt = x.clone() * y.clone(); + let mut x = big_int::from(0); + let y = big_int::from(0); + let z: big_int = x.clone() * y.clone(); x *= y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x = BigInt::from(20100000100u64); - let y = BigInt::from(0); - let z: BigInt = x.clone() * y.clone(); + let mut x = big_int::from(20100000100u64); + let y = big_int::from(0); + let z: big_int = x.clone() * y.clone(); x *= y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x = BigInt::from(10); - let y = BigInt::from(-10); - let z: BigInt = x.clone() * y.clone(); + let mut x = big_int::from(10); + let y = big_int::from(-10); + let z: big_int = x.clone() * y.clone(); x *= y.clone(); assert_eq!(z, -100); assert_ne!(x, y); - let mut x = BigInt::from(11); - let y = BigInt::from(10); - let z: BigInt = x.clone() * y.clone(); + let mut x = big_int::from(11); + let y = big_int::from(10); + let z: big_int = x.clone() * y.clone(); x *= y.clone(); assert_eq!(z, 110); assert_ne!(x, y); - let mut x = BigInt::from(-0); - let y = BigInt::from(0); - let z: BigInt = x.clone() * y.clone(); + let mut x = big_int::from(-0); + let y = big_int::from(0); + let z: big_int = x.clone() * y.clone(); x *= y.clone(); assert_eq!(z, 0); assert_eq!(x, y); - let mut x = BigInt::from(6); - let y = BigInt::from(4); - let z: BigInt = x.clone() * y.clone(); + let mut x = big_int::from(6); + let y = big_int::from(4); + let z: big_int = x.clone() * y.clone(); x *= y.clone(); assert_eq!(z, 24); assert_eq!(x, y); - let mut x = BigInt::from(-15); - let y = BigInt::from(-4); - let z: BigInt = x.clone() * y.clone(); + let mut x = big_int::from(-15); + let y = big_int::from(-4); + let z: big_int = x.clone() * y.clone(); x *= y.clone(); assert_eq!(z, 60); assert_eq!(x, y); @@ -1084,71 +1084,71 @@ mod tests { #[test] fn div() { - let mut x: BigInt = BigInt::from(10000); - let y = BigInt::from(10); + let mut x: big_int = big_int::from(10000); + let y = big_int::from(10); let z = x.clone() / y.clone(); x /= y; assert_eq!(z, 1000); assert_eq!(z, x); - let mut x = BigInt::from(101); - let y = BigInt::from(101); + let mut x = big_int::from(101); + let y = big_int::from(101); let z = x.clone() / y.clone(); x /= y; assert_eq!(z, 1); assert_eq!(z, x); - let mut x = BigInt::from(0); - let y = BigInt::from(2); + let mut x = big_int::from(0); + let y = big_int::from(2); let z = x.clone() / y.clone(); x /= y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x = BigInt::from(0); - let y = BigInt::from(0); + let mut x = big_int::from(0); + let y = big_int::from(0); let z = x.clone() / y.clone(); x /= y; - assert_eq!(z, BigInt::default()); + assert_eq!(z, big_int::default()); assert_eq!(z, x); - let mut x = BigInt::from(10000); - let y = BigInt::from(0); + let mut x = big_int::from(10000); + let y = big_int::from(0); let z = x.clone() / y.clone(); x /= y; - assert_eq!(z, BigInt::default()); + assert_eq!(z, big_int::default()); assert_eq!(z, x); - let mut x = BigInt::from(20100000100u64); - let y = BigInt::from(200); + let mut x = big_int::from(20100000100u64); + let y = big_int::from(200); let z = x.clone() / y.clone(); x /= y; assert_eq!(z, 100500); assert_eq!(z, x); - let mut x = BigInt::from(10); - let y = BigInt::from(-10); + let mut x = big_int::from(10); + let y = big_int::from(-10); let z = x.clone() / y.clone(); x /= y.clone(); assert_eq!(z, -1); assert_ne!(x, y); - let mut x = BigInt::from(110); - let y = BigInt::from(10); + let mut x = big_int::from(110); + let y = big_int::from(10); let z = x.clone() / y.clone(); x /= y.clone(); assert_eq!(z, 11); assert_ne!(x, y); - let mut x = BigInt::from(6); - let y = BigInt::from(2); + let mut x = big_int::from(6); + let y = big_int::from(2); let z = x.clone() / y.clone(); x /= y.clone(); assert_eq!(z, 3); assert_eq!(x, y); - let mut x = BigInt::from(-15); - let y = BigInt::from(-5); + let mut x = big_int::from(-15); + let y = big_int::from(-5); let z = x.clone() / y.clone(); x /= y.clone(); assert_eq!(z, 3); @@ -1157,50 +1157,50 @@ mod tests { #[test] fn reminder() { - let mut x: BigInt = BigInt::from(10000); - let y = BigInt::from(10); + let mut x: big_int = big_int::from(10000); + let y = big_int::from(10); let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x: BigInt = BigInt::from(10); - let y = BigInt::from(0); + let mut x: big_int = big_int::from(10); + let y = big_int::from(0); let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x: BigInt = BigInt::from(10); - let y = BigInt::from(7); + let mut x: big_int = big_int::from(10); + let y = big_int::from(7); let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 3); assert_eq!(z, x); - let mut x: BigInt = BigInt::from(10000); - let y = BigInt::from(10); + let mut x: big_int = big_int::from(10000); + let y = big_int::from(10); let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x: BigInt = BigInt::from(-104); - let y = BigInt::from(10); + let mut x: big_int = big_int::from(-104); + let y = big_int::from(10); let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 4); assert_eq!(z, x); - let mut x: BigInt = BigInt::from(24); - let y = BigInt::from(3); + let mut x: big_int = big_int::from(24); + let y = big_int::from(3); let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x: BigInt = BigInt::from(33); - let y = BigInt::from(7); + let mut x: big_int = big_int::from(33); + let y = big_int::from(7); let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 5); @@ -1209,52 +1209,52 @@ mod tests { #[test] fn binary() { - let x = BigInt::from(4); + let x = big_int::from(4); assert_eq!(format!("{x:b}"), "100"); - let x = BigInt::from(4); + let x = big_int::from(4); assert_eq!(format!("{x:#b}"), "0b100"); - let x = BigInt::from(4); + let x = big_int::from(4); assert_eq!(format!("{x:010b}"), "0000000100"); - let x = BigInt::from(10); + let x = big_int::from(10); assert_eq!(format!("{x:#110b}"), "0b11111010"); - let x = BigInt::from(172); + let x = big_int::from(172); assert_eq!(format!("{x:b}"), "10101100"); - let x = BigInt::from(17220003931i64); + let x = big_int::from(17220003931i64); assert_eq!(format!("{x:#b}"), "0b10000000010011001000110100001011011"); } #[test] fn bit_and() { - let mut x = BigInt::from(4); //100 - let y = BigInt::from(12); //1100 + let mut x = big_int::from(4); //100 + let y = big_int::from(12); //1100 let z = x.clone() & y.clone(); //0100 x &= y; assert_eq!(x, z); assert_eq!(z, 4); - let mut x = BigInt::from(10); //1010 - let y = BigInt::from(13); //1101 + let mut x = big_int::from(10); //1010 + let y = big_int::from(13); //1101 let z = x.clone() & y.clone(); //1000 x &= y; assert_eq!(x, z); assert_eq!(z, 8); - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(223); //11011111 + let mut x = big_int::from(172); //10101100 + let y = big_int::from(223); //11011111 let z = x.clone() & y.clone(); //10001100 x &= y; assert_eq!(x, z); assert_eq!(z, 140); - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(1); //1 + let mut x = big_int::from(172); //10101100 + let y = big_int::from(1); //1 let z = x.clone() & y.clone(); //0 x &= y; assert_eq!(x, z); assert_eq!(z, 0); - let mut x = BigInt::from(173); //10101101 - let y = BigInt::from(1); //1 + let mut x = big_int::from(173); //10101101 + let y = big_int::from(1); //1 let z = x.clone() & y.clone(); //1 x &= y; assert_eq!(x, z); @@ -1263,36 +1263,36 @@ mod tests { #[test] fn bit_or() { - let mut x = BigInt::from(4); //100 - let y = BigInt::from(12); //1100 + let mut x = big_int::from(4); //100 + let y = big_int::from(12); //1100 let z = x.clone() | y.clone(); //1100 x |= y; assert_eq!(x, z); assert_eq!(z, 12); - let mut x = BigInt::from(10); //1010 - let y = BigInt::from(13); //1101 + let mut x = big_int::from(10); //1010 + let y = big_int::from(13); //1101 let z = x.clone() | y.clone(); //1111 x |= y; assert_eq!(x, z); assert_eq!(z, 15); - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(223); //11011111 + let mut x = big_int::from(172); //10101100 + let y = big_int::from(223); //11011111 let z = x.clone() | y.clone(); //11111111 x |= y; assert_eq!(x, z); assert_eq!(z, 255); - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(1); //1 + let mut x = big_int::from(172); //10101100 + let y = big_int::from(1); //1 let z = x.clone() | y.clone(); //10101101 x |= y; assert_eq!(x, z); assert_eq!(z, 173); - let mut x = BigInt::from(173); //10101101 - let y = BigInt::from(1); //1 + let mut x = big_int::from(173); //10101101 + let y = big_int::from(1); //1 let z = x.clone() | y.clone(); //10101101 x |= y; assert_eq!(x, z); @@ -1301,36 +1301,36 @@ mod tests { #[test] fn bit_xor() { - let mut x = BigInt::from(4); //100 - let y = BigInt::from(12); //1100 + let mut x = big_int::from(4); //100 + let y = big_int::from(12); //1100 let z = x.clone() ^ y.clone(); //1000 x ^= y; assert_eq!(x, z); assert_eq!(z, 8); - let mut x = BigInt::from(10); //1010 - let y = BigInt::from(13); //1101 + let mut x = big_int::from(10); //1010 + let y = big_int::from(13); //1101 let z = x.clone() ^ y.clone(); //0111 x ^= y; assert_eq!(x, z); assert_eq!(z, 7); - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(223); //11011111 + let mut x = big_int::from(172); //10101100 + let y = big_int::from(223); //11011111 let z = x.clone() ^ y.clone(); //01110011 x ^= y; assert_eq!(x, z); assert_eq!(z, 115); - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(1); //1 + let mut x = big_int::from(172); //10101100 + let y = big_int::from(1); //1 let z = x.clone() ^ y.clone(); //10101101 x ^= y; assert_eq!(x, z); assert_eq!(z, 173); - let mut x = BigInt::from(173); //10101101 - let y = BigInt::from(1); //1 + let mut x = big_int::from(173); //10101101 + let y = big_int::from(1); //1 let z = x.clone() ^ y.clone(); //10101100 x ^= y; assert_eq!(x, z); @@ -1339,36 +1339,36 @@ mod tests { #[test] fn bit_shift_left() { - let mut x = BigInt::from(4); //100 - let y = BigInt::from(2); + let mut x = big_int::from(4); //100 + let y = big_int::from(2); let z = x.clone() << y.clone(); //10000 x <<= y; assert_eq!(x, z); assert_eq!(z, 16); - let mut x = BigInt::from(10); //1010 - let y = BigInt::from(1); + let mut x = big_int::from(10); //1010 + let y = big_int::from(1); let z = x.clone() << y.clone(); //10100 x <<= y; assert_eq!(x, z); assert_eq!(z, 20); - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(0); + let mut x = big_int::from(172); //10101100 + let y = big_int::from(0); let z = x.clone() << y.clone(); x <<= y; assert_eq!(x, z); assert_eq!(z, 172); - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(10); + let mut x = big_int::from(172); //10101100 + let y = big_int::from(10); let z = x.clone() << y.clone(); //101011000000000000 x <<= y; assert_eq!(x, z); assert_eq!(z, 176128); - let mut x = BigInt::from(173); //10101101 - let y = BigInt::from(1); + let mut x = big_int::from(173); //10101101 + let y = big_int::from(1); let z = x.clone() << y.clone(); //101011010 x <<= y; assert_eq!(x, z); @@ -1377,36 +1377,36 @@ mod tests { #[test] fn bit_shift_right() { - let mut x = BigInt::from(4); //100 - let y = BigInt::from(2); + let mut x = big_int::from(4); //100 + let y = big_int::from(2); let z = x.clone() >> y.clone(); //1 x >>= y; assert_eq!(x, z); assert_eq!(z, 1); - let mut x = BigInt::from(10); //1010 - let y = BigInt::from(1); + let mut x = big_int::from(10); //1010 + let y = big_int::from(1); let z = x.clone() >> y.clone(); //101 x >>= y; assert_eq!(x, z); assert_eq!(z, 5); - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(0); + let mut x = big_int::from(172); //10101100 + let y = big_int::from(0); let z = x.clone() >> y.clone(); //10101100 x >>= y; assert_eq!(x, z); assert_eq!(z, 172); - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(10); + let mut x = big_int::from(172); //10101100 + let y = big_int::from(10); let z = x.clone() >> y.clone(); //0 x >>= y; assert_eq!(x, z); assert_eq!(z, 0); - let mut x = BigInt::from(173); //10101101 - let y = BigInt::from(1); + let mut x = big_int::from(173); //10101101 + let y = big_int::from(1); let z = x.clone() >> y.clone(); //1010110 x >>= y; assert_eq!(x, z); @@ -1415,86 +1415,86 @@ mod tests { #[test] fn hexadecimal() { - let x = BigInt::from(4); + let x = big_int::from(4); assert_eq!(format!("{x:X}"), "4"); - let x = BigInt::from(16); + let x = big_int::from(16); assert_eq!(format!("{x:#X}"), "0xF"); - let x = BigInt::from(10); + let x = big_int::from(10); assert_eq!(format!("{x:#X}"), "0x10"); - let x = BigInt::from(172); + let x = big_int::from(172); assert_eq!(format!("{x:X}"), "AC"); - let x = BigInt::from(17220003931u128); + let x = big_int::from(17220003931u128); assert_eq!(format!("{x:#X}"), "0x40264685B"); - let x = BigInt::from(4); + let x = big_int::from(4); assert_eq!(format!("{x:x}"), "4"); - let x = BigInt::from(16); + let x = big_int::from(16); assert_eq!(format!("{x:#x}"), "0xf"); - let x = BigInt::from(10); + let x = big_int::from(10); assert_eq!(format!("{x:#x}"), "0x10"); - let x = BigInt::from(172); + let x = big_int::from(172); assert_eq!(format!("{x:x}"), "ac"); - let x = BigInt::from(17220003931u128); + let x = big_int::from(17220003931u128); assert_eq!(format!("{x:#x}"), "0x40264685b"); } #[test] fn progtest_tests() { - let mut a = BigInt::new(); - let mut b = BigInt::new(); - a = BigInt::from(10); - a += BigInt::from(20); + let mut a = big_int::new(); + let mut b = big_int::new(); + a = big_int::from(10); + a += big_int::from(20); assert_eq!(a, 30); - a *= BigInt::from(5); + a *= big_int::from(5); assert_eq!(a, 150); - b = a.clone() + BigInt::from(3); + b = a.clone() + big_int::from(3); assert_eq!(b, 153); - b = a.clone() * BigInt::from(7); + b = a.clone() * big_int::from(7); assert_eq!(b, 1050); assert_eq!(a, 150); assert_eq!(format!("{a:X}"), "96"); - a = BigInt::from(10); - a += BigInt::from(-20); + a = big_int::from(10); + a += big_int::from(-20); assert_eq!(a, -10); - a *= BigInt::from(5); + a *= big_int::from(5); assert_eq!(a, -50); - b = a.clone() + BigInt::from(73); + b = a.clone() + big_int::from(73); assert_eq!(b, 23); - b = a.clone() * BigInt::from(-7); + b = a.clone() * big_int::from(-7); assert_eq!(b, 350); assert_eq!(a, -50); assert_eq!(format!("{a:X}"), "-32"); - a = BigInt::from(12345678901234567890i128); - a += BigInt::from(-99999999999999999999i128); - assert_eq!(a, -87654321098765432109); - a *= BigInt::from(54321987654321987654i128); + a = big_int::from(12345678901234567890i128); + a += big_int::from(-99999999999999999999i128); + assert_eq!(a, -87654321098765432109i128); + a *= big_int::from(54321987654321987654i128); assert_eq!(a, "-4761556948575111126880627366067073182286"); - a *= BigInt::from(0); + a *= big_int::from(0); assert_eq!(a, 0); - a = BigInt::from(10); + a = big_int::from(10); b = a.clone() + 400; assert_eq!(b, "410"); - b = a.clone() * BigInt::from_str("15").unwrap_or_default(); + b = a.clone() * big_int::from_str("15").unwrap_or_default(); assert_eq!(b, "150"); assert_eq!(a, "10"); assert_eq!(format!("{a:X}"), "A"); - b = BigInt::from_str("1234").unwrap_or_default(); + b = big_int::from_str("1234").unwrap_or_default(); assert_eq!(format!("{b}"), "1234"); - assert!(BigInt::from_str(" 12 34").is_err()); - assert!(BigInt::from_str("999z").is_err()); - assert!(BigInt::from_str("abcd").is_err()); - assert!(BigInt::from_str("-xyz").is_err()); - assert!(BigInt::from_str(":").is_err()); - assert!(BigInt::from_str("%").is_err()); - assert!(BigInt::from_str("- 758").is_err()); - a = BigInt::from(42); + assert!(big_int::from_str(" 12 34").is_err()); + assert!(big_int::from_str("999z").is_err()); + assert!(big_int::from_str("abcd").is_err()); + assert!(big_int::from_str("-xyz").is_err()); + assert!(big_int::from_str(":").is_err()); + assert!(big_int::from_str("%").is_err()); + assert!(big_int::from_str("- 758").is_err()); + a = big_int::from(42); assert_eq!(a, 42); - a = BigInt::from(73786976294838206464i128); - assert_eq!(a, 73786976294838206464); + a = big_int::from(73786976294838206464i128); + assert_eq!(a, 73786976294838206464u128); assert_eq!(format!("{a:X}"), "40000000000000000"); assert!(a < "1361129467683753853853498429727072845824"); assert!(a <= "1361129467683753853853498429727072845824"); @@ -1506,22 +1506,22 @@ mod tests { assert!(a <= 73786976294838206464); assert!(!(a > 73786976294838206464)); assert!(a >= 73786976294838206464); - assert!(a == 73786976294838206464); - assert!(!(a != 73786976294838206464)); + assert!(a == 73786976294838206464u128); + assert!(!(a != 73786976294838206464i128)); assert!(a < 73786976294838206465); assert!(a <= 73786976294838206465); assert!(!(a > 73786976294838206465)); assert!(!(a >= 73786976294838206465)); - assert!(!(a == 73786976294838206465)); - assert!(a != 73786976294838206465); - a = BigInt::from_str("2147483648").unwrap_or(BigInt::new()); + assert!(!(a == 73786976294838206465u128)); + assert!(a != 73786976294838206465i128); + a = big_int::from_str("2147483648").unwrap_or(big_int::new()); assert!(!(a < -2147483648)); assert!(!(a <= -2147483648)); assert!(a > -2147483648); assert!(a >= -2147483648); assert!(!(a == -2147483648)); assert!(a != -2147483648); - a = BigInt::from_str("-12345678").unwrap_or(BigInt::new()); + a = big_int::from_str("-12345678").unwrap_or(big_int::new()); assert!(!(a < -87654321)); assert!(!(a <= -87654321)); assert!(a > -87654321); From b2a88d74e40fab49baceb5cc097462b6a2f1910c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 15 Jul 2025 01:01:03 +0200 Subject: [PATCH 078/243] move of asociated functions to be in impl big_int --- src/lib.rs | 138 ++++++++++++++++++++++++++--------------------------- 1 file changed, 69 insertions(+), 69 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 42bf996..1acde0a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,72 +30,6 @@ impl Default for big_int { } } -fn word_to_number(word: &str) -> Result { - match word { - "zero" => Ok(0), - "one" => Ok(1), - "two" => Ok(2), - "three" => Ok(3), - "four" => Ok(4), - "five" => Ok(5), - "six" => Ok(6), - "seven" => Ok(7), - "eight" => Ok(8), - "nine" => Ok(9), - _ => Err(BigIntError::NaN), - } -} - -fn number_to_word(nmr: u8) -> String { - match nmr { - 0 => "zero", - 1 => "one", - 2 => "two", - 3 => "three", - 4 => "four", - 5 => "five", - 6 => "six", - 7 => "seven", - 8 => "eight", - 9 => "nine", - _ => "zero", - } - .to_string() -} - -fn parse_word_digits(num_str: String) -> Result { - //create lowercase iterator - let mut parsed = num_str.split_whitespace().map(str::to_lowercase).peekable(); - let mut fin_pos = true; - let mut fin_vec: Vec = Vec::new(); - - //if empty string - if parsed.peek().is_none() { - return Err(BigIntError::NaN); - } - - //positive/negative - if let Some("-" | "minus") = parsed.peek().map(String::as_str) { - fin_pos = false; - parsed.next(); - } - - //loop for translating words to u8 - for word in parsed { - fin_vec.push(word_to_number(&word)?); - } - - //additional check - if fin_vec.is_empty() { - return Err(BigIntError::NaN); - } - - Ok(big_int { - positive: fin_pos, - numbers: fin_vec, - }) -} - impl FromStr for big_int { type Err = BigIntError; fn from_str(mut num_str: &str) -> Result { @@ -114,7 +48,7 @@ impl FromStr for big_int { for char in num_str.chars() { if !char.is_ascii_digit() { - return parse_word_digits(if fin_pos { + return big_int::parse_word_digits(if fin_pos { num_str.to_string() } else { format!("-{}", num_str) @@ -556,16 +490,82 @@ impl big_int { if !self.positive { fin_str = "minus".to_string(); } else { - fin_str = number_to_word(*nmr_iter.next().unwrap_or(&0)); + fin_str = big_int::number_to_word(*nmr_iter.next().unwrap_or(&0)); } //print all digits for num in nmr_iter { - fin_str = format!("{} {}", fin_str, number_to_word(*num)); + fin_str = format!("{} {}", fin_str, big_int::number_to_word(*num)); } fin_str } + + fn word_to_number(word: &str) -> Result { + match word { + "zero" => Ok(0), + "one" => Ok(1), + "two" => Ok(2), + "three" => Ok(3), + "four" => Ok(4), + "five" => Ok(5), + "six" => Ok(6), + "seven" => Ok(7), + "eight" => Ok(8), + "nine" => Ok(9), + _ => Err(BigIntError::NaN), + } + } + + fn number_to_word(nmr: u8) -> String { + match nmr { + 0 => "zero", + 1 => "one", + 2 => "two", + 3 => "three", + 4 => "four", + 5 => "five", + 6 => "six", + 7 => "seven", + 8 => "eight", + 9 => "nine", + _ => "zero", + } + .to_string() + } + + fn parse_word_digits(num_str: String) -> Result { + //create lowercase iterator + let mut parsed = num_str.split_whitespace().map(str::to_lowercase).peekable(); + let mut fin_pos = true; + let mut fin_vec: Vec = Vec::new(); + + //if empty string + if parsed.peek().is_none() { + return Err(BigIntError::NaN); + } + + //positive/negative + if let Some("-" | "minus") = parsed.peek().map(String::as_str) { + fin_pos = false; + parsed.next(); + } + + //loop for translating words to u8 + for word in parsed { + fin_vec.push(big_int::word_to_number(&word)?); + } + + //additional check + if fin_vec.is_empty() { + return Err(BigIntError::NaN); + } + + Ok(big_int { + positive: fin_pos, + numbers: fin_vec, + }) + } } #[cfg(test)] From fb8b0d5df8db83a915de41a039b07f61a38666a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 15 Jul 2025 01:10:46 +0200 Subject: [PATCH 079/243] change all names to be longer and more exact --- src/lib.rs | 98 ++++++++++++++++++++++++------------------------------ 1 file changed, 44 insertions(+), 54 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1acde0a..83679fa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,40 +32,37 @@ impl Default for big_int { impl FromStr for big_int { type Err = BigIntError; - fn from_str(mut num_str: &str) -> Result { + fn from_str(mut string_of_numbers: &str) -> Result { //empty string edgecaase - if num_str.is_empty() { + if string_of_numbers.is_empty() { return Err(BigIntError::NaN); } - let mut fin_pos = !(num_str.starts_with('-')); - let mut fin_vec: Vec = Vec::new(); + let mut positive = !(string_of_numbers.starts_with('-')); + let mut numbers: Vec = Vec::new(); //if negative - remove '-' - if !fin_pos { - num_str = num_str.split_at(1).1; + if !positive { + string_of_numbers = string_of_numbers.split_at(1).1; } - for char in num_str.chars() { + for char in string_of_numbers.chars() { if !char.is_ascii_digit() { - return big_int::parse_word_digits(if fin_pos { - num_str.to_string() + return big_int::parse_word_digits(if positive { + string_of_numbers.to_string() } else { - format!("-{}", num_str) + format!("-{}", string_of_numbers) }); } - fin_vec.push(char.to_digit(10).unwrap().to_u8().unwrap()); + numbers.push(char.to_digit(10).unwrap().to_u8().unwrap()); } - if fin_vec == vec![0] { - fin_pos = true; + if numbers == vec![0] { + positive = true; } - Ok(big_int { - positive: fin_pos, - numbers: fin_vec, - }) + Ok(big_int { positive, numbers }) } } @@ -73,33 +70,29 @@ macro_rules! from_int { ($($t:ty),*)=>{ $( impl From<$t> for big_int{ - fn from(value: $t) -> Self { + fn from(mut original_number: $t) -> Self { //zero edgecase - if value == 0 { + if original_number == 0 { return big_int::default(); } - let mut val = value; - let mut fin_vec = Vec::new(); - let mut fin_pos = !(val < 0); + let mut numbers = Vec::new(); + let positive = !(original_number < 0); //negative number - if val < 0 { - val = -val; + if original_number < 0 { + original_number = -original_number; } //transformation of digits - while val != 0{ - fin_vec.insert(0, (val % 10).to_u8().unwrap()); - val = val / 10; + while original_number != 0{ + numbers.insert(0, (original_number % 10).to_u8().unwrap()); + original_number /= 10; } //return value - big_int { - positive: fin_pos, - numbers: fin_vec, - } + big_int {positive, numbers} } } @@ -111,27 +104,24 @@ macro_rules! from_uint { ($($t:ty),*)=>{ $( impl From<$t> for big_int{ - fn from(value: $t) -> Self { + fn from(mut original_number: $t) -> Self { //zero edgecase - if value == 0 { + if original_number == 0 { return big_int::default(); } - let mut val = value; - let mut fin_vec = Vec::new(); + let mut numbers = Vec::new(); //transformation of digits - while val != 0{ - fin_vec.insert(0, (val % 10).to_u8().unwrap()); - val = val / 10; + while original_number != 0{ + numbers.insert(0, (original_number % 10).to_u8().unwrap()); + original_number /= 10; } //return value big_int { - positive: true, - numbers: fin_vec, - } + positive: true, numbers} } } @@ -517,8 +507,8 @@ impl big_int { } } - fn number_to_word(nmr: u8) -> String { - match nmr { + fn number_to_word(number: u8) -> String { + match number { 0 => "zero", 1 => "one", 2 => "two", @@ -534,11 +524,14 @@ impl big_int { .to_string() } - fn parse_word_digits(num_str: String) -> Result { + fn parse_word_digits(string_of_numbers: String) -> Result { //create lowercase iterator - let mut parsed = num_str.split_whitespace().map(str::to_lowercase).peekable(); - let mut fin_pos = true; - let mut fin_vec: Vec = Vec::new(); + let mut parsed = string_of_numbers + .split_whitespace() + .map(str::to_lowercase) + .peekable(); + let mut positive = true; + let mut numbers: Vec = Vec::new(); //if empty string if parsed.peek().is_none() { @@ -547,24 +540,21 @@ impl big_int { //positive/negative if let Some("-" | "minus") = parsed.peek().map(String::as_str) { - fin_pos = false; + positive = false; parsed.next(); } //loop for translating words to u8 for word in parsed { - fin_vec.push(big_int::word_to_number(&word)?); + numbers.push(big_int::word_to_number(&word)?); } //additional check - if fin_vec.is_empty() { + if numbers.is_empty() { return Err(BigIntError::NaN); } - Ok(big_int { - positive: fin_pos, - numbers: fin_vec, - }) + Ok(big_int { positive, numbers }) } } From 4b63730a887a4c8b727ae3e5d8412428437a1984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 15 Jul 2025 01:12:05 +0200 Subject: [PATCH 080/243] change Debug to be derived --- src/lib.rs | 31 +------------------------------ 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 83679fa..083b90a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,7 +15,7 @@ enum BigIntError { NaN, } -#[derive(Clone, Eq)] +#[derive(Clone, Eq, Debug)] struct big_int { positive: bool, numbers: Vec, @@ -145,16 +145,6 @@ impl Display for big_int { } } -impl fmt::Debug for big_int { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - if self.positive { - write!(f, "+")?; - } - - write!(f, "{}", self) - } -} - impl Binary for big_int { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { todo!() @@ -685,25 +675,6 @@ mod tests { ); } - #[test] - fn debug() { - let x = big_int::from(1003); - assert_eq!(format!("Number is: {x:?}"), "Number is: +1003"); - let x = big_int::from(0); - assert_eq!(format!("Number is: {x:?}"), "Number is: +0"); - let x = big_int::from(-100); - assert_eq!(format!("Number is: {x:?}"), "Number is: -100"); - let x = big_int::from(-0); - assert_eq!(format!("Number is: {x:?}"), "Number is: +0"); - let x = big_int::from(-1); - assert_eq!(format!("Number is: {x:?}"), "Number is: -1"); - let x = big_int::from(320020000981234567890i128); - assert_eq!( - format!("Number is: {x:?}"), - "Number is: +320020000981234567890" - ); - } - #[test] fn to_words() { let x = big_int::from(20); From 28e7c53b4ed90774f43442730f946d08198070f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 15 Jul 2025 01:13:39 +0200 Subject: [PATCH 081/243] remove neg trait --- src/lib.rs | 28 +--------------------------- 1 file changed, 1 insertion(+), 27 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 083b90a..6115265 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -199,14 +199,7 @@ impl PartialEq for big_int { impl Neg for big_int { type Output = Self; - fn neg(self) -> Self::Output { - !self - } -} - -impl Not for big_int { - type Output = Self; - fn not(mut self) -> Self::Output { + fn neg(mut self) -> Self::Output { self.positive = !self.positive; self } @@ -690,25 +683,6 @@ mod tests { ); } - #[test] - fn negation() { - let mut x = big_int::from(1); - assert_eq!(x.positive, true); - assert_eq!(x.to_string(), "1"); - x = !x; - assert_eq!(x.positive, false); - assert_eq!(x.to_string(), "-1"); - x = !x; - assert_eq!(x.positive, true); - assert_eq!(x.to_string(), "1"); - let mut x = big_int::from(-22); - assert_eq!(x.positive, false); - assert_eq!(x.to_string(), "-22"); - x = !x; - assert_eq!(x.positive, true); - assert_eq!(x.to_string(), "22"); - } - #[test] fn not() { let mut x = big_int::from(1); From 677f8ecc498f7efa4f04283ea431c98625755610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 16 Jul 2025 00:09:21 +0200 Subject: [PATCH 082/243] move of tests to another file --- src/lib.rs | 924 +------------------------------------------------- src/tests.rs | 926 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 927 insertions(+), 923 deletions(-) create mode 100644 src/tests.rs diff --git a/src/lib.rs b/src/lib.rs index 6115265..4bf4bf6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -542,926 +542,4 @@ impl big_int { } #[cfg(test)] -mod tests { - use super::*; - - #[test] - fn default() { - let def = big_int::default(); - assert_eq!(def.positive, true); - assert_eq!(def.numbers, [0].to_vec()); - } - - #[test] - fn new() { - let new = big_int::new(); - assert_eq!(new.positive, true); - assert_eq!(new.numbers, [0].to_vec()); - } - - #[test] - fn from() { - let x = big_int::from(20); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [2, 0].to_vec()); - let x = big_int::from(-20); - assert_eq!(x.positive, false); - assert_eq!(x.numbers, [2, 0].to_vec()); - let x = big_int::from(-320020000981234567890i128); - assert_eq!(x.positive, false); - assert_eq!( - x.numbers, - [ - 3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 - ] - .to_vec() - ); - let x = big_int::from(0); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [0].to_vec()); - let x = big_int::from(-0); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [0].to_vec()); - } - - #[test] - fn from_string_numbers() { - let x = big_int::from_str("20").unwrap(); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [2, 0].to_vec()); - let x = big_int::from_str("666").unwrap(); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [6, 6, 6].to_vec()); - let x = big_int::from_str("-20").unwrap(); - assert_eq!(x.positive, false); - assert_eq!(x.numbers, [2, 0].to_vec()); - let x = big_int::from_str("-320020000981234567890").unwrap(); - assert_eq!(x.positive, false); - assert_eq!( - x.numbers, - [ - 3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 - ] - .to_vec() - ); - let x = big_int::from_str("-0").unwrap(); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [0].to_vec()); - let x = big_int::from_str("0sw"); - assert!(x.is_err()); - let x = big_int::from_str("0 2020000"); - assert!(x.is_err()); - let x = big_int::from_str("--200"); - assert!(x.is_err()); - let x = big_int::from_str("+2000303"); - assert!(x.is_err()); - let x = big_int::from_str("minus20003002"); - assert!(x.is_err()); - } - - #[test] - fn from_string_words_from_str_digits() { - let x = big_int::from_str("two zero ").unwrap(); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [2, 0].to_vec()); - let x = big_int::from_str("minus two four").unwrap(); - assert_eq!(x.positive, false); - assert_eq!(x.numbers, [2, 4].to_vec()); - let x = big_int::from_str("two five five zero zero two one").unwrap(); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [2, 5, 5, 0, 0, 2, 1].to_vec()); - let x = big_int::from_str("minus two zero zero zero zero zero one").unwrap(); - assert_eq!(x.positive, false); - assert_eq!(x.numbers, [2, 0, 0, 0, 0, 0, 1].to_vec()); - let x = big_int::from_str("zero").unwrap(); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [0].to_vec()); - let x = big_int::from_str("onse"); - assert!(x.is_err()); - let x = big_int::from_str(" "); - assert!(x.is_err()); - let x = big_int::from_str("twenty thousand thousand"); - assert!(x.is_err()); - let x: Result = big_int::from_str("twenty thousand hundred"); - assert!(x.is_err()); - let x = big_int::from_str("- five four").unwrap(); - assert_eq!(x.positive, false); - assert_eq!(x.numbers, [5, 4].to_vec()); - } - - #[test] - fn display() { - let x = big_int::from(1003); - assert_eq!(format!("Number is: {x}"), "Number is: 1003"); - let x = big_int::from(0); - assert_eq!(format!("Number is: {x}"), "Number is: 0"); - let x = big_int::from(-100); - assert_eq!(format!("Number is: {x}"), "Number is: -100"); - let x = big_int::from(-0); - assert_eq!(format!("Number is: {x}"), "Number is: 0"); - let x = big_int::from(-1); - assert_eq!(format!("Number is: {x}"), "Number is: -1"); - let x = big_int::from(320020000981234567890i128); - assert_eq!( - format!("Number is: {x}"), - "Number is: 320020000981234567890" - ); - } - - #[test] - fn to_words() { - let x = big_int::from(20); - assert_eq!(x.to_words(), "two zero"); - let x = big_int::from(-24); - assert_eq!(x.to_words(), "minus two four"); - let x = big_int::from(2550021); - assert_eq!(x.to_words(), "two five five zero zero two one"); - let x = big_int::from(-2000000000001i128); - assert_eq!( - x.to_words(), - "minus two zero zero zero zero zero zero zero zero zero zero zero one" - ); - } - - #[test] - fn not() { - let mut x = big_int::from(1); - assert_eq!(x.positive, true); - assert_eq!(x.to_string(), "1"); - x = -x; - assert_eq!(x.positive, false); - assert_eq!(x.to_string(), "-1"); - x = -x; - assert_eq!(x.positive, true); - assert_eq!(x.to_string(), "1"); - let mut x = big_int::from(-22); - assert_eq!(x.positive, false); - assert_eq!(x.to_string(), "-22"); - x = -x; - assert_eq!(x.positive, true); - assert_eq!(x.to_string(), "22"); - } - - #[test] - fn equal() { - let x = big_int::from(10); - let y = big_int::from(10); - assert!(x == y); - let x = big_int::from(101010); - let y = big_int::from(101010); - assert!(x == y); - let x = big_int::from(101010); - let y = big_int::from(101210); - assert!(!(x == y)); - let x = big_int::from(101010); - let y = big_int::from(1); - assert!(!(x == y)); - let x = big_int::from(0); - let y = big_int::from(0); - assert!(x == y); - let x = big_int::from(10); - let y = big_int::from(-10); - assert!(x != y); - let x = big_int::from(11); - let y = big_int::from(10); - assert!(x != y); - let x = big_int::from(-0); - let y = big_int::from(0); - assert!(x == y); - } - - #[test] - fn equal_int() { - let x = big_int::from(10); - assert!(x == 10); - let x = big_int::from(101010); - assert!(x == 101010); - let x = big_int::from(101010); - assert!(!(x == 101210)); - let x = big_int::from(101010); - assert!(!(x == 1)); - let x = big_int::from(0); - assert!(x == 0); - let x = big_int::from(10); - assert!(x != -10); - let x = big_int::from(11); - assert!(x != 10); - let x = big_int::from(-0); - assert!(x == 0); - } - - #[test] - fn equal_str() { - let x = big_int::from(10); - assert!(x == "10"); - let x = big_int::from(101010); - assert!(x == "101010"); - let x = big_int::from(101010); - assert!(!(x == "101210")); - let x = big_int::from(101010); - assert!(!(x == "1")); - let x = big_int::from(0); - assert!(x == "0"); - let x = big_int::from(10); - assert!(x != "-10"); - let x = big_int::from(11); - assert!(x != "10"); - let x = big_int::from(-0); - assert!(x == "0"); - } - - #[test] - fn greater() { - let x: big_int = big_int::from(15); - let y = big_int::from(10); - assert!(x > y); - let x: big_int = big_int::from(8); - let y = big_int::from(7); - assert!(x > y); - let x = big_int::from(10); - let y = big_int::from(10); - assert!(!(x > y)); - let x = big_int::from(10); - let y = big_int::from(10); - assert!(x >= y); - let x = big_int::from(101010); - let y = big_int::from(101010); - assert!(x >= y); - let x = big_int::from(0); - let y = big_int::from(0); - assert!(!(x > y)); - let x = big_int::from(10); - let y = big_int::from(-10); - assert!(x > y); - let x = big_int::from(11); - let y = big_int::from(10); - assert!(x > y); - } - - #[test] - fn lesser() { - let x: big_int = big_int::from(0); - let y = big_int::from(10); - assert!(x < y); - let x: big_int = big_int::from(8); - let y = big_int::from(9); - assert!(x < y); - let x = big_int::from(10); - let y = big_int::from(10); - assert!(!(x < y)); - let x = big_int::from(10); - let y = big_int::from(10); - assert!(x <= y); - let x = big_int::from(99999999999i64); - let y = big_int::from(99999999999i128); - assert!(x <= y); - let x = big_int::from(0); - let y = big_int::from(0); - assert!(!(x < y)); - let x = big_int::from(-10); - let y = big_int::from(10); - assert!(x < y); - let x = big_int::from(11); - let y = big_int::from(99999999999u64); - assert!(x < y); - } - - #[test] - fn add() { - let mut x: big_int = big_int::from(10); - let y = big_int::from(10); - let z = x.clone() + y.clone(); - x += y; - assert_eq!(z, 20); - assert_eq!(z, x); - - let mut x = big_int::from(101010); - let y = big_int::from(101010); - let z = x.clone() + y.clone(); - x += y; - assert_eq!(z, 202020); - assert_eq!(z, x); - - let mut x = big_int::from(0); - let y = big_int::from(0); - let z = x.clone() + y.clone(); - x += y; - assert_eq!(z, 0); - assert_eq!(z, x); - - let mut x = big_int::from(10); - let y = big_int::from(-10); - let z = x.clone() + y.clone(); - x += y.clone(); - assert_eq!(z, 0); - assert_ne!(x, y); - - let mut x = big_int::from(11); - let y = big_int::from(10); - let z = x.clone() + y.clone(); - x += y.clone(); - assert_eq!(z, 21); - assert_ne!(x, y); - - let mut x = big_int::from(-0); - let y = big_int::from(0); - let z = x.clone() + y.clone(); - x += y.clone(); - assert_eq!(z, 0); - assert_eq!(x, y); - - let mut x = big_int::from(6); - let y = big_int::from(4); - let z = x.clone() + y.clone(); - x += y.clone(); - assert_eq!(z, 10); - assert_eq!(x, y); - - let mut x = big_int::from(-15); - let y = big_int::from(-4); - let z = x.clone() + y.clone(); - x += y.clone(); - assert_eq!(z, -19); - assert_eq!(x, y); - } - - #[test] - fn sub() { - let mut x: big_int = big_int::from(10); - let y = big_int::from(10); - let z = x.clone() - y.clone(); - x -= y; - assert_eq!(z, 0); - assert_eq!(z, x); - - let mut x = big_int::from(101010); - let y = big_int::from(10); - let z = x.clone() - y.clone(); - x -= y; - assert_eq!(z, 101000); - assert_eq!(z, x); - - let mut x = big_int::from(0); - let y = big_int::from(0); - let z = x.clone() - y.clone(); - x -= y; - assert_eq!(z, 0); - assert_eq!(z, x); - - let mut x = big_int::from(10); - let y = big_int::from(-10); - let z = x.clone() - y.clone(); - x -= y.clone(); - assert_eq!(z, 20); - assert_ne!(x, y); - - let mut x = big_int::from(-10); - let y = big_int::from(10); - let z = x.clone() - y.clone(); - x -= y.clone(); - assert_eq!(z, -20); - assert_ne!(x, y); - - let mut x = big_int::from(11); - let y = big_int::from(10); - let z = x.clone() - y.clone(); - x -= y.clone(); - assert_eq!(z, 1); - assert_ne!(x, y); - - let mut x = big_int::from(-0); - let y = big_int::from(0); - let z = x.clone() - y.clone(); - x -= y.clone(); - assert_eq!(z, 0); - assert_eq!(x, y); - - let mut x = big_int::from(6); - let y = big_int::from(4); - let z = x.clone() - y.clone(); - x -= y.clone(); - assert_eq!(z, 2); - assert_eq!(x, y); - - let mut x = big_int::from(-15); - let y = big_int::from(-4); - let z = x.clone() - y.clone(); - x -= y.clone(); - assert_eq!(z, -11); - assert_eq!(x, y); - } - - #[test] - fn mul() { - let mut x: big_int = big_int::from(10); - let y = big_int::from(10); - let z: big_int = x.clone() * y.clone(); - x *= y; - assert_eq!(z, 100); - assert_eq!(z, x); - - let mut x = big_int::from(101); - let y = big_int::from(101); - let z = x.clone() * y.clone(); - x *= y; - assert_eq!(z, 10201); - assert_eq!(z, x); - - let mut x = big_int::from(0); - let y = big_int::from(0); - let z: big_int = x.clone() * y.clone(); - x *= y; - assert_eq!(z, 0); - assert_eq!(z, x); - - let mut x = big_int::from(20100000100u64); - let y = big_int::from(0); - let z: big_int = x.clone() * y.clone(); - x *= y; - assert_eq!(z, 0); - assert_eq!(z, x); - - let mut x = big_int::from(10); - let y = big_int::from(-10); - let z: big_int = x.clone() * y.clone(); - x *= y.clone(); - assert_eq!(z, -100); - assert_ne!(x, y); - - let mut x = big_int::from(11); - let y = big_int::from(10); - let z: big_int = x.clone() * y.clone(); - x *= y.clone(); - assert_eq!(z, 110); - assert_ne!(x, y); - - let mut x = big_int::from(-0); - let y = big_int::from(0); - let z: big_int = x.clone() * y.clone(); - x *= y.clone(); - assert_eq!(z, 0); - assert_eq!(x, y); - - let mut x = big_int::from(6); - let y = big_int::from(4); - let z: big_int = x.clone() * y.clone(); - x *= y.clone(); - assert_eq!(z, 24); - assert_eq!(x, y); - - let mut x = big_int::from(-15); - let y = big_int::from(-4); - let z: big_int = x.clone() * y.clone(); - x *= y.clone(); - assert_eq!(z, 60); - assert_eq!(x, y); - } - - #[test] - fn div() { - let mut x: big_int = big_int::from(10000); - let y = big_int::from(10); - let z = x.clone() / y.clone(); - x /= y; - assert_eq!(z, 1000); - assert_eq!(z, x); - - let mut x = big_int::from(101); - let y = big_int::from(101); - let z = x.clone() / y.clone(); - x /= y; - assert_eq!(z, 1); - assert_eq!(z, x); - - let mut x = big_int::from(0); - let y = big_int::from(2); - let z = x.clone() / y.clone(); - x /= y; - assert_eq!(z, 0); - assert_eq!(z, x); - - let mut x = big_int::from(0); - let y = big_int::from(0); - let z = x.clone() / y.clone(); - x /= y; - assert_eq!(z, big_int::default()); - assert_eq!(z, x); - - let mut x = big_int::from(10000); - let y = big_int::from(0); - let z = x.clone() / y.clone(); - x /= y; - assert_eq!(z, big_int::default()); - assert_eq!(z, x); - - let mut x = big_int::from(20100000100u64); - let y = big_int::from(200); - let z = x.clone() / y.clone(); - x /= y; - assert_eq!(z, 100500); - assert_eq!(z, x); - - let mut x = big_int::from(10); - let y = big_int::from(-10); - let z = x.clone() / y.clone(); - x /= y.clone(); - assert_eq!(z, -1); - assert_ne!(x, y); - - let mut x = big_int::from(110); - let y = big_int::from(10); - let z = x.clone() / y.clone(); - x /= y.clone(); - assert_eq!(z, 11); - assert_ne!(x, y); - - let mut x = big_int::from(6); - let y = big_int::from(2); - let z = x.clone() / y.clone(); - x /= y.clone(); - assert_eq!(z, 3); - assert_eq!(x, y); - - let mut x = big_int::from(-15); - let y = big_int::from(-5); - let z = x.clone() / y.clone(); - x /= y.clone(); - assert_eq!(z, 3); - assert_eq!(x, y); - } - - #[test] - fn reminder() { - let mut x: big_int = big_int::from(10000); - let y = big_int::from(10); - let z = x.clone() % y.clone(); - x %= y; - assert_eq!(z, 0); - assert_eq!(z, x); - - let mut x: big_int = big_int::from(10); - let y = big_int::from(0); - let z = x.clone() % y.clone(); - x %= y; - assert_eq!(z, 0); - assert_eq!(z, x); - - let mut x: big_int = big_int::from(10); - let y = big_int::from(7); - let z = x.clone() % y.clone(); - x %= y; - assert_eq!(z, 3); - assert_eq!(z, x); - - let mut x: big_int = big_int::from(10000); - let y = big_int::from(10); - let z = x.clone() % y.clone(); - x %= y; - assert_eq!(z, 0); - assert_eq!(z, x); - - let mut x: big_int = big_int::from(-104); - let y = big_int::from(10); - let z = x.clone() % y.clone(); - x %= y; - assert_eq!(z, 4); - assert_eq!(z, x); - - let mut x: big_int = big_int::from(24); - let y = big_int::from(3); - let z = x.clone() % y.clone(); - x %= y; - assert_eq!(z, 0); - assert_eq!(z, x); - - let mut x: big_int = big_int::from(33); - let y = big_int::from(7); - let z = x.clone() % y.clone(); - x %= y; - assert_eq!(z, 5); - assert_eq!(z, x); - } - - #[test] - fn binary() { - let x = big_int::from(4); - assert_eq!(format!("{x:b}"), "100"); - let x = big_int::from(4); - assert_eq!(format!("{x:#b}"), "0b100"); - let x = big_int::from(4); - assert_eq!(format!("{x:010b}"), "0000000100"); - let x = big_int::from(10); - assert_eq!(format!("{x:#110b}"), "0b11111010"); - let x = big_int::from(172); - assert_eq!(format!("{x:b}"), "10101100"); - let x = big_int::from(17220003931i64); - assert_eq!(format!("{x:#b}"), "0b10000000010011001000110100001011011"); - } - - #[test] - fn bit_and() { - let mut x = big_int::from(4); //100 - let y = big_int::from(12); //1100 - let z = x.clone() & y.clone(); //0100 - x &= y; - assert_eq!(x, z); - assert_eq!(z, 4); - - let mut x = big_int::from(10); //1010 - let y = big_int::from(13); //1101 - let z = x.clone() & y.clone(); //1000 - x &= y; - assert_eq!(x, z); - assert_eq!(z, 8); - - let mut x = big_int::from(172); //10101100 - let y = big_int::from(223); //11011111 - let z = x.clone() & y.clone(); //10001100 - x &= y; - assert_eq!(x, z); - assert_eq!(z, 140); - - let mut x = big_int::from(172); //10101100 - let y = big_int::from(1); //1 - let z = x.clone() & y.clone(); //0 - x &= y; - assert_eq!(x, z); - assert_eq!(z, 0); - - let mut x = big_int::from(173); //10101101 - let y = big_int::from(1); //1 - let z = x.clone() & y.clone(); //1 - x &= y; - assert_eq!(x, z); - assert_eq!(z, 1); - } - - #[test] - fn bit_or() { - let mut x = big_int::from(4); //100 - let y = big_int::from(12); //1100 - let z = x.clone() | y.clone(); //1100 - x |= y; - assert_eq!(x, z); - assert_eq!(z, 12); - - let mut x = big_int::from(10); //1010 - let y = big_int::from(13); //1101 - let z = x.clone() | y.clone(); //1111 - x |= y; - assert_eq!(x, z); - assert_eq!(z, 15); - - let mut x = big_int::from(172); //10101100 - let y = big_int::from(223); //11011111 - let z = x.clone() | y.clone(); //11111111 - x |= y; - assert_eq!(x, z); - assert_eq!(z, 255); - - let mut x = big_int::from(172); //10101100 - let y = big_int::from(1); //1 - let z = x.clone() | y.clone(); //10101101 - x |= y; - assert_eq!(x, z); - assert_eq!(z, 173); - - let mut x = big_int::from(173); //10101101 - let y = big_int::from(1); //1 - let z = x.clone() | y.clone(); //10101101 - x |= y; - assert_eq!(x, z); - assert_eq!(z, 173); - } - - #[test] - fn bit_xor() { - let mut x = big_int::from(4); //100 - let y = big_int::from(12); //1100 - let z = x.clone() ^ y.clone(); //1000 - x ^= y; - assert_eq!(x, z); - assert_eq!(z, 8); - - let mut x = big_int::from(10); //1010 - let y = big_int::from(13); //1101 - let z = x.clone() ^ y.clone(); //0111 - x ^= y; - assert_eq!(x, z); - assert_eq!(z, 7); - - let mut x = big_int::from(172); //10101100 - let y = big_int::from(223); //11011111 - let z = x.clone() ^ y.clone(); //01110011 - x ^= y; - assert_eq!(x, z); - assert_eq!(z, 115); - - let mut x = big_int::from(172); //10101100 - let y = big_int::from(1); //1 - let z = x.clone() ^ y.clone(); //10101101 - x ^= y; - assert_eq!(x, z); - assert_eq!(z, 173); - - let mut x = big_int::from(173); //10101101 - let y = big_int::from(1); //1 - let z = x.clone() ^ y.clone(); //10101100 - x ^= y; - assert_eq!(x, z); - assert_eq!(z, 172); - } - - #[test] - fn bit_shift_left() { - let mut x = big_int::from(4); //100 - let y = big_int::from(2); - let z = x.clone() << y.clone(); //10000 - x <<= y; - assert_eq!(x, z); - assert_eq!(z, 16); - - let mut x = big_int::from(10); //1010 - let y = big_int::from(1); - let z = x.clone() << y.clone(); //10100 - x <<= y; - assert_eq!(x, z); - assert_eq!(z, 20); - - let mut x = big_int::from(172); //10101100 - let y = big_int::from(0); - let z = x.clone() << y.clone(); - x <<= y; - assert_eq!(x, z); - assert_eq!(z, 172); - - let mut x = big_int::from(172); //10101100 - let y = big_int::from(10); - let z = x.clone() << y.clone(); //101011000000000000 - x <<= y; - assert_eq!(x, z); - assert_eq!(z, 176128); - - let mut x = big_int::from(173); //10101101 - let y = big_int::from(1); - let z = x.clone() << y.clone(); //101011010 - x <<= y; - assert_eq!(x, z); - assert_eq!(z, 346); - } - - #[test] - fn bit_shift_right() { - let mut x = big_int::from(4); //100 - let y = big_int::from(2); - let z = x.clone() >> y.clone(); //1 - x >>= y; - assert_eq!(x, z); - assert_eq!(z, 1); - - let mut x = big_int::from(10); //1010 - let y = big_int::from(1); - let z = x.clone() >> y.clone(); //101 - x >>= y; - assert_eq!(x, z); - assert_eq!(z, 5); - - let mut x = big_int::from(172); //10101100 - let y = big_int::from(0); - let z = x.clone() >> y.clone(); //10101100 - x >>= y; - assert_eq!(x, z); - assert_eq!(z, 172); - - let mut x = big_int::from(172); //10101100 - let y = big_int::from(10); - let z = x.clone() >> y.clone(); //0 - x >>= y; - assert_eq!(x, z); - assert_eq!(z, 0); - - let mut x = big_int::from(173); //10101101 - let y = big_int::from(1); - let z = x.clone() >> y.clone(); //1010110 - x >>= y; - assert_eq!(x, z); - assert_eq!(z, 86); - } - - #[test] - fn hexadecimal() { - let x = big_int::from(4); - assert_eq!(format!("{x:X}"), "4"); - let x = big_int::from(16); - assert_eq!(format!("{x:#X}"), "0xF"); - let x = big_int::from(10); - assert_eq!(format!("{x:#X}"), "0x10"); - let x = big_int::from(172); - assert_eq!(format!("{x:X}"), "AC"); - let x = big_int::from(17220003931u128); - assert_eq!(format!("{x:#X}"), "0x40264685B"); - - let x = big_int::from(4); - assert_eq!(format!("{x:x}"), "4"); - let x = big_int::from(16); - assert_eq!(format!("{x:#x}"), "0xf"); - let x = big_int::from(10); - assert_eq!(format!("{x:#x}"), "0x10"); - let x = big_int::from(172); - assert_eq!(format!("{x:x}"), "ac"); - let x = big_int::from(17220003931u128); - assert_eq!(format!("{x:#x}"), "0x40264685b"); - } - - #[test] - fn progtest_tests() { - let mut a = big_int::new(); - let mut b = big_int::new(); - a = big_int::from(10); - a += big_int::from(20); - assert_eq!(a, 30); - a *= big_int::from(5); - assert_eq!(a, 150); - b = a.clone() + big_int::from(3); - assert_eq!(b, 153); - b = a.clone() * big_int::from(7); - assert_eq!(b, 1050); - assert_eq!(a, 150); - assert_eq!(format!("{a:X}"), "96"); - - a = big_int::from(10); - a += big_int::from(-20); - assert_eq!(a, -10); - a *= big_int::from(5); - assert_eq!(a, -50); - b = a.clone() + big_int::from(73); - assert_eq!(b, 23); - b = a.clone() * big_int::from(-7); - assert_eq!(b, 350); - assert_eq!(a, -50); - assert_eq!(format!("{a:X}"), "-32"); - - a = big_int::from(12345678901234567890i128); - a += big_int::from(-99999999999999999999i128); - assert_eq!(a, -87654321098765432109i128); - a *= big_int::from(54321987654321987654i128); - assert_eq!(a, "-4761556948575111126880627366067073182286"); - a *= big_int::from(0); - assert_eq!(a, 0); - a = big_int::from(10); - b = a.clone() + 400; - assert_eq!(b, "410"); - b = a.clone() * big_int::from_str("15").unwrap_or_default(); - assert_eq!(b, "150"); - assert_eq!(a, "10"); - assert_eq!(format!("{a:X}"), "A"); - - b = big_int::from_str("1234").unwrap_or_default(); - assert_eq!(format!("{b}"), "1234"); - assert!(big_int::from_str(" 12 34").is_err()); - assert!(big_int::from_str("999z").is_err()); - assert!(big_int::from_str("abcd").is_err()); - assert!(big_int::from_str("-xyz").is_err()); - assert!(big_int::from_str(":").is_err()); - assert!(big_int::from_str("%").is_err()); - assert!(big_int::from_str("- 758").is_err()); - a = big_int::from(42); - assert_eq!(a, 42); - - a = big_int::from(73786976294838206464i128); - assert_eq!(a, 73786976294838206464u128); - assert_eq!(format!("{a:X}"), "40000000000000000"); - assert!(a < "1361129467683753853853498429727072845824"); - assert!(a <= "1361129467683753853853498429727072845824"); - assert!(!(a > "1361129467683753853853498429727072845824")); - assert!(!(a >= "1361129467683753853853498429727072845824")); - assert!(!(a == "1361129467683753853853498429727072845824")); - assert!(a != "1361129467683753853853498429727072845824"); - assert!(!(a < 73786976294838206464)); - assert!(a <= 73786976294838206464); - assert!(!(a > 73786976294838206464)); - assert!(a >= 73786976294838206464); - assert!(a == 73786976294838206464u128); - assert!(!(a != 73786976294838206464i128)); - assert!(a < 73786976294838206465); - assert!(a <= 73786976294838206465); - assert!(!(a > 73786976294838206465)); - assert!(!(a >= 73786976294838206465)); - assert!(!(a == 73786976294838206465u128)); - assert!(a != 73786976294838206465i128); - a = big_int::from_str("2147483648").unwrap_or(big_int::new()); - assert!(!(a < -2147483648)); - assert!(!(a <= -2147483648)); - assert!(a > -2147483648); - assert!(a >= -2147483648); - assert!(!(a == -2147483648)); - assert!(a != -2147483648); - a = big_int::from_str("-12345678").unwrap_or(big_int::new()); - assert!(!(a < -87654321)); - assert!(!(a <= -87654321)); - assert!(a > -87654321); - assert!(a >= -87654321); - assert!(!(a == -87654321)); - assert!(a != -87654321); - } -} +mod tests; diff --git a/src/tests.rs b/src/tests.rs new file mode 100644 index 0000000..71b5da1 --- /dev/null +++ b/src/tests.rs @@ -0,0 +1,926 @@ +mod tests { + use std::str::FromStr; + + use crate::BigIntError; + use crate::big_int; + + #[test] + fn default() { + let def = big_int::default(); + assert_eq!(def.positive, true); + assert_eq!(def.numbers, [0].to_vec()); + } + + #[test] + fn new() { + let new = big_int::new(); + assert_eq!(new.positive, true); + assert_eq!(new.numbers, [0].to_vec()); + } + + #[test] + fn from() { + let x = big_int::from(20); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [2, 0].to_vec()); + let x = big_int::from(-20); + assert_eq!(x.positive, false); + assert_eq!(x.numbers, [2, 0].to_vec()); + let x = big_int::from(-320020000981234567890i128); + assert_eq!(x.positive, false); + assert_eq!( + x.numbers, + [ + 3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 + ] + .to_vec() + ); + let x = big_int::from(0); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [0].to_vec()); + let x = big_int::from(-0); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [0].to_vec()); + } + + #[test] + fn from_string_numbers() { + let x = big_int::from_str("20").unwrap(); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [2, 0].to_vec()); + let x = big_int::from_str("666").unwrap(); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [6, 6, 6].to_vec()); + let x = big_int::from_str("-20").unwrap(); + assert_eq!(x.positive, false); + assert_eq!(x.numbers, [2, 0].to_vec()); + let x = big_int::from_str("-320020000981234567890").unwrap(); + assert_eq!(x.positive, false); + assert_eq!( + x.numbers, + [ + 3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 + ] + .to_vec() + ); + let x = big_int::from_str("-0").unwrap(); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [0].to_vec()); + let x = big_int::from_str("0sw"); + assert!(x.is_err()); + let x = big_int::from_str("0 2020000"); + assert!(x.is_err()); + let x = big_int::from_str("--200"); + assert!(x.is_err()); + let x = big_int::from_str("+2000303"); + assert!(x.is_err()); + let x = big_int::from_str("minus20003002"); + assert!(x.is_err()); + } + + #[test] + fn from_string_words_from_str_digits() { + let x = big_int::from_str("two zero ").unwrap(); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [2, 0].to_vec()); + let x = big_int::from_str("minus two four").unwrap(); + assert_eq!(x.positive, false); + assert_eq!(x.numbers, [2, 4].to_vec()); + let x = big_int::from_str("two five five zero zero two one").unwrap(); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [2, 5, 5, 0, 0, 2, 1].to_vec()); + let x = big_int::from_str("minus two zero zero zero zero zero one").unwrap(); + assert_eq!(x.positive, false); + assert_eq!(x.numbers, [2, 0, 0, 0, 0, 0, 1].to_vec()); + let x = big_int::from_str("zero").unwrap(); + assert_eq!(x.positive, true); + assert_eq!(x.numbers, [0].to_vec()); + let x = big_int::from_str("onse"); + assert!(x.is_err()); + let x = big_int::from_str(" "); + assert!(x.is_err()); + let x = big_int::from_str("twenty thousand thousand"); + assert!(x.is_err()); + let x: Result = big_int::from_str("twenty thousand hundred"); + assert!(x.is_err()); + let x = big_int::from_str("- five four").unwrap(); + assert_eq!(x.positive, false); + assert_eq!(x.numbers, [5, 4].to_vec()); + } + + #[test] + fn display() { + let x = big_int::from(1003); + assert_eq!(format!("Number is: {x}"), "Number is: 1003"); + let x = big_int::from(0); + assert_eq!(format!("Number is: {x}"), "Number is: 0"); + let x = big_int::from(-100); + assert_eq!(format!("Number is: {x}"), "Number is: -100"); + let x = big_int::from(-0); + assert_eq!(format!("Number is: {x}"), "Number is: 0"); + let x = big_int::from(-1); + assert_eq!(format!("Number is: {x}"), "Number is: -1"); + let x = big_int::from(320020000981234567890i128); + assert_eq!( + format!("Number is: {x}"), + "Number is: 320020000981234567890" + ); + } + + #[test] + fn to_words() { + let x = big_int::from(20); + assert_eq!(x.to_words(), "two zero"); + let x = big_int::from(-24); + assert_eq!(x.to_words(), "minus two four"); + let x = big_int::from(2550021); + assert_eq!(x.to_words(), "two five five zero zero two one"); + let x = big_int::from(-2000000000001i128); + assert_eq!( + x.to_words(), + "minus two zero zero zero zero zero zero zero zero zero zero zero one" + ); + } + + #[test] + fn not() { + let mut x = big_int::from(1); + assert_eq!(x.positive, true); + assert_eq!(x.to_string(), "1"); + x = -x; + assert_eq!(x.positive, false); + assert_eq!(x.to_string(), "-1"); + x = -x; + assert_eq!(x.positive, true); + assert_eq!(x.to_string(), "1"); + let mut x = big_int::from(-22); + assert_eq!(x.positive, false); + assert_eq!(x.to_string(), "-22"); + x = -x; + assert_eq!(x.positive, true); + assert_eq!(x.to_string(), "22"); + } + + #[test] + fn equal() { + let x = big_int::from(10); + let y = big_int::from(10); + assert!(x == y); + let x = big_int::from(101010); + let y = big_int::from(101010); + assert!(x == y); + let x = big_int::from(101010); + let y = big_int::from(101210); + assert!(!(x == y)); + let x = big_int::from(101010); + let y = big_int::from(1); + assert!(!(x == y)); + let x = big_int::from(0); + let y = big_int::from(0); + assert!(x == y); + let x = big_int::from(10); + let y = big_int::from(-10); + assert!(x != y); + let x = big_int::from(11); + let y = big_int::from(10); + assert!(x != y); + let x = big_int::from(-0); + let y = big_int::from(0); + assert!(x == y); + } + + #[test] + fn equal_int() { + let x = big_int::from(10); + assert!(x == 10); + let x = big_int::from(101010); + assert!(x == 101010); + let x = big_int::from(101010); + assert!(!(x == 101210)); + let x = big_int::from(101010); + assert!(!(x == 1)); + let x = big_int::from(0); + assert!(x == 0); + let x = big_int::from(10); + assert!(x != -10); + let x = big_int::from(11); + assert!(x != 10); + let x = big_int::from(-0); + assert!(x == 0); + } + + #[test] + fn equal_str() { + let x = big_int::from(10); + assert!(x == "10"); + let x = big_int::from(101010); + assert!(x == "101010"); + let x = big_int::from(101010); + assert!(!(x == "101210")); + let x = big_int::from(101010); + assert!(!(x == "1")); + let x = big_int::from(0); + assert!(x == "0"); + let x = big_int::from(10); + assert!(x != "-10"); + let x = big_int::from(11); + assert!(x != "10"); + let x = big_int::from(-0); + assert!(x == "0"); + } + + #[test] + fn greater() { + let x: big_int = big_int::from(15); + let y = big_int::from(10); + assert!(x > y); + let x: big_int = big_int::from(8); + let y = big_int::from(7); + assert!(x > y); + let x = big_int::from(10); + let y = big_int::from(10); + assert!(!(x > y)); + let x = big_int::from(10); + let y = big_int::from(10); + assert!(x >= y); + let x = big_int::from(101010); + let y = big_int::from(101010); + assert!(x >= y); + let x = big_int::from(0); + let y = big_int::from(0); + assert!(!(x > y)); + let x = big_int::from(10); + let y = big_int::from(-10); + assert!(x > y); + let x = big_int::from(11); + let y = big_int::from(10); + assert!(x > y); + } + + #[test] + fn lesser() { + let x: big_int = big_int::from(0); + let y = big_int::from(10); + assert!(x < y); + let x: big_int = big_int::from(8); + let y = big_int::from(9); + assert!(x < y); + let x = big_int::from(10); + let y = big_int::from(10); + assert!(!(x < y)); + let x = big_int::from(10); + let y = big_int::from(10); + assert!(x <= y); + let x = big_int::from(99999999999i64); + let y = big_int::from(99999999999i128); + assert!(x <= y); + let x = big_int::from(0); + let y = big_int::from(0); + assert!(!(x < y)); + let x = big_int::from(-10); + let y = big_int::from(10); + assert!(x < y); + let x = big_int::from(11); + let y = big_int::from(99999999999u64); + assert!(x < y); + } + + #[test] + fn add() { + let mut x: big_int = big_int::from(10); + let y = big_int::from(10); + let z = x.clone() + y.clone(); + x += y; + assert_eq!(z, 20); + assert_eq!(z, x); + + let mut x = big_int::from(101010); + let y = big_int::from(101010); + let z = x.clone() + y.clone(); + x += y; + assert_eq!(z, 202020); + assert_eq!(z, x); + + let mut x = big_int::from(0); + let y = big_int::from(0); + let z = x.clone() + y.clone(); + x += y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x = big_int::from(10); + let y = big_int::from(-10); + let z = x.clone() + y.clone(); + x += y.clone(); + assert_eq!(z, 0); + assert_ne!(x, y); + + let mut x = big_int::from(11); + let y = big_int::from(10); + let z = x.clone() + y.clone(); + x += y.clone(); + assert_eq!(z, 21); + assert_ne!(x, y); + + let mut x = big_int::from(-0); + let y = big_int::from(0); + let z = x.clone() + y.clone(); + x += y.clone(); + assert_eq!(z, 0); + assert_eq!(x, y); + + let mut x = big_int::from(6); + let y = big_int::from(4); + let z = x.clone() + y.clone(); + x += y.clone(); + assert_eq!(z, 10); + assert_eq!(x, y); + + let mut x = big_int::from(-15); + let y = big_int::from(-4); + let z = x.clone() + y.clone(); + x += y.clone(); + assert_eq!(z, -19); + assert_eq!(x, y); + } + + #[test] + fn sub() { + let mut x: big_int = big_int::from(10); + let y = big_int::from(10); + let z = x.clone() - y.clone(); + x -= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x = big_int::from(101010); + let y = big_int::from(10); + let z = x.clone() - y.clone(); + x -= y; + assert_eq!(z, 101000); + assert_eq!(z, x); + + let mut x = big_int::from(0); + let y = big_int::from(0); + let z = x.clone() - y.clone(); + x -= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x = big_int::from(10); + let y = big_int::from(-10); + let z = x.clone() - y.clone(); + x -= y.clone(); + assert_eq!(z, 20); + assert_ne!(x, y); + + let mut x = big_int::from(-10); + let y = big_int::from(10); + let z = x.clone() - y.clone(); + x -= y.clone(); + assert_eq!(z, -20); + assert_ne!(x, y); + + let mut x = big_int::from(11); + let y = big_int::from(10); + let z = x.clone() - y.clone(); + x -= y.clone(); + assert_eq!(z, 1); + assert_ne!(x, y); + + let mut x = big_int::from(-0); + let y = big_int::from(0); + let z = x.clone() - y.clone(); + x -= y.clone(); + assert_eq!(z, 0); + assert_eq!(x, y); + + let mut x = big_int::from(6); + let y = big_int::from(4); + let z = x.clone() - y.clone(); + x -= y.clone(); + assert_eq!(z, 2); + assert_eq!(x, y); + + let mut x = big_int::from(-15); + let y = big_int::from(-4); + let z = x.clone() - y.clone(); + x -= y.clone(); + assert_eq!(z, -11); + assert_eq!(x, y); + } + + #[test] + fn mul() { + let mut x: big_int = big_int::from(10); + let y = big_int::from(10); + let z: big_int = x.clone() * y.clone(); + x *= y; + assert_eq!(z, 100); + assert_eq!(z, x); + + let mut x = big_int::from(101); + let y = big_int::from(101); + let z = x.clone() * y.clone(); + x *= y; + assert_eq!(z, 10201); + assert_eq!(z, x); + + let mut x = big_int::from(0); + let y = big_int::from(0); + let z: big_int = x.clone() * y.clone(); + x *= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x = big_int::from(20100000100u64); + let y = big_int::from(0); + let z: big_int = x.clone() * y.clone(); + x *= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x = big_int::from(10); + let y = big_int::from(-10); + let z: big_int = x.clone() * y.clone(); + x *= y.clone(); + assert_eq!(z, -100); + assert_ne!(x, y); + + let mut x = big_int::from(11); + let y = big_int::from(10); + let z: big_int = x.clone() * y.clone(); + x *= y.clone(); + assert_eq!(z, 110); + assert_ne!(x, y); + + let mut x = big_int::from(-0); + let y = big_int::from(0); + let z: big_int = x.clone() * y.clone(); + x *= y.clone(); + assert_eq!(z, 0); + assert_eq!(x, y); + + let mut x = big_int::from(6); + let y = big_int::from(4); + let z: big_int = x.clone() * y.clone(); + x *= y.clone(); + assert_eq!(z, 24); + assert_eq!(x, y); + + let mut x = big_int::from(-15); + let y = big_int::from(-4); + let z: big_int = x.clone() * y.clone(); + x *= y.clone(); + assert_eq!(z, 60); + assert_eq!(x, y); + } + + #[test] + fn div() { + let mut x: big_int = big_int::from(10000); + let y = big_int::from(10); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!(z, 1000); + assert_eq!(z, x); + + let mut x = big_int::from(101); + let y = big_int::from(101); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!(z, 1); + assert_eq!(z, x); + + let mut x = big_int::from(0); + let y = big_int::from(2); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x = big_int::from(0); + let y = big_int::from(0); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!(z, big_int::default()); + assert_eq!(z, x); + + let mut x = big_int::from(10000); + let y = big_int::from(0); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!(z, big_int::default()); + assert_eq!(z, x); + + let mut x = big_int::from(20100000100u64); + let y = big_int::from(200); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!(z, 100500); + assert_eq!(z, x); + + let mut x = big_int::from(10); + let y = big_int::from(-10); + let z = x.clone() / y.clone(); + x /= y.clone(); + assert_eq!(z, -1); + assert_ne!(x, y); + + let mut x = big_int::from(110); + let y = big_int::from(10); + let z = x.clone() / y.clone(); + x /= y.clone(); + assert_eq!(z, 11); + assert_ne!(x, y); + + let mut x = big_int::from(6); + let y = big_int::from(2); + let z = x.clone() / y.clone(); + x /= y.clone(); + assert_eq!(z, 3); + assert_eq!(x, y); + + let mut x = big_int::from(-15); + let y = big_int::from(-5); + let z = x.clone() / y.clone(); + x /= y.clone(); + assert_eq!(z, 3); + assert_eq!(x, y); + } + + #[test] + fn reminder() { + let mut x: big_int = big_int::from(10000); + let y = big_int::from(10); + let z = x.clone() % y.clone(); + x %= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x: big_int = big_int::from(10); + let y = big_int::from(0); + let z = x.clone() % y.clone(); + x %= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x: big_int = big_int::from(10); + let y = big_int::from(7); + let z = x.clone() % y.clone(); + x %= y; + assert_eq!(z, 3); + assert_eq!(z, x); + + let mut x: big_int = big_int::from(10000); + let y = big_int::from(10); + let z = x.clone() % y.clone(); + x %= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x: big_int = big_int::from(-104); + let y = big_int::from(10); + let z = x.clone() % y.clone(); + x %= y; + assert_eq!(z, 4); + assert_eq!(z, x); + + let mut x: big_int = big_int::from(24); + let y = big_int::from(3); + let z = x.clone() % y.clone(); + x %= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x: big_int = big_int::from(33); + let y = big_int::from(7); + let z = x.clone() % y.clone(); + x %= y; + assert_eq!(z, 5); + assert_eq!(z, x); + } + + #[test] + fn binary() { + let x = big_int::from(4); + assert_eq!(format!("{x:b}"), "100"); + let x = big_int::from(4); + assert_eq!(format!("{x:#b}"), "0b100"); + let x = big_int::from(4); + assert_eq!(format!("{x:010b}"), "0000000100"); + let x = big_int::from(10); + assert_eq!(format!("{x:#110b}"), "0b11111010"); + let x = big_int::from(172); + assert_eq!(format!("{x:b}"), "10101100"); + let x = big_int::from(17220003931i64); + assert_eq!(format!("{x:#b}"), "0b10000000010011001000110100001011011"); + } + + #[test] + fn bit_and() { + let mut x = big_int::from(4); //100 + let y = big_int::from(12); //1100 + let z = x.clone() & y.clone(); //0100 + x &= y; + assert_eq!(x, z); + assert_eq!(z, 4); + + let mut x = big_int::from(10); //1010 + let y = big_int::from(13); //1101 + let z = x.clone() & y.clone(); //1000 + x &= y; + assert_eq!(x, z); + assert_eq!(z, 8); + + let mut x = big_int::from(172); //10101100 + let y = big_int::from(223); //11011111 + let z = x.clone() & y.clone(); //10001100 + x &= y; + assert_eq!(x, z); + assert_eq!(z, 140); + + let mut x = big_int::from(172); //10101100 + let y = big_int::from(1); //1 + let z = x.clone() & y.clone(); //0 + x &= y; + assert_eq!(x, z); + assert_eq!(z, 0); + + let mut x = big_int::from(173); //10101101 + let y = big_int::from(1); //1 + let z = x.clone() & y.clone(); //1 + x &= y; + assert_eq!(x, z); + assert_eq!(z, 1); + } + + #[test] + fn bit_or() { + let mut x = big_int::from(4); //100 + let y = big_int::from(12); //1100 + let z = x.clone() | y.clone(); //1100 + x |= y; + assert_eq!(x, z); + assert_eq!(z, 12); + + let mut x = big_int::from(10); //1010 + let y = big_int::from(13); //1101 + let z = x.clone() | y.clone(); //1111 + x |= y; + assert_eq!(x, z); + assert_eq!(z, 15); + + let mut x = big_int::from(172); //10101100 + let y = big_int::from(223); //11011111 + let z = x.clone() | y.clone(); //11111111 + x |= y; + assert_eq!(x, z); + assert_eq!(z, 255); + + let mut x = big_int::from(172); //10101100 + let y = big_int::from(1); //1 + let z = x.clone() | y.clone(); //10101101 + x |= y; + assert_eq!(x, z); + assert_eq!(z, 173); + + let mut x = big_int::from(173); //10101101 + let y = big_int::from(1); //1 + let z = x.clone() | y.clone(); //10101101 + x |= y; + assert_eq!(x, z); + assert_eq!(z, 173); + } + + #[test] + fn bit_xor() { + let mut x = big_int::from(4); //100 + let y = big_int::from(12); //1100 + let z = x.clone() ^ y.clone(); //1000 + x ^= y; + assert_eq!(x, z); + assert_eq!(z, 8); + + let mut x = big_int::from(10); //1010 + let y = big_int::from(13); //1101 + let z = x.clone() ^ y.clone(); //0111 + x ^= y; + assert_eq!(x, z); + assert_eq!(z, 7); + + let mut x = big_int::from(172); //10101100 + let y = big_int::from(223); //11011111 + let z = x.clone() ^ y.clone(); //01110011 + x ^= y; + assert_eq!(x, z); + assert_eq!(z, 115); + + let mut x = big_int::from(172); //10101100 + let y = big_int::from(1); //1 + let z = x.clone() ^ y.clone(); //10101101 + x ^= y; + assert_eq!(x, z); + assert_eq!(z, 173); + + let mut x = big_int::from(173); //10101101 + let y = big_int::from(1); //1 + let z = x.clone() ^ y.clone(); //10101100 + x ^= y; + assert_eq!(x, z); + assert_eq!(z, 172); + } + + #[test] + fn bit_shift_left() { + let mut x = big_int::from(4); //100 + let y = big_int::from(2); + let z = x.clone() << y.clone(); //10000 + x <<= y; + assert_eq!(x, z); + assert_eq!(z, 16); + + let mut x = big_int::from(10); //1010 + let y = big_int::from(1); + let z = x.clone() << y.clone(); //10100 + x <<= y; + assert_eq!(x, z); + assert_eq!(z, 20); + + let mut x = big_int::from(172); //10101100 + let y = big_int::from(0); + let z = x.clone() << y.clone(); + x <<= y; + assert_eq!(x, z); + assert_eq!(z, 172); + + let mut x = big_int::from(172); //10101100 + let y = big_int::from(10); + let z = x.clone() << y.clone(); //101011000000000000 + x <<= y; + assert_eq!(x, z); + assert_eq!(z, 176128); + + let mut x = big_int::from(173); //10101101 + let y = big_int::from(1); + let z = x.clone() << y.clone(); //101011010 + x <<= y; + assert_eq!(x, z); + assert_eq!(z, 346); + } + + #[test] + fn bit_shift_right() { + let mut x = big_int::from(4); //100 + let y = big_int::from(2); + let z = x.clone() >> y.clone(); //1 + x >>= y; + assert_eq!(x, z); + assert_eq!(z, 1); + + let mut x = big_int::from(10); //1010 + let y = big_int::from(1); + let z = x.clone() >> y.clone(); //101 + x >>= y; + assert_eq!(x, z); + assert_eq!(z, 5); + + let mut x = big_int::from(172); //10101100 + let y = big_int::from(0); + let z = x.clone() >> y.clone(); //10101100 + x >>= y; + assert_eq!(x, z); + assert_eq!(z, 172); + + let mut x = big_int::from(172); //10101100 + let y = big_int::from(10); + let z = x.clone() >> y.clone(); //0 + x >>= y; + assert_eq!(x, z); + assert_eq!(z, 0); + + let mut x = big_int::from(173); //10101101 + let y = big_int::from(1); + let z = x.clone() >> y.clone(); //1010110 + x >>= y; + assert_eq!(x, z); + assert_eq!(z, 86); + } + + #[test] + fn hexadecimal() { + let x = big_int::from(4); + assert_eq!(format!("{x:X}"), "4"); + let x = big_int::from(16); + assert_eq!(format!("{x:#X}"), "0xF"); + let x = big_int::from(10); + assert_eq!(format!("{x:#X}"), "0x10"); + let x = big_int::from(172); + assert_eq!(format!("{x:X}"), "AC"); + let x = big_int::from(17220003931u128); + assert_eq!(format!("{x:#X}"), "0x40264685B"); + + let x = big_int::from(4); + assert_eq!(format!("{x:x}"), "4"); + let x = big_int::from(16); + assert_eq!(format!("{x:#x}"), "0xf"); + let x = big_int::from(10); + assert_eq!(format!("{x:#x}"), "0x10"); + let x = big_int::from(172); + assert_eq!(format!("{x:x}"), "ac"); + let x = big_int::from(17220003931u128); + assert_eq!(format!("{x:#x}"), "0x40264685b"); + } + + #[test] + fn progtest_tests() { + let mut a = big_int::new(); + let mut b = big_int::new(); + a = big_int::from(10); + a += big_int::from(20); + assert_eq!(a, 30); + a *= big_int::from(5); + assert_eq!(a, 150); + b = a.clone() + big_int::from(3); + assert_eq!(b, 153); + b = a.clone() * big_int::from(7); + assert_eq!(b, 1050); + assert_eq!(a, 150); + assert_eq!(format!("{a:X}"), "96"); + + a = big_int::from(10); + a += big_int::from(-20); + assert_eq!(a, -10); + a *= big_int::from(5); + assert_eq!(a, -50); + b = a.clone() + big_int::from(73); + assert_eq!(b, 23); + b = a.clone() * big_int::from(-7); + assert_eq!(b, 350); + assert_eq!(a, -50); + assert_eq!(format!("{a:X}"), "-32"); + + a = big_int::from(12345678901234567890i128); + a += big_int::from(-99999999999999999999i128); + assert_eq!(a, -87654321098765432109i128); + a *= big_int::from(54321987654321987654i128); + assert_eq!(a, "-4761556948575111126880627366067073182286"); + a *= big_int::from(0); + assert_eq!(a, 0); + a = big_int::from(10); + b = a.clone() + 400; + assert_eq!(b, "410"); + b = a.clone() * big_int::from_str("15").unwrap_or_default(); + assert_eq!(b, "150"); + assert_eq!(a, "10"); + assert_eq!(format!("{a:X}"), "A"); + + b = big_int::from_str("1234").unwrap_or_default(); + assert_eq!(format!("{b}"), "1234"); + assert!(big_int::from_str(" 12 34").is_err()); + assert!(big_int::from_str("999z").is_err()); + assert!(big_int::from_str("abcd").is_err()); + assert!(big_int::from_str("-xyz").is_err()); + assert!(big_int::from_str(":").is_err()); + assert!(big_int::from_str("%").is_err()); + assert!(big_int::from_str("- 758").is_err()); + a = big_int::from(42); + assert_eq!(a, 42); + + a = big_int::from(73786976294838206464i128); + assert_eq!(a, 73786976294838206464u128); + assert_eq!(format!("{a:X}"), "40000000000000000"); + assert!(a < "1361129467683753853853498429727072845824"); + assert!(a <= "1361129467683753853853498429727072845824"); + assert!(!(a > "1361129467683753853853498429727072845824")); + assert!(!(a >= "1361129467683753853853498429727072845824")); + assert!(!(a == "1361129467683753853853498429727072845824")); + assert!(a != "1361129467683753853853498429727072845824"); + assert!(!(a < 73786976294838206464)); + assert!(a <= 73786976294838206464); + assert!(!(a > 73786976294838206464)); + assert!(a >= 73786976294838206464); + assert!(a == 73786976294838206464u128); + assert!(!(a != 73786976294838206464i128)); + assert!(a < 73786976294838206465); + assert!(a <= 73786976294838206465); + assert!(!(a > 73786976294838206465)); + assert!(!(a >= 73786976294838206465)); + assert!(!(a == 73786976294838206465u128)); + assert!(a != 73786976294838206465i128); + a = big_int::from_str("2147483648").unwrap_or(big_int::new()); + assert!(!(a < -2147483648)); + assert!(!(a <= -2147483648)); + assert!(a > -2147483648); + assert!(a >= -2147483648); + assert!(!(a == -2147483648)); + assert!(a != -2147483648); + a = big_int::from_str("-12345678").unwrap_or(big_int::new()); + assert!(!(a < -87654321)); + assert!(!(a <= -87654321)); + assert!(a > -87654321); + assert!(a >= -87654321); + assert!(!(a == -87654321)); + assert!(a != -87654321); + } +} From a7d7d3a0b51712655e2b3cb8154670de135d542b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 16 Jul 2025 00:10:41 +0200 Subject: [PATCH 083/243] rename of big_int --> BigInt --- src/lib.rs | 148 ++++++------- src/tests.rs | 592 +++++++++++++++++++++++++-------------------------- 2 files changed, 370 insertions(+), 370 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4bf4bf6..eed7d28 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,21 +16,21 @@ enum BigIntError { } #[derive(Clone, Eq, Debug)] -struct big_int { +struct BigInt { positive: bool, numbers: Vec, } -impl Default for big_int { +impl Default for BigInt { fn default() -> Self { - big_int { + BigInt { positive: true, numbers: vec![0], } } } -impl FromStr for big_int { +impl FromStr for BigInt { type Err = BigIntError; fn from_str(mut string_of_numbers: &str) -> Result { //empty string edgecaase @@ -48,7 +48,7 @@ impl FromStr for big_int { for char in string_of_numbers.chars() { if !char.is_ascii_digit() { - return big_int::parse_word_digits(if positive { + return BigInt::parse_word_digits(if positive { string_of_numbers.to_string() } else { format!("-{}", string_of_numbers) @@ -62,19 +62,19 @@ impl FromStr for big_int { positive = true; } - Ok(big_int { positive, numbers }) + Ok(BigInt { positive, numbers }) } } macro_rules! from_int { ($($t:ty),*)=>{ $( - impl From<$t> for big_int{ + impl From<$t> for BigInt{ fn from(mut original_number: $t) -> Self { //zero edgecase if original_number == 0 { - return big_int::default(); + return BigInt::default(); } let mut numbers = Vec::new(); @@ -92,7 +92,7 @@ macro_rules! from_int { } //return value - big_int {positive, numbers} + BigInt {positive, numbers} } } @@ -103,12 +103,12 @@ macro_rules! from_int { macro_rules! from_uint { ($($t:ty),*)=>{ $( - impl From<$t> for big_int{ + impl From<$t> for BigInt{ fn from(mut original_number: $t) -> Self { //zero edgecase if original_number == 0 { - return big_int::default(); + return BigInt::default(); } let mut numbers = Vec::new(); @@ -120,7 +120,7 @@ macro_rules! from_uint { } //return value - big_int { + BigInt { positive: true, numbers} } @@ -132,7 +132,7 @@ macro_rules! from_uint { from_int!(i8, i16, i32, i64, i128); from_uint!(u8, u16, u32, u64, u128); -impl Display for big_int { +impl Display for BigInt { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { if !self.positive { write!(f, "-")?; @@ -145,19 +145,19 @@ impl Display for big_int { } } -impl Binary for big_int { +impl Binary for BigInt { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { todo!() } } -impl UpperHex for big_int { +impl UpperHex for BigInt { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { todo!() } } -impl LowerHex for big_int { +impl LowerHex for BigInt { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { todo!() } @@ -166,9 +166,9 @@ impl LowerHex for big_int { macro_rules! eq_with_int { ($($t:ty),*) => { $( - impl PartialEq<$t> for big_int{ + impl PartialEq<$t> for BigInt{ fn eq(&self, other: &$t) -> bool { - self == &big_int::from(*other) + self == &BigInt::from(*other) } } )* @@ -177,9 +177,9 @@ macro_rules! eq_with_int { eq_with_int!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128); -impl PartialEq<&str> for big_int { +impl PartialEq<&str> for BigInt { fn eq(&self, other: &&str) -> bool { - let right_side = big_int::from_str(other); + let right_side = BigInt::from_str(other); if right_side.is_err() { return false; } @@ -187,8 +187,8 @@ impl PartialEq<&str> for big_int { } } -impl PartialEq for big_int { - fn eq(&self, other: &big_int) -> bool { +impl PartialEq for BigInt { + fn eq(&self, other: &BigInt) -> bool { if self.positive != other.positive { false } else { @@ -197,7 +197,7 @@ impl PartialEq for big_int { } } -impl Neg for big_int { +impl Neg for BigInt { type Output = Self; fn neg(mut self) -> Self::Output { self.positive = !self.positive; @@ -205,7 +205,7 @@ impl Neg for big_int { } } -impl PartialOrd for big_int { +impl PartialOrd for BigInt { fn ge(&self, other: &Self) -> bool { todo!() } @@ -223,7 +223,7 @@ impl PartialOrd for big_int { } } -impl PartialOrd for big_int { +impl PartialOrd for BigInt { fn ge(&self, other: &i128) -> bool { todo!() } @@ -241,7 +241,7 @@ impl PartialOrd for big_int { } } -impl PartialOrd<&str> for big_int { +impl PartialOrd<&str> for BigInt { fn ge(&self, other: &&str) -> bool { todo!() } @@ -259,9 +259,9 @@ impl PartialOrd<&str> for big_int { } } -impl Add for big_int +impl Add for BigInt where - T: Into, + T: Into, { type Output = Self; fn add(self, rhs: T) -> Self::Output { @@ -269,18 +269,18 @@ where } } -impl AddAssign for big_int +impl AddAssign for BigInt where - T: Into, + T: Into, { fn add_assign(&mut self, rhs: T) { todo!() } } -impl Sub for big_int +impl Sub for BigInt where - T: Into, + T: Into, { type Output = Self; @@ -289,18 +289,18 @@ where } } -impl SubAssign for big_int +impl SubAssign for BigInt where - T: Into, + T: Into, { fn sub_assign(&mut self, rhs: T) { todo!() } } -impl Mul for big_int +impl Mul for BigInt where - T: Into, + T: Into, { type Output = Self; fn mul(self, rhs: T) -> Self::Output { @@ -308,18 +308,18 @@ where } } -impl MulAssign for big_int +impl MulAssign for BigInt where - T: Into, + T: Into, { fn mul_assign(&mut self, rhs: T) { todo!() } } -impl Div for big_int +impl Div for BigInt where - T: Into, + T: Into, { type Output = Self; fn div(self, rhs: T) -> Self::Output { @@ -327,18 +327,18 @@ where } } -impl DivAssign for big_int +impl DivAssign for BigInt where - T: Into, + T: Into, { fn div_assign(&mut self, rhs: T) { todo!() } } -impl Rem for big_int +impl Rem for BigInt where - T: Into, + T: Into, { type Output = Self; fn rem(self, rhs: T) -> Self::Output { @@ -346,18 +346,18 @@ where } } -impl RemAssign for big_int +impl RemAssign for BigInt where - T: Into, + T: Into, { fn rem_assign(&mut self, rhs: T) { todo!() } } -impl BitAnd for big_int +impl BitAnd for BigInt where - T: Into, + T: Into, { type Output = Self; fn bitand(self, rhs: T) -> Self::Output { @@ -365,18 +365,18 @@ where } } -impl BitAndAssign for big_int +impl BitAndAssign for BigInt where - T: Into, + T: Into, { fn bitand_assign(&mut self, rhs: T) { todo!() } } -impl BitOr for big_int +impl BitOr for BigInt where - T: Into, + T: Into, { type Output = Self; fn bitor(self, rhs: T) -> Self::Output { @@ -384,18 +384,18 @@ where } } -impl BitOrAssign for big_int +impl BitOrAssign for BigInt where - T: Into, + T: Into, { fn bitor_assign(&mut self, rhs: T) { todo!() } } -impl BitXor for big_int +impl BitXor for BigInt where - T: Into, + T: Into, { type Output = Self; fn bitxor(self, rhs: T) -> Self::Output { @@ -403,18 +403,18 @@ where } } -impl BitXorAssign for big_int +impl BitXorAssign for BigInt where - T: Into, + T: Into, { fn bitxor_assign(&mut self, rhs: T) { todo!() } } -impl Shl for big_int +impl Shl for BigInt where - T: Into, + T: Into, { type Output = Self; fn shl(self, rhs: T) -> Self::Output { @@ -422,18 +422,18 @@ where } } -impl ShlAssign for big_int +impl ShlAssign for BigInt where - T: Into, + T: Into, { fn shl_assign(&mut self, rhs: T) { todo!() } } -impl Shr for big_int +impl Shr for BigInt where - T: Into, + T: Into, { type Output = Self; fn shr(self, rhs: T) -> Self::Output { @@ -441,18 +441,18 @@ where } } -impl ShrAssign for big_int +impl ShrAssign for BigInt where - T: Into, + T: Into, { fn shr_assign(&mut self, rhs: T) { todo!() } } -impl big_int { - pub fn new() -> big_int { - big_int::default() +impl BigInt { + pub fn new() -> BigInt { + BigInt::default() } pub fn to_words(&self) -> String { @@ -463,12 +463,12 @@ impl big_int { if !self.positive { fin_str = "minus".to_string(); } else { - fin_str = big_int::number_to_word(*nmr_iter.next().unwrap_or(&0)); + fin_str = BigInt::number_to_word(*nmr_iter.next().unwrap_or(&0)); } //print all digits for num in nmr_iter { - fin_str = format!("{} {}", fin_str, big_int::number_to_word(*num)); + fin_str = format!("{} {}", fin_str, BigInt::number_to_word(*num)); } fin_str @@ -507,7 +507,7 @@ impl big_int { .to_string() } - fn parse_word_digits(string_of_numbers: String) -> Result { + fn parse_word_digits(string_of_numbers: String) -> Result { //create lowercase iterator let mut parsed = string_of_numbers .split_whitespace() @@ -529,7 +529,7 @@ impl big_int { //loop for translating words to u8 for word in parsed { - numbers.push(big_int::word_to_number(&word)?); + numbers.push(BigInt::word_to_number(&word)?); } //additional check @@ -537,7 +537,7 @@ impl big_int { return Err(BigIntError::NaN); } - Ok(big_int { positive, numbers }) + Ok(BigInt { positive, numbers }) } } diff --git a/src/tests.rs b/src/tests.rs index 71b5da1..77c872e 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -2,31 +2,31 @@ mod tests { use std::str::FromStr; use crate::BigIntError; - use crate::big_int; + use crate::BigInt; #[test] fn default() { - let def = big_int::default(); + let def = BigInt::default(); assert_eq!(def.positive, true); assert_eq!(def.numbers, [0].to_vec()); } #[test] fn new() { - let new = big_int::new(); + let new = BigInt::new(); assert_eq!(new.positive, true); assert_eq!(new.numbers, [0].to_vec()); } #[test] fn from() { - let x = big_int::from(20); + let x = BigInt::from(20); assert_eq!(x.positive, true); assert_eq!(x.numbers, [2, 0].to_vec()); - let x = big_int::from(-20); + let x = BigInt::from(-20); assert_eq!(x.positive, false); assert_eq!(x.numbers, [2, 0].to_vec()); - let x = big_int::from(-320020000981234567890i128); + let x = BigInt::from(-320020000981234567890i128); assert_eq!(x.positive, false); assert_eq!( x.numbers, @@ -35,26 +35,26 @@ mod tests { ] .to_vec() ); - let x = big_int::from(0); + let x = BigInt::from(0); assert_eq!(x.positive, true); assert_eq!(x.numbers, [0].to_vec()); - let x = big_int::from(-0); + let x = BigInt::from(-0); assert_eq!(x.positive, true); assert_eq!(x.numbers, [0].to_vec()); } #[test] fn from_string_numbers() { - let x = big_int::from_str("20").unwrap(); + let x = BigInt::from_str("20").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [2, 0].to_vec()); - let x = big_int::from_str("666").unwrap(); + let x = BigInt::from_str("666").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [6, 6, 6].to_vec()); - let x = big_int::from_str("-20").unwrap(); + let x = BigInt::from_str("-20").unwrap(); assert_eq!(x.positive, false); assert_eq!(x.numbers, [2, 0].to_vec()); - let x = big_int::from_str("-320020000981234567890").unwrap(); + let x = BigInt::from_str("-320020000981234567890").unwrap(); assert_eq!(x.positive, false); assert_eq!( x.numbers, @@ -63,64 +63,64 @@ mod tests { ] .to_vec() ); - let x = big_int::from_str("-0").unwrap(); + let x = BigInt::from_str("-0").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [0].to_vec()); - let x = big_int::from_str("0sw"); + let x = BigInt::from_str("0sw"); assert!(x.is_err()); - let x = big_int::from_str("0 2020000"); + let x = BigInt::from_str("0 2020000"); assert!(x.is_err()); - let x = big_int::from_str("--200"); + let x = BigInt::from_str("--200"); assert!(x.is_err()); - let x = big_int::from_str("+2000303"); + let x = BigInt::from_str("+2000303"); assert!(x.is_err()); - let x = big_int::from_str("minus20003002"); + let x = BigInt::from_str("minus20003002"); assert!(x.is_err()); } #[test] fn from_string_words_from_str_digits() { - let x = big_int::from_str("two zero ").unwrap(); + let x = BigInt::from_str("two zero ").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [2, 0].to_vec()); - let x = big_int::from_str("minus two four").unwrap(); + let x = BigInt::from_str("minus two four").unwrap(); assert_eq!(x.positive, false); assert_eq!(x.numbers, [2, 4].to_vec()); - let x = big_int::from_str("two five five zero zero two one").unwrap(); + let x = BigInt::from_str("two five five zero zero two one").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [2, 5, 5, 0, 0, 2, 1].to_vec()); - let x = big_int::from_str("minus two zero zero zero zero zero one").unwrap(); + let x = BigInt::from_str("minus two zero zero zero zero zero one").unwrap(); assert_eq!(x.positive, false); assert_eq!(x.numbers, [2, 0, 0, 0, 0, 0, 1].to_vec()); - let x = big_int::from_str("zero").unwrap(); + let x = BigInt::from_str("zero").unwrap(); assert_eq!(x.positive, true); assert_eq!(x.numbers, [0].to_vec()); - let x = big_int::from_str("onse"); + let x = BigInt::from_str("onse"); assert!(x.is_err()); - let x = big_int::from_str(" "); + let x = BigInt::from_str(" "); assert!(x.is_err()); - let x = big_int::from_str("twenty thousand thousand"); + let x = BigInt::from_str("twenty thousand thousand"); assert!(x.is_err()); - let x: Result = big_int::from_str("twenty thousand hundred"); + let x: Result = BigInt::from_str("twenty thousand hundred"); assert!(x.is_err()); - let x = big_int::from_str("- five four").unwrap(); + let x = BigInt::from_str("- five four").unwrap(); assert_eq!(x.positive, false); assert_eq!(x.numbers, [5, 4].to_vec()); } #[test] fn display() { - let x = big_int::from(1003); + let x = BigInt::from(1003); assert_eq!(format!("Number is: {x}"), "Number is: 1003"); - let x = big_int::from(0); + let x = BigInt::from(0); assert_eq!(format!("Number is: {x}"), "Number is: 0"); - let x = big_int::from(-100); + let x = BigInt::from(-100); assert_eq!(format!("Number is: {x}"), "Number is: -100"); - let x = big_int::from(-0); + let x = BigInt::from(-0); assert_eq!(format!("Number is: {x}"), "Number is: 0"); - let x = big_int::from(-1); + let x = BigInt::from(-1); assert_eq!(format!("Number is: {x}"), "Number is: -1"); - let x = big_int::from(320020000981234567890i128); + let x = BigInt::from(320020000981234567890i128); assert_eq!( format!("Number is: {x}"), "Number is: 320020000981234567890" @@ -129,13 +129,13 @@ mod tests { #[test] fn to_words() { - let x = big_int::from(20); + let x = BigInt::from(20); assert_eq!(x.to_words(), "two zero"); - let x = big_int::from(-24); + let x = BigInt::from(-24); assert_eq!(x.to_words(), "minus two four"); - let x = big_int::from(2550021); + let x = BigInt::from(2550021); assert_eq!(x.to_words(), "two five five zero zero two one"); - let x = big_int::from(-2000000000001i128); + let x = BigInt::from(-2000000000001i128); assert_eq!( x.to_words(), "minus two zero zero zero zero zero zero zero zero zero zero zero one" @@ -144,7 +144,7 @@ mod tests { #[test] fn not() { - let mut x = big_int::from(1); + let mut x = BigInt::from(1); assert_eq!(x.positive, true); assert_eq!(x.to_string(), "1"); x = -x; @@ -153,7 +153,7 @@ mod tests { x = -x; assert_eq!(x.positive, true); assert_eq!(x.to_string(), "1"); - let mut x = big_int::from(-22); + let mut x = BigInt::from(-22); assert_eq!(x.positive, false); assert_eq!(x.to_string(), "-22"); x = -x; @@ -163,181 +163,181 @@ mod tests { #[test] fn equal() { - let x = big_int::from(10); - let y = big_int::from(10); + let x = BigInt::from(10); + let y = BigInt::from(10); assert!(x == y); - let x = big_int::from(101010); - let y = big_int::from(101010); + let x = BigInt::from(101010); + let y = BigInt::from(101010); assert!(x == y); - let x = big_int::from(101010); - let y = big_int::from(101210); + let x = BigInt::from(101010); + let y = BigInt::from(101210); assert!(!(x == y)); - let x = big_int::from(101010); - let y = big_int::from(1); + let x = BigInt::from(101010); + let y = BigInt::from(1); assert!(!(x == y)); - let x = big_int::from(0); - let y = big_int::from(0); + let x = BigInt::from(0); + let y = BigInt::from(0); assert!(x == y); - let x = big_int::from(10); - let y = big_int::from(-10); + let x = BigInt::from(10); + let y = BigInt::from(-10); assert!(x != y); - let x = big_int::from(11); - let y = big_int::from(10); + let x = BigInt::from(11); + let y = BigInt::from(10); assert!(x != y); - let x = big_int::from(-0); - let y = big_int::from(0); + let x = BigInt::from(-0); + let y = BigInt::from(0); assert!(x == y); } #[test] fn equal_int() { - let x = big_int::from(10); + let x = BigInt::from(10); assert!(x == 10); - let x = big_int::from(101010); + let x = BigInt::from(101010); assert!(x == 101010); - let x = big_int::from(101010); + let x = BigInt::from(101010); assert!(!(x == 101210)); - let x = big_int::from(101010); + let x = BigInt::from(101010); assert!(!(x == 1)); - let x = big_int::from(0); + let x = BigInt::from(0); assert!(x == 0); - let x = big_int::from(10); + let x = BigInt::from(10); assert!(x != -10); - let x = big_int::from(11); + let x = BigInt::from(11); assert!(x != 10); - let x = big_int::from(-0); + let x = BigInt::from(-0); assert!(x == 0); } #[test] fn equal_str() { - let x = big_int::from(10); + let x = BigInt::from(10); assert!(x == "10"); - let x = big_int::from(101010); + let x = BigInt::from(101010); assert!(x == "101010"); - let x = big_int::from(101010); + let x = BigInt::from(101010); assert!(!(x == "101210")); - let x = big_int::from(101010); + let x = BigInt::from(101010); assert!(!(x == "1")); - let x = big_int::from(0); + let x = BigInt::from(0); assert!(x == "0"); - let x = big_int::from(10); + let x = BigInt::from(10); assert!(x != "-10"); - let x = big_int::from(11); + let x = BigInt::from(11); assert!(x != "10"); - let x = big_int::from(-0); + let x = BigInt::from(-0); assert!(x == "0"); } #[test] fn greater() { - let x: big_int = big_int::from(15); - let y = big_int::from(10); + let x: BigInt = BigInt::from(15); + let y = BigInt::from(10); assert!(x > y); - let x: big_int = big_int::from(8); - let y = big_int::from(7); + let x: BigInt = BigInt::from(8); + let y = BigInt::from(7); assert!(x > y); - let x = big_int::from(10); - let y = big_int::from(10); + let x = BigInt::from(10); + let y = BigInt::from(10); assert!(!(x > y)); - let x = big_int::from(10); - let y = big_int::from(10); + let x = BigInt::from(10); + let y = BigInt::from(10); assert!(x >= y); - let x = big_int::from(101010); - let y = big_int::from(101010); + let x = BigInt::from(101010); + let y = BigInt::from(101010); assert!(x >= y); - let x = big_int::from(0); - let y = big_int::from(0); + let x = BigInt::from(0); + let y = BigInt::from(0); assert!(!(x > y)); - let x = big_int::from(10); - let y = big_int::from(-10); + let x = BigInt::from(10); + let y = BigInt::from(-10); assert!(x > y); - let x = big_int::from(11); - let y = big_int::from(10); + let x = BigInt::from(11); + let y = BigInt::from(10); assert!(x > y); } #[test] fn lesser() { - let x: big_int = big_int::from(0); - let y = big_int::from(10); + let x: BigInt = BigInt::from(0); + let y = BigInt::from(10); assert!(x < y); - let x: big_int = big_int::from(8); - let y = big_int::from(9); + let x: BigInt = BigInt::from(8); + let y = BigInt::from(9); assert!(x < y); - let x = big_int::from(10); - let y = big_int::from(10); + let x = BigInt::from(10); + let y = BigInt::from(10); assert!(!(x < y)); - let x = big_int::from(10); - let y = big_int::from(10); + let x = BigInt::from(10); + let y = BigInt::from(10); assert!(x <= y); - let x = big_int::from(99999999999i64); - let y = big_int::from(99999999999i128); + let x = BigInt::from(99999999999i64); + let y = BigInt::from(99999999999i128); assert!(x <= y); - let x = big_int::from(0); - let y = big_int::from(0); + let x = BigInt::from(0); + let y = BigInt::from(0); assert!(!(x < y)); - let x = big_int::from(-10); - let y = big_int::from(10); + let x = BigInt::from(-10); + let y = BigInt::from(10); assert!(x < y); - let x = big_int::from(11); - let y = big_int::from(99999999999u64); + let x = BigInt::from(11); + let y = BigInt::from(99999999999u64); assert!(x < y); } #[test] fn add() { - let mut x: big_int = big_int::from(10); - let y = big_int::from(10); + let mut x: BigInt = BigInt::from(10); + let y = BigInt::from(10); let z = x.clone() + y.clone(); x += y; assert_eq!(z, 20); assert_eq!(z, x); - let mut x = big_int::from(101010); - let y = big_int::from(101010); + let mut x = BigInt::from(101010); + let y = BigInt::from(101010); let z = x.clone() + y.clone(); x += y; assert_eq!(z, 202020); assert_eq!(z, x); - let mut x = big_int::from(0); - let y = big_int::from(0); + let mut x = BigInt::from(0); + let y = BigInt::from(0); let z = x.clone() + y.clone(); x += y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x = big_int::from(10); - let y = big_int::from(-10); + let mut x = BigInt::from(10); + let y = BigInt::from(-10); let z = x.clone() + y.clone(); x += y.clone(); assert_eq!(z, 0); assert_ne!(x, y); - let mut x = big_int::from(11); - let y = big_int::from(10); + let mut x = BigInt::from(11); + let y = BigInt::from(10); let z = x.clone() + y.clone(); x += y.clone(); assert_eq!(z, 21); assert_ne!(x, y); - let mut x = big_int::from(-0); - let y = big_int::from(0); + let mut x = BigInt::from(-0); + let y = BigInt::from(0); let z = x.clone() + y.clone(); x += y.clone(); assert_eq!(z, 0); assert_eq!(x, y); - let mut x = big_int::from(6); - let y = big_int::from(4); + let mut x = BigInt::from(6); + let y = BigInt::from(4); let z = x.clone() + y.clone(); x += y.clone(); assert_eq!(z, 10); assert_eq!(x, y); - let mut x = big_int::from(-15); - let y = big_int::from(-4); + let mut x = BigInt::from(-15); + let y = BigInt::from(-4); let z = x.clone() + y.clone(); x += y.clone(); assert_eq!(z, -19); @@ -346,64 +346,64 @@ mod tests { #[test] fn sub() { - let mut x: big_int = big_int::from(10); - let y = big_int::from(10); + let mut x: BigInt = BigInt::from(10); + let y = BigInt::from(10); let z = x.clone() - y.clone(); x -= y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x = big_int::from(101010); - let y = big_int::from(10); + let mut x = BigInt::from(101010); + let y = BigInt::from(10); let z = x.clone() - y.clone(); x -= y; assert_eq!(z, 101000); assert_eq!(z, x); - let mut x = big_int::from(0); - let y = big_int::from(0); + let mut x = BigInt::from(0); + let y = BigInt::from(0); let z = x.clone() - y.clone(); x -= y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x = big_int::from(10); - let y = big_int::from(-10); + let mut x = BigInt::from(10); + let y = BigInt::from(-10); let z = x.clone() - y.clone(); x -= y.clone(); assert_eq!(z, 20); assert_ne!(x, y); - let mut x = big_int::from(-10); - let y = big_int::from(10); + let mut x = BigInt::from(-10); + let y = BigInt::from(10); let z = x.clone() - y.clone(); x -= y.clone(); assert_eq!(z, -20); assert_ne!(x, y); - let mut x = big_int::from(11); - let y = big_int::from(10); + let mut x = BigInt::from(11); + let y = BigInt::from(10); let z = x.clone() - y.clone(); x -= y.clone(); assert_eq!(z, 1); assert_ne!(x, y); - let mut x = big_int::from(-0); - let y = big_int::from(0); + let mut x = BigInt::from(-0); + let y = BigInt::from(0); let z = x.clone() - y.clone(); x -= y.clone(); assert_eq!(z, 0); assert_eq!(x, y); - let mut x = big_int::from(6); - let y = big_int::from(4); + let mut x = BigInt::from(6); + let y = BigInt::from(4); let z = x.clone() - y.clone(); x -= y.clone(); assert_eq!(z, 2); assert_eq!(x, y); - let mut x = big_int::from(-15); - let y = big_int::from(-4); + let mut x = BigInt::from(-15); + let y = BigInt::from(-4); let z = x.clone() - y.clone(); x -= y.clone(); assert_eq!(z, -11); @@ -412,65 +412,65 @@ mod tests { #[test] fn mul() { - let mut x: big_int = big_int::from(10); - let y = big_int::from(10); - let z: big_int = x.clone() * y.clone(); + let mut x: BigInt = BigInt::from(10); + let y = BigInt::from(10); + let z: BigInt = x.clone() * y.clone(); x *= y; assert_eq!(z, 100); assert_eq!(z, x); - let mut x = big_int::from(101); - let y = big_int::from(101); + let mut x = BigInt::from(101); + let y = BigInt::from(101); let z = x.clone() * y.clone(); x *= y; assert_eq!(z, 10201); assert_eq!(z, x); - let mut x = big_int::from(0); - let y = big_int::from(0); - let z: big_int = x.clone() * y.clone(); + let mut x = BigInt::from(0); + let y = BigInt::from(0); + let z: BigInt = x.clone() * y.clone(); x *= y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x = big_int::from(20100000100u64); - let y = big_int::from(0); - let z: big_int = x.clone() * y.clone(); + let mut x = BigInt::from(20100000100u64); + let y = BigInt::from(0); + let z: BigInt = x.clone() * y.clone(); x *= y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x = big_int::from(10); - let y = big_int::from(-10); - let z: big_int = x.clone() * y.clone(); + let mut x = BigInt::from(10); + let y = BigInt::from(-10); + let z: BigInt = x.clone() * y.clone(); x *= y.clone(); assert_eq!(z, -100); assert_ne!(x, y); - let mut x = big_int::from(11); - let y = big_int::from(10); - let z: big_int = x.clone() * y.clone(); + let mut x = BigInt::from(11); + let y = BigInt::from(10); + let z: BigInt = x.clone() * y.clone(); x *= y.clone(); assert_eq!(z, 110); assert_ne!(x, y); - let mut x = big_int::from(-0); - let y = big_int::from(0); - let z: big_int = x.clone() * y.clone(); + let mut x = BigInt::from(-0); + let y = BigInt::from(0); + let z: BigInt = x.clone() * y.clone(); x *= y.clone(); assert_eq!(z, 0); assert_eq!(x, y); - let mut x = big_int::from(6); - let y = big_int::from(4); - let z: big_int = x.clone() * y.clone(); + let mut x = BigInt::from(6); + let y = BigInt::from(4); + let z: BigInt = x.clone() * y.clone(); x *= y.clone(); assert_eq!(z, 24); assert_eq!(x, y); - let mut x = big_int::from(-15); - let y = big_int::from(-4); - let z: big_int = x.clone() * y.clone(); + let mut x = BigInt::from(-15); + let y = BigInt::from(-4); + let z: BigInt = x.clone() * y.clone(); x *= y.clone(); assert_eq!(z, 60); assert_eq!(x, y); @@ -478,71 +478,71 @@ mod tests { #[test] fn div() { - let mut x: big_int = big_int::from(10000); - let y = big_int::from(10); + let mut x: BigInt = BigInt::from(10000); + let y = BigInt::from(10); let z = x.clone() / y.clone(); x /= y; assert_eq!(z, 1000); assert_eq!(z, x); - let mut x = big_int::from(101); - let y = big_int::from(101); + let mut x = BigInt::from(101); + let y = BigInt::from(101); let z = x.clone() / y.clone(); x /= y; assert_eq!(z, 1); assert_eq!(z, x); - let mut x = big_int::from(0); - let y = big_int::from(2); + let mut x = BigInt::from(0); + let y = BigInt::from(2); let z = x.clone() / y.clone(); x /= y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x = big_int::from(0); - let y = big_int::from(0); + let mut x = BigInt::from(0); + let y = BigInt::from(0); let z = x.clone() / y.clone(); x /= y; - assert_eq!(z, big_int::default()); + assert_eq!(z, BigInt::default()); assert_eq!(z, x); - let mut x = big_int::from(10000); - let y = big_int::from(0); + let mut x = BigInt::from(10000); + let y = BigInt::from(0); let z = x.clone() / y.clone(); x /= y; - assert_eq!(z, big_int::default()); + assert_eq!(z, BigInt::default()); assert_eq!(z, x); - let mut x = big_int::from(20100000100u64); - let y = big_int::from(200); + let mut x = BigInt::from(20100000100u64); + let y = BigInt::from(200); let z = x.clone() / y.clone(); x /= y; assert_eq!(z, 100500); assert_eq!(z, x); - let mut x = big_int::from(10); - let y = big_int::from(-10); + let mut x = BigInt::from(10); + let y = BigInt::from(-10); let z = x.clone() / y.clone(); x /= y.clone(); assert_eq!(z, -1); assert_ne!(x, y); - let mut x = big_int::from(110); - let y = big_int::from(10); + let mut x = BigInt::from(110); + let y = BigInt::from(10); let z = x.clone() / y.clone(); x /= y.clone(); assert_eq!(z, 11); assert_ne!(x, y); - let mut x = big_int::from(6); - let y = big_int::from(2); + let mut x = BigInt::from(6); + let y = BigInt::from(2); let z = x.clone() / y.clone(); x /= y.clone(); assert_eq!(z, 3); assert_eq!(x, y); - let mut x = big_int::from(-15); - let y = big_int::from(-5); + let mut x = BigInt::from(-15); + let y = BigInt::from(-5); let z = x.clone() / y.clone(); x /= y.clone(); assert_eq!(z, 3); @@ -551,50 +551,50 @@ mod tests { #[test] fn reminder() { - let mut x: big_int = big_int::from(10000); - let y = big_int::from(10); + let mut x: BigInt = BigInt::from(10000); + let y = BigInt::from(10); let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x: big_int = big_int::from(10); - let y = big_int::from(0); + let mut x: BigInt = BigInt::from(10); + let y = BigInt::from(0); let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x: big_int = big_int::from(10); - let y = big_int::from(7); + let mut x: BigInt = BigInt::from(10); + let y = BigInt::from(7); let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 3); assert_eq!(z, x); - let mut x: big_int = big_int::from(10000); - let y = big_int::from(10); + let mut x: BigInt = BigInt::from(10000); + let y = BigInt::from(10); let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x: big_int = big_int::from(-104); - let y = big_int::from(10); + let mut x: BigInt = BigInt::from(-104); + let y = BigInt::from(10); let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 4); assert_eq!(z, x); - let mut x: big_int = big_int::from(24); - let y = big_int::from(3); + let mut x: BigInt = BigInt::from(24); + let y = BigInt::from(3); let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 0); assert_eq!(z, x); - let mut x: big_int = big_int::from(33); - let y = big_int::from(7); + let mut x: BigInt = BigInt::from(33); + let y = BigInt::from(7); let z = x.clone() % y.clone(); x %= y; assert_eq!(z, 5); @@ -603,52 +603,52 @@ mod tests { #[test] fn binary() { - let x = big_int::from(4); + let x = BigInt::from(4); assert_eq!(format!("{x:b}"), "100"); - let x = big_int::from(4); + let x = BigInt::from(4); assert_eq!(format!("{x:#b}"), "0b100"); - let x = big_int::from(4); + let x = BigInt::from(4); assert_eq!(format!("{x:010b}"), "0000000100"); - let x = big_int::from(10); + let x = BigInt::from(10); assert_eq!(format!("{x:#110b}"), "0b11111010"); - let x = big_int::from(172); + let x = BigInt::from(172); assert_eq!(format!("{x:b}"), "10101100"); - let x = big_int::from(17220003931i64); + let x = BigInt::from(17220003931i64); assert_eq!(format!("{x:#b}"), "0b10000000010011001000110100001011011"); } #[test] fn bit_and() { - let mut x = big_int::from(4); //100 - let y = big_int::from(12); //1100 + let mut x = BigInt::from(4); //100 + let y = BigInt::from(12); //1100 let z = x.clone() & y.clone(); //0100 x &= y; assert_eq!(x, z); assert_eq!(z, 4); - let mut x = big_int::from(10); //1010 - let y = big_int::from(13); //1101 + let mut x = BigInt::from(10); //1010 + let y = BigInt::from(13); //1101 let z = x.clone() & y.clone(); //1000 x &= y; assert_eq!(x, z); assert_eq!(z, 8); - let mut x = big_int::from(172); //10101100 - let y = big_int::from(223); //11011111 + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(223); //11011111 let z = x.clone() & y.clone(); //10001100 x &= y; assert_eq!(x, z); assert_eq!(z, 140); - let mut x = big_int::from(172); //10101100 - let y = big_int::from(1); //1 + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(1); //1 let z = x.clone() & y.clone(); //0 x &= y; assert_eq!(x, z); assert_eq!(z, 0); - let mut x = big_int::from(173); //10101101 - let y = big_int::from(1); //1 + let mut x = BigInt::from(173); //10101101 + let y = BigInt::from(1); //1 let z = x.clone() & y.clone(); //1 x &= y; assert_eq!(x, z); @@ -657,36 +657,36 @@ mod tests { #[test] fn bit_or() { - let mut x = big_int::from(4); //100 - let y = big_int::from(12); //1100 + let mut x = BigInt::from(4); //100 + let y = BigInt::from(12); //1100 let z = x.clone() | y.clone(); //1100 x |= y; assert_eq!(x, z); assert_eq!(z, 12); - let mut x = big_int::from(10); //1010 - let y = big_int::from(13); //1101 + let mut x = BigInt::from(10); //1010 + let y = BigInt::from(13); //1101 let z = x.clone() | y.clone(); //1111 x |= y; assert_eq!(x, z); assert_eq!(z, 15); - let mut x = big_int::from(172); //10101100 - let y = big_int::from(223); //11011111 + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(223); //11011111 let z = x.clone() | y.clone(); //11111111 x |= y; assert_eq!(x, z); assert_eq!(z, 255); - let mut x = big_int::from(172); //10101100 - let y = big_int::from(1); //1 + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(1); //1 let z = x.clone() | y.clone(); //10101101 x |= y; assert_eq!(x, z); assert_eq!(z, 173); - let mut x = big_int::from(173); //10101101 - let y = big_int::from(1); //1 + let mut x = BigInt::from(173); //10101101 + let y = BigInt::from(1); //1 let z = x.clone() | y.clone(); //10101101 x |= y; assert_eq!(x, z); @@ -695,36 +695,36 @@ mod tests { #[test] fn bit_xor() { - let mut x = big_int::from(4); //100 - let y = big_int::from(12); //1100 + let mut x = BigInt::from(4); //100 + let y = BigInt::from(12); //1100 let z = x.clone() ^ y.clone(); //1000 x ^= y; assert_eq!(x, z); assert_eq!(z, 8); - let mut x = big_int::from(10); //1010 - let y = big_int::from(13); //1101 + let mut x = BigInt::from(10); //1010 + let y = BigInt::from(13); //1101 let z = x.clone() ^ y.clone(); //0111 x ^= y; assert_eq!(x, z); assert_eq!(z, 7); - let mut x = big_int::from(172); //10101100 - let y = big_int::from(223); //11011111 + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(223); //11011111 let z = x.clone() ^ y.clone(); //01110011 x ^= y; assert_eq!(x, z); assert_eq!(z, 115); - let mut x = big_int::from(172); //10101100 - let y = big_int::from(1); //1 + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(1); //1 let z = x.clone() ^ y.clone(); //10101101 x ^= y; assert_eq!(x, z); assert_eq!(z, 173); - let mut x = big_int::from(173); //10101101 - let y = big_int::from(1); //1 + let mut x = BigInt::from(173); //10101101 + let y = BigInt::from(1); //1 let z = x.clone() ^ y.clone(); //10101100 x ^= y; assert_eq!(x, z); @@ -733,36 +733,36 @@ mod tests { #[test] fn bit_shift_left() { - let mut x = big_int::from(4); //100 - let y = big_int::from(2); + let mut x = BigInt::from(4); //100 + let y = BigInt::from(2); let z = x.clone() << y.clone(); //10000 x <<= y; assert_eq!(x, z); assert_eq!(z, 16); - let mut x = big_int::from(10); //1010 - let y = big_int::from(1); + let mut x = BigInt::from(10); //1010 + let y = BigInt::from(1); let z = x.clone() << y.clone(); //10100 x <<= y; assert_eq!(x, z); assert_eq!(z, 20); - let mut x = big_int::from(172); //10101100 - let y = big_int::from(0); + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(0); let z = x.clone() << y.clone(); x <<= y; assert_eq!(x, z); assert_eq!(z, 172); - let mut x = big_int::from(172); //10101100 - let y = big_int::from(10); + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(10); let z = x.clone() << y.clone(); //101011000000000000 x <<= y; assert_eq!(x, z); assert_eq!(z, 176128); - let mut x = big_int::from(173); //10101101 - let y = big_int::from(1); + let mut x = BigInt::from(173); //10101101 + let y = BigInt::from(1); let z = x.clone() << y.clone(); //101011010 x <<= y; assert_eq!(x, z); @@ -771,36 +771,36 @@ mod tests { #[test] fn bit_shift_right() { - let mut x = big_int::from(4); //100 - let y = big_int::from(2); + let mut x = BigInt::from(4); //100 + let y = BigInt::from(2); let z = x.clone() >> y.clone(); //1 x >>= y; assert_eq!(x, z); assert_eq!(z, 1); - let mut x = big_int::from(10); //1010 - let y = big_int::from(1); + let mut x = BigInt::from(10); //1010 + let y = BigInt::from(1); let z = x.clone() >> y.clone(); //101 x >>= y; assert_eq!(x, z); assert_eq!(z, 5); - let mut x = big_int::from(172); //10101100 - let y = big_int::from(0); + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(0); let z = x.clone() >> y.clone(); //10101100 x >>= y; assert_eq!(x, z); assert_eq!(z, 172); - let mut x = big_int::from(172); //10101100 - let y = big_int::from(10); + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(10); let z = x.clone() >> y.clone(); //0 x >>= y; assert_eq!(x, z); assert_eq!(z, 0); - let mut x = big_int::from(173); //10101101 - let y = big_int::from(1); + let mut x = BigInt::from(173); //10101101 + let y = BigInt::from(1); let z = x.clone() >> y.clone(); //1010110 x >>= y; assert_eq!(x, z); @@ -809,85 +809,85 @@ mod tests { #[test] fn hexadecimal() { - let x = big_int::from(4); + let x = BigInt::from(4); assert_eq!(format!("{x:X}"), "4"); - let x = big_int::from(16); + let x = BigInt::from(16); assert_eq!(format!("{x:#X}"), "0xF"); - let x = big_int::from(10); + let x = BigInt::from(10); assert_eq!(format!("{x:#X}"), "0x10"); - let x = big_int::from(172); + let x = BigInt::from(172); assert_eq!(format!("{x:X}"), "AC"); - let x = big_int::from(17220003931u128); + let x = BigInt::from(17220003931u128); assert_eq!(format!("{x:#X}"), "0x40264685B"); - let x = big_int::from(4); + let x = BigInt::from(4); assert_eq!(format!("{x:x}"), "4"); - let x = big_int::from(16); + let x = BigInt::from(16); assert_eq!(format!("{x:#x}"), "0xf"); - let x = big_int::from(10); + let x = BigInt::from(10); assert_eq!(format!("{x:#x}"), "0x10"); - let x = big_int::from(172); + let x = BigInt::from(172); assert_eq!(format!("{x:x}"), "ac"); - let x = big_int::from(17220003931u128); + let x = BigInt::from(17220003931u128); assert_eq!(format!("{x:#x}"), "0x40264685b"); } #[test] fn progtest_tests() { - let mut a = big_int::new(); - let mut b = big_int::new(); - a = big_int::from(10); - a += big_int::from(20); + let mut a = BigInt::new(); + let mut b = BigInt::new(); + a = BigInt::from(10); + a += BigInt::from(20); assert_eq!(a, 30); - a *= big_int::from(5); + a *= BigInt::from(5); assert_eq!(a, 150); - b = a.clone() + big_int::from(3); + b = a.clone() + BigInt::from(3); assert_eq!(b, 153); - b = a.clone() * big_int::from(7); + b = a.clone() * BigInt::from(7); assert_eq!(b, 1050); assert_eq!(a, 150); assert_eq!(format!("{a:X}"), "96"); - a = big_int::from(10); - a += big_int::from(-20); + a = BigInt::from(10); + a += BigInt::from(-20); assert_eq!(a, -10); - a *= big_int::from(5); + a *= BigInt::from(5); assert_eq!(a, -50); - b = a.clone() + big_int::from(73); + b = a.clone() + BigInt::from(73); assert_eq!(b, 23); - b = a.clone() * big_int::from(-7); + b = a.clone() * BigInt::from(-7); assert_eq!(b, 350); assert_eq!(a, -50); assert_eq!(format!("{a:X}"), "-32"); - a = big_int::from(12345678901234567890i128); - a += big_int::from(-99999999999999999999i128); + a = BigInt::from(12345678901234567890i128); + a += BigInt::from(-99999999999999999999i128); assert_eq!(a, -87654321098765432109i128); - a *= big_int::from(54321987654321987654i128); + a *= BigInt::from(54321987654321987654i128); assert_eq!(a, "-4761556948575111126880627366067073182286"); - a *= big_int::from(0); + a *= BigInt::from(0); assert_eq!(a, 0); - a = big_int::from(10); + a = BigInt::from(10); b = a.clone() + 400; assert_eq!(b, "410"); - b = a.clone() * big_int::from_str("15").unwrap_or_default(); + b = a.clone() * BigInt::from_str("15").unwrap_or_default(); assert_eq!(b, "150"); assert_eq!(a, "10"); assert_eq!(format!("{a:X}"), "A"); - b = big_int::from_str("1234").unwrap_or_default(); + b = BigInt::from_str("1234").unwrap_or_default(); assert_eq!(format!("{b}"), "1234"); - assert!(big_int::from_str(" 12 34").is_err()); - assert!(big_int::from_str("999z").is_err()); - assert!(big_int::from_str("abcd").is_err()); - assert!(big_int::from_str("-xyz").is_err()); - assert!(big_int::from_str(":").is_err()); - assert!(big_int::from_str("%").is_err()); - assert!(big_int::from_str("- 758").is_err()); - a = big_int::from(42); + assert!(BigInt::from_str(" 12 34").is_err()); + assert!(BigInt::from_str("999z").is_err()); + assert!(BigInt::from_str("abcd").is_err()); + assert!(BigInt::from_str("-xyz").is_err()); + assert!(BigInt::from_str(":").is_err()); + assert!(BigInt::from_str("%").is_err()); + assert!(BigInt::from_str("- 758").is_err()); + a = BigInt::from(42); assert_eq!(a, 42); - a = big_int::from(73786976294838206464i128); + a = BigInt::from(73786976294838206464i128); assert_eq!(a, 73786976294838206464u128); assert_eq!(format!("{a:X}"), "40000000000000000"); assert!(a < "1361129467683753853853498429727072845824"); @@ -908,14 +908,14 @@ mod tests { assert!(!(a >= 73786976294838206465)); assert!(!(a == 73786976294838206465u128)); assert!(a != 73786976294838206465i128); - a = big_int::from_str("2147483648").unwrap_or(big_int::new()); + a = BigInt::from_str("2147483648").unwrap_or(BigInt::new()); assert!(!(a < -2147483648)); assert!(!(a <= -2147483648)); assert!(a > -2147483648); assert!(a >= -2147483648); assert!(!(a == -2147483648)); assert!(a != -2147483648); - a = big_int::from_str("-12345678").unwrap_or(big_int::new()); + a = BigInt::from_str("-12345678").unwrap_or(BigInt::new()); assert!(!(a < -87654321)); assert!(!(a <= -87654321)); assert!(a > -87654321); From 4ea8a78f0bece44e5b251f5913fe61f2b104b2c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 16 Jul 2025 00:11:04 +0200 Subject: [PATCH 084/243] remove unused import --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index eed7d28..2743c54 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ use std::{ fmt::{self, Binary, Display, LowerHex, UpperHex}, ops::{ Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, - DivAssign, Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, + DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign, }, str::FromStr, From ac4c851563cc44237c3614c6390186c62403c594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 16 Jul 2025 00:12:20 +0200 Subject: [PATCH 085/243] =?UTF-8?q?change=20of=20.clone()=20->=C2=A0&?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 2743c54..cb31638 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -138,7 +138,7 @@ impl Display for BigInt { write!(f, "-")?; } - for digit in self.numbers.clone() { + for digit in &self.numbers { write!(f, "{digit}")?; } Ok(()) From a700ba49c5872787be1d40f919713534affbf9f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 16 Jul 2025 00:13:42 +0200 Subject: [PATCH 086/243] rename of crate --- Cargo.lock | 14 +++++++------- Cargo.toml | 2 +- src/tests.rs | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d0c01de..ff48f55 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,19 +2,19 @@ # It is not intended for manual editing. version = 4 -[[package]] -name = "BigInt" -version = "0.1.0" -dependencies = [ - "num-traits", -] - [[package]] name = "autocfg" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" +[[package]] +name = "big_int" +version = "0.1.0" +dependencies = [ + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" diff --git a/Cargo.toml b/Cargo.toml index b253c26..a4dede9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "BigInt" +name = "big_int" version = "0.1.0" edition = "2024" diff --git a/src/tests.rs b/src/tests.rs index 77c872e..835834b 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,8 +1,8 @@ mod tests { use std::str::FromStr; - use crate::BigIntError; use crate::BigInt; + use crate::BigIntError; #[test] fn default() { From c0f17505de6f858cfab52f47f5cf5e911f99b857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 16 Jul 2025 00:35:29 +0200 Subject: [PATCH 087/243] edit of use block --- src/lib.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cb31638..1e74544 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1,12 @@ use num_traits::ToPrimitive; -use std::{ - cmp::Ordering, - fmt::{self, Binary, Display, LowerHex, UpperHex}, - ops::{ - Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, - DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, - SubAssign, - }, - str::FromStr, + +use std::cmp::Ordering; +use std::fmt; +use std::ops::{ + Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign, + Mul, MulAssign, Neg, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign, }; +use std::str::FromStr; #[derive(Debug)] enum BigIntError { From ffa90c3856c83774077eed88410a6d540252e45a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 16 Jul 2025 00:38:31 +0200 Subject: [PATCH 088/243] quick fix --- src/lib.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1e74544..24c930b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,11 +1,8 @@ use num_traits::ToPrimitive; use std::cmp::Ordering; -use std::fmt; -use std::ops::{ - Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign, - Mul, MulAssign, Neg, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign, -}; +use std::fmt::*; +use std::ops::*; use std::str::FromStr; #[derive(Debug)] From 984507396fc07775e28ef47800e24cae26b970fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 16 Jul 2025 00:49:55 +0200 Subject: [PATCH 089/243] quick fix --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 24c930b..af09e0e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ use num_traits::ToPrimitive; use std::cmp::Ordering; use std::fmt::*; +use std::fmt::{self, Binary, Display, LowerHex, UpperHex}; use std::ops::*; use std::str::FromStr; From 02bfe36b14fd614b112bf09617d2eb6f19d4d972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 16 Jul 2025 00:57:05 +0200 Subject: [PATCH 090/243] add of Display for BigIntError --- src/lib.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index af09e0e..6840905 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ use num_traits::ToPrimitive; use std::cmp::Ordering; +use std::error::Error; use std::fmt::*; use std::fmt::{self, Binary, Display, LowerHex, UpperHex}; use std::ops::*; @@ -11,6 +12,14 @@ enum BigIntError { NaN, } +impl Display for BigIntError { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + write!(f, "Not and Number") + } +} + +impl Error for BigIntError {} + #[derive(Clone, Eq, Debug)] struct BigInt { positive: bool, From 534c135cd91a2626400bc38300cddaeab185236c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 17 Jul 2025 01:15:14 +0200 Subject: [PATCH 091/243] error traits fix --- src/lib.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6840905..a78f235 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,6 @@ use num_traits::ToPrimitive; use std::cmp::Ordering; use std::error::Error; -use std::fmt::*; use std::fmt::{self, Binary, Display, LowerHex, UpperHex}; use std::ops::*; use std::str::FromStr; @@ -12,14 +11,14 @@ enum BigIntError { NaN, } +impl Error for BigIntError {} + impl Display for BigIntError { - fn fmt(&self, f: &mut Formatter<'_>) -> Result { - write!(f, "Not and Number") + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Not a Number") } } -impl Error for BigIntError {} - #[derive(Clone, Eq, Debug)] struct BigInt { positive: bool, From ced20c2ad0fe9cd739293be940a1d1114f17daf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 17 Jul 2025 01:19:58 +0200 Subject: [PATCH 092/243] performance fix --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index a78f235..a6451c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,7 +62,7 @@ impl FromStr for BigInt { numbers.push(char.to_digit(10).unwrap().to_u8().unwrap()); } - if numbers == vec![0] { + if numbers.as_slice() == &[0] { positive = true; } From b7daaac75510c93d8796313a0c136a3c68e5b2d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 17 Jul 2025 01:20:14 +0200 Subject: [PATCH 093/243] change of if to match --- src/lib.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a6451c6..bef306e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -183,11 +183,10 @@ eq_with_int!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128); impl PartialEq<&str> for BigInt { fn eq(&self, other: &&str) -> bool { - let right_side = BigInt::from_str(other); - if right_side.is_err() { - return false; + match BigInt::from_str(other) { + Ok(right) => self == &right, + Err(_) => false, } - self == &right_side.unwrap() } } From b31f2e0ee4ce91a87ffa0f9156b76e9a48632f46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 17 Jul 2025 01:25:16 +0200 Subject: [PATCH 094/243] remove useless fn traits --- src/lib.rs | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bef306e..2096d88 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -209,54 +209,27 @@ impl Neg for BigInt { } impl PartialOrd for BigInt { - fn ge(&self, other: &Self) -> bool { - todo!() - } fn gt(&self, other: &Self) -> bool { todo!() } - fn le(&self, other: &Self) -> bool { - todo!() - } - fn lt(&self, other: &Self) -> bool { - todo!() - } fn partial_cmp(&self, other: &Self) -> Option { todo!() } } impl PartialOrd for BigInt { - fn ge(&self, other: &i128) -> bool { - todo!() - } fn gt(&self, other: &i128) -> bool { todo!() } - fn le(&self, other: &i128) -> bool { - todo!() - } - fn lt(&self, other: &i128) -> bool { - todo!() - } fn partial_cmp(&self, other: &i128) -> Option { todo!() } } impl PartialOrd<&str> for BigInt { - fn ge(&self, other: &&str) -> bool { - todo!() - } fn gt(&self, other: &&str) -> bool { todo!() } - fn le(&self, other: &&str) -> bool { - todo!() - } - fn lt(&self, other: &&str) -> bool { - todo!() - } fn partial_cmp(&self, other: &&str) -> Option { todo!() } From 4e1920c3a917ba8462740a68daf3651aded2de61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 20 Jul 2025 17:32:04 +0200 Subject: [PATCH 095/243] allow unused code --- src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 2096d88..d8a22f1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +#![allow(unused)] + use num_traits::ToPrimitive; use std::cmp::Ordering; From dd3a3077e5013186da241ca585d8020be7377e2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 20 Jul 2025 17:56:50 +0200 Subject: [PATCH 096/243] implement gt fn of PartialOrd trait --- src/lib.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index d8a22f1..e5c78aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -212,7 +212,36 @@ impl Neg for BigInt { impl PartialOrd for BigInt { fn gt(&self, other: &Self) -> bool { - todo!() + //check +/- + if self.positive != other.positive { + return self.positive; + } + + let positive = self.positive; + //Compare length + if self.numbers.len() != other.numbers.len() { + if positive { + return self.numbers.len() > other.numbers.len(); + } else { + return self.numbers.len() < other.numbers.len(); + } + } + + //Compare digits + let left_iterator = self.numbers.iter(); + let right_iterator = other.numbers.iter(); + let mut numbers_iterator = left_iterator.zip(right_iterator); + + let mut greater_then = false; + + for (left, right) in numbers_iterator { + if left != right { + greater_then = left > right; + break; + } + } + + greater_then } fn partial_cmp(&self, other: &Self) -> Option { todo!() From 06203dd97bf96797077b1ccd5fd20cf25adf6b5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 20 Jul 2025 17:56:59 +0200 Subject: [PATCH 097/243] implement partial_cmp fn of PartialOrd trait --- src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index e5c78aa..f49ba50 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -244,7 +244,13 @@ impl PartialOrd for BigInt { greater_then } fn partial_cmp(&self, other: &Self) -> Option { - todo!() + if self == other { + Some(Ordering::Equal) + } else if self.gt(other) { + Some(Ordering::Greater) + } else { + Some(Ordering::Less) + } } } From 827389685de575a4df698a3185aa444bbb3750a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 20 Jul 2025 18:07:34 +0200 Subject: [PATCH 098/243] rework PartialOrd gt function is now implemented in partial_cmp --- src/lib.rs | 50 +++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f49ba50..e45416e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -211,46 +211,42 @@ impl Neg for BigInt { } impl PartialOrd for BigInt { - fn gt(&self, other: &Self) -> bool { + fn partial_cmp(&self, other: &Self) -> Option { + if self == other { + return Some(Ordering::Equal); + } + + let mut greater = false; + //check +/- if self.positive != other.positive { - return self.positive; + greater = self.positive; } - - let positive = self.positive; - //Compare length - if self.numbers.len() != other.numbers.len() { - if positive { - return self.numbers.len() > other.numbers.len(); + //check length + else if self.numbers.len() != other.numbers.len() { + if self.positive { + greater = self.numbers.len() > other.numbers.len(); } else { - return self.numbers.len() < other.numbers.len(); + greater = self.numbers.len() < other.numbers.len(); } } + //compare digits + else { + let mut numbers_iterator = self.numbers.iter().zip(other.numbers.iter()); - //Compare digits - let left_iterator = self.numbers.iter(); - let right_iterator = other.numbers.iter(); - let mut numbers_iterator = left_iterator.zip(right_iterator); - - let mut greater_then = false; - - for (left, right) in numbers_iterator { - if left != right { - greater_then = left > right; - break; + for (left, right) in numbers_iterator { + if left != right { + greater = left > right; + } } } - greater_then - } - fn partial_cmp(&self, other: &Self) -> Option { - if self == other { - Some(Ordering::Equal) - } else if self.gt(other) { + //return value + return if greater { Some(Ordering::Greater) } else { Some(Ordering::Less) - } + }; } } From 739c29bd5794e7d8c65d3a4350a16f66544ba4c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 20 Jul 2025 18:08:23 +0200 Subject: [PATCH 099/243] remove useless implementations --- src/lib.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e45416e..4b0078d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -251,18 +251,12 @@ impl PartialOrd for BigInt { } impl PartialOrd for BigInt { - fn gt(&self, other: &i128) -> bool { - todo!() - } fn partial_cmp(&self, other: &i128) -> Option { todo!() } } impl PartialOrd<&str> for BigInt { - fn gt(&self, other: &&str) -> bool { - todo!() - } fn partial_cmp(&self, other: &&str) -> Option { todo!() } From 20fa281f9a0b53e20bdaf9e02a8c7166103762bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 20 Jul 2025 18:09:42 +0200 Subject: [PATCH 100/243] minor changes in code structure --- src/lib.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4b0078d..3e1a9b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -181,6 +181,16 @@ macro_rules! eq_with_int { }; } +impl PartialEq for BigInt { + fn eq(&self, other: &BigInt) -> bool { + if self.positive != other.positive { + false + } else { + self.numbers == other.numbers + } + } +} + eq_with_int!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128); impl PartialEq<&str> for BigInt { @@ -192,16 +202,6 @@ impl PartialEq<&str> for BigInt { } } -impl PartialEq for BigInt { - fn eq(&self, other: &BigInt) -> bool { - if self.positive != other.positive { - false - } else { - self.numbers == other.numbers - } - } -} - impl Neg for BigInt { type Output = Self; fn neg(mut self) -> Self::Output { From 94703611e800bd21d6a211307638812b2c88e1f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 20 Jul 2025 18:11:15 +0200 Subject: [PATCH 101/243] minor changes in code structure --- src/lib.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3e1a9b1..6195774 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -169,6 +169,16 @@ impl LowerHex for BigInt { } } +impl PartialEq for BigInt { + fn eq(&self, other: &BigInt) -> bool { + if self.positive != other.positive { + false + } else { + self.numbers == other.numbers + } + } +} + macro_rules! eq_with_int { ($($t:ty),*) => { $( @@ -181,16 +191,6 @@ macro_rules! eq_with_int { }; } -impl PartialEq for BigInt { - fn eq(&self, other: &BigInt) -> bool { - if self.positive != other.positive { - false - } else { - self.numbers == other.numbers - } - } -} - eq_with_int!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128); impl PartialEq<&str> for BigInt { From 58ef97331f3db9d45da3d4f5a44d2a781acf4b0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 20 Jul 2025 19:42:06 +0200 Subject: [PATCH 102/243] implement partial_cmp with string --- src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 6195774..897fb66 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -258,7 +258,13 @@ impl PartialOrd for BigInt { impl PartialOrd<&str> for BigInt { fn partial_cmp(&self, other: &&str) -> Option { - todo!() + let other = BigInt::from_str(&other); + + if other.is_err() { + return None; + } + + return self.partial_cmp(&other.unwrap()); } } From 9646b1c177a58199e58ae86682def8f2a8846a44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 20 Jul 2025 19:54:31 +0200 Subject: [PATCH 103/243] implement partialOrd for intiegers --- src/lib.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 897fb66..cd0eb42 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -210,6 +210,20 @@ impl Neg for BigInt { } } +macro_rules! partial_ord_intieger { + ($($t:ty),*) => { + $( + impl PartialOrd<$t> for BigInt{ + fn partial_cmp(&self, other: &$t) -> Option { + return self.partial_cmp(&BigInt::from(*other)); + } + } + )* + }; +} + +partial_ord_intieger!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128); + impl PartialOrd for BigInt { fn partial_cmp(&self, other: &Self) -> Option { if self == other { @@ -250,12 +264,6 @@ impl PartialOrd for BigInt { } } -impl PartialOrd for BigInt { - fn partial_cmp(&self, other: &i128) -> Option { - todo!() - } -} - impl PartialOrd<&str> for BigInt { fn partial_cmp(&self, other: &&str) -> Option { let other = BigInt::from_str(&other); From 8d190bf4dfe41036a58186f708199068b359988c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 20 Jul 2025 19:58:41 +0200 Subject: [PATCH 104/243] fix some tests --- src/tests.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 835834b..3c7f3e2 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -896,16 +896,16 @@ mod tests { assert!(!(a >= "1361129467683753853853498429727072845824")); assert!(!(a == "1361129467683753853853498429727072845824")); assert!(a != "1361129467683753853853498429727072845824"); - assert!(!(a < 73786976294838206464)); - assert!(a <= 73786976294838206464); - assert!(!(a > 73786976294838206464)); - assert!(a >= 73786976294838206464); + assert!(!(a < 73786976294838206464i128)); + assert!(a <= 73786976294838206464u128); + assert!(!(a > 73786976294838206464i128)); + assert!(a >= 73786976294838206464u128); assert!(a == 73786976294838206464u128); assert!(!(a != 73786976294838206464i128)); - assert!(a < 73786976294838206465); - assert!(a <= 73786976294838206465); - assert!(!(a > 73786976294838206465)); - assert!(!(a >= 73786976294838206465)); + assert!(a < 73786976294838206464i128); + assert!(a <= 73786976294838206464u128); + assert!(!(a > 73786976294838206464i128)); + assert!(!(a >= 73786976294838206464u128)); assert!(!(a == 73786976294838206465u128)); assert!(a != 73786976294838206465i128); a = BigInt::from_str("2147483648").unwrap_or(BigInt::new()); From 3cbaca7f76d477764f7c433f136979d99ec21a7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 21 Jul 2025 00:29:04 +0200 Subject: [PATCH 105/243] add implementation of add trait --- src/lib.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cd0eb42..5f5aeb9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ #![allow(unused)] -use num_traits::ToPrimitive; +use num_traits::{ToPrimitive, Zero}; use std::cmp::Ordering; use std::error::Error; @@ -281,8 +281,82 @@ where T: Into, { type Output = Self; - fn add(self, rhs: T) -> Self::Output { - todo!() + fn add(mut self, rhs: T) -> Self::Output { + let mut right: BigInt = rhs.into(); + + // X + 0 edgecase + if self == 0 { + return right; + } + if right == 0 { + return self; + } + + // negative numbers edgecases + if !self.positive && right.positive { + // -X + Y => Y - X => -(X - Y) + self.positive = true; + return right - self; + } else if self.positive && !right.positive { + //X - Y + right.positive = true; + return self - right; + } + + let mut result = BigInt::new(); + result.positive = self.positive; + let mut carry = 0; + + let mut left_iterator = self.numbers.iter().rev(); + let mut right_iterator = right.numbers.iter().rev(); + + let mut left_iterator_valid = true; + let mut right_iterator_valid = true; + + while left_iterator_valid || right_iterator_valid || !carry.is_zero() { + //check if there are some digits left + let mut left_digit: Option<&u8> = None; + let mut right_digit: Option<&u8> = None; + + if left_iterator_valid { + left_digit = left_iterator.next(); + } + if right_iterator_valid { + right_digit = right_iterator.next(); + } + + if left_digit.is_none() { + left_iterator_valid = false; + } + if right_digit.is_none() { + right_iterator_valid = false; + } + if !left_iterator_valid && !right_iterator_valid && carry.is_zero() { + break; + } + + //move carry + let mut num = 0; + num += carry; + carry = 0; + + if left_iterator_valid { + num += left_digit.unwrap(); + } + if right_iterator_valid { + num += right_digit.unwrap(); + } + + //prepare one digit for sum and reminder hide in carry + let last_digit = num % 10; + num -= last_digit; + num /= 10; + carry = num; + + result.numbers.insert(0, last_digit); + } + + result } } From 19050629fc595b1383a2edd29c9b24feae99a3bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 21 Jul 2025 22:42:18 +0200 Subject: [PATCH 106/243] implement add_assign --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 5f5aeb9..f848cf7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -365,7 +365,8 @@ where T: Into, { fn add_assign(&mut self, rhs: T) { - todo!() + let right: BigInt = rhs.into(); + *self = self.clone().add(right); } } From 0c349ec6767fb4568fba14a965211b89a4791c26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 21 Jul 2025 22:50:27 +0200 Subject: [PATCH 107/243] fix an error with an extra zero --- src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index f848cf7..f63591c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -303,7 +303,10 @@ where return self - right; } - let mut result = BigInt::new(); + let mut result = BigInt { + positive: true, + numbers: vec![], + }; result.positive = self.positive; let mut carry = 0; From 0c5ebc361ebf17a4d3a35c848e15e41d593a7420 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 22 Jul 2025 22:44:16 +0200 Subject: [PATCH 108/243] squish two lines into one --- src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f63591c..23b04cd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -368,8 +368,7 @@ where T: Into, { fn add_assign(&mut self, rhs: T) { - let right: BigInt = rhs.into(); - *self = self.clone().add(right); + *self = self.clone().add(rhs.into()); } } From a1aeaff20fc8218978e5fac9bea4ed58b38471b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 22 Jul 2025 22:45:21 +0200 Subject: [PATCH 109/243] implement sub_assign --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 23b04cd..7c0cabf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -388,7 +388,7 @@ where T: Into, { fn sub_assign(&mut self, rhs: T) { - todo!() + *self = self.clone().sub(rhs.into()); } } From 8b0c6c9626ee7928277e3b64dfdd07bb50a9f502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 24 Jul 2025 19:58:31 +0200 Subject: [PATCH 110/243] add implementation of sub trait --- src/lib.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7c0cabf..254fcbc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -378,8 +378,93 @@ where { type Output = Self; - fn sub(self, rhs: T) -> Self::Output { - todo!() + fn sub(mut self, rhs: T) -> Self::Output { + let mut right: BigInt = rhs.into(); + + // X - 0 edgecase + if self == 0 { + return right; + } + if right == 0 { + return self; + } + if right == self { + return 0.into(); + } + + // negative numbers edgecases + if !self.positive && right.positive { + // -X - Y => -(X+Y) + return -(self + right); + } else if self.positive && !right.positive { + //X - (-Y) + right.positive = true; + return self + right; + } else if !self.positive && !right.positive { + // -X - (-Y) => -X + Y => Y - X + self.positive = true; + right.positive = true; + return right - self; + } + + let mut result: BigInt = BigInt { + positive: self >= right, + numbers: vec![], + }; + + let mut carry = 0; + + let mut left_iterator = self.numbers.iter().rev(); + let mut right_iterator = right.numbers.iter().rev(); + + let mut left_iterator_valid = true; + let mut right_iterator_valid = true; + + while left_iterator_valid || right_iterator_valid || !carry.is_zero() { + //check if there are some digits left + let mut left_digit: Option<&u8> = None; + let mut right_digit: Option<&u8> = None; + + if left_iterator_valid { + left_digit = left_iterator.next(); + } + if right_iterator_valid { + right_digit = right_iterator.next(); + } + + if left_digit.is_none() { + left_iterator_valid = false; + } + if right_digit.is_none() { + right_iterator_valid = false; + } + if !left_iterator_valid && !right_iterator_valid && carry.is_zero() { + break; + } + + let mut new_digit: i8 = 0; + + if left_iterator_valid { + let left_digit_unwrap = *left_digit.unwrap() as i8; + new_digit += left_digit_unwrap; + } + if right_iterator_valid { + let right_digit_unwrap: i8 = *right_digit.unwrap() as i8; + new_digit -= right_digit_unwrap; + } + + new_digit -= carry; + carry = 0; + + if new_digit < 0 { + new_digit += 10; + carry += 1; + } + + result.numbers.insert(0, new_digit as u8); + } + + result } } From 670b9089e734da063356e4cb12364fd3173e1da9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 24 Jul 2025 20:43:51 +0200 Subject: [PATCH 111/243] fix tests --- src/tests.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 3c7f3e2..179f5c2 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -292,21 +292,21 @@ mod tests { let z = x.clone() + y.clone(); x += y; assert_eq!(z, 20); - assert_eq!(z, x); + assert_eq!(x, z); let mut x = BigInt::from(101010); let y = BigInt::from(101010); let z = x.clone() + y.clone(); x += y; assert_eq!(z, 202020); - assert_eq!(z, x); + assert_eq!(x, z); let mut x = BigInt::from(0); let y = BigInt::from(0); let z = x.clone() + y.clone(); x += y; assert_eq!(z, 0); - assert_eq!(z, x); + assert_eq!(x, z); let mut x = BigInt::from(10); let y = BigInt::from(-10); @@ -327,21 +327,21 @@ mod tests { let z = x.clone() + y.clone(); x += y.clone(); assert_eq!(z, 0); - assert_eq!(x, y); + assert_eq!(x, z); let mut x = BigInt::from(6); let y = BigInt::from(4); let z = x.clone() + y.clone(); x += y.clone(); assert_eq!(z, 10); - assert_eq!(x, y); + assert_eq!(x, z); let mut x = BigInt::from(-15); let y = BigInt::from(-4); let z = x.clone() + y.clone(); x += y.clone(); assert_eq!(z, -19); - assert_eq!(x, y); + assert_eq!(x, z); } #[test] @@ -351,21 +351,21 @@ mod tests { let z = x.clone() - y.clone(); x -= y; assert_eq!(z, 0); - assert_eq!(z, x); + assert_eq!(x, z); let mut x = BigInt::from(101010); let y = BigInt::from(10); let z = x.clone() - y.clone(); x -= y; assert_eq!(z, 101000); - assert_eq!(z, x); + assert_eq!(x, z); let mut x = BigInt::from(0); let y = BigInt::from(0); let z = x.clone() - y.clone(); x -= y; assert_eq!(z, 0); - assert_eq!(z, x); + assert_eq!(x, z); let mut x = BigInt::from(10); let y = BigInt::from(-10); @@ -393,21 +393,21 @@ mod tests { let z = x.clone() - y.clone(); x -= y.clone(); assert_eq!(z, 0); - assert_eq!(x, y); + assert_eq!(x, z); let mut x = BigInt::from(6); let y = BigInt::from(4); let z = x.clone() - y.clone(); x -= y.clone(); assert_eq!(z, 2); - assert_eq!(x, y); + assert_eq!(x, z); let mut x = BigInt::from(-15); let y = BigInt::from(-4); let z = x.clone() - y.clone(); x -= y.clone(); assert_eq!(z, -11); - assert_eq!(x, y); + assert_eq!(x, z); } #[test] From b4feeff1baffed0dd0db37c54a28f90e6a96618d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 24 Jul 2025 20:51:29 +0200 Subject: [PATCH 112/243] fix -X-Y edgecase --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 254fcbc..69bea3e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -395,6 +395,7 @@ where // negative numbers edgecases if !self.positive && right.positive { // -X - Y => -(X+Y) + self.positive = true; return -(self + right); } else if self.positive && !right.positive { //X - (-Y) From 6dd4c58af90a82c8ec9248fd085433cbd264f4f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 24 Jul 2025 21:08:31 +0200 Subject: [PATCH 113/243] add more tests --- src/tests.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index 179f5c2..850eb74 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -408,6 +408,13 @@ mod tests { x -= y.clone(); assert_eq!(z, -11); assert_eq!(x, z); + + let mut x = BigInt::from(987654); + let y = BigInt::from(987653); + let z = x.clone() - y.clone(); + x -= y.clone(); + assert_eq!(z, 1); + assert_eq!(x, z); } #[test] From 669442c9f0c9292131854a3d3c653bd78f0962a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 24 Jul 2025 21:22:25 +0200 Subject: [PATCH 114/243] add even more tests --- src/tests.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index 850eb74..57e0a5c 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -402,6 +402,13 @@ mod tests { assert_eq!(z, 2); assert_eq!(x, z); + let mut x = BigInt::from(4); + let y = BigInt::from(15); + let z = x.clone() - y.clone(); + x -= y.clone(); + assert_eq!(z, -11); + assert_eq!(x, z); + let mut x = BigInt::from(-15); let y = BigInt::from(-4); let z = x.clone() - y.clone(); From b2eab2e87fd4d81a96fdf83ee60d9229c95d94fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 24 Jul 2025 21:23:07 +0200 Subject: [PATCH 115/243] add removal of first zeros --- src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 69bea3e..8d215d7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -465,6 +465,10 @@ where result.numbers.insert(0, new_digit as u8); } + while result.numbers.first().unwrap_or(&1).is_zero() { + result.numbers.remove(0); + } + result } } From ae2abece770c5411863e4021aaa7f27fcdd4fdc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 24 Jul 2025 21:34:37 +0200 Subject: [PATCH 116/243] add more tests for sub trait --- src/tests.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index 57e0a5c..7ddb865 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -422,6 +422,13 @@ mod tests { x -= y.clone(); assert_eq!(z, 1); assert_eq!(x, z); + + let mut x = BigInt::from(1); + let y = BigInt::from(1000); + let z = x.clone() - y.clone(); + x -= y.clone(); + assert_eq!(z, -999); + assert_eq!(x, z); } #[test] From a1860c3a2be30dafacdabbd081c72e08e4bdf862 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 24 Jul 2025 21:34:54 +0200 Subject: [PATCH 117/243] fix self < right edgecase --- src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 8d215d7..beef7fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -408,6 +408,10 @@ where return right - self; } + if self < right { + return -(right - self); + } + let mut result: BigInt = BigInt { positive: self >= right, numbers: vec![], From d2b46c628f9f880b0a5fe46b5261603ffd900d2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 24 Jul 2025 23:07:37 +0200 Subject: [PATCH 118/243] remove useless .into() --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index beef7fd..1601cc9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -368,7 +368,7 @@ where T: Into, { fn add_assign(&mut self, rhs: T) { - *self = self.clone().add(rhs.into()); + *self = self.clone().add(rhs); } } @@ -482,7 +482,7 @@ where T: Into, { fn sub_assign(&mut self, rhs: T) { - *self = self.clone().sub(rhs.into()); + *self = self.clone().sub(rhs); } } From 891f689f6cbde11e3b95fb22d787f69752d8299e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 24 Jul 2025 23:08:10 +0200 Subject: [PATCH 119/243] implement remaining assign traits --- src/lib.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1601cc9..63763d1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -501,7 +501,7 @@ where T: Into, { fn mul_assign(&mut self, rhs: T) { - todo!() + *self = self.clone().mul(rhs); } } @@ -520,7 +520,7 @@ where T: Into, { fn div_assign(&mut self, rhs: T) { - todo!() + *self = self.clone().div(rhs); } } @@ -539,7 +539,7 @@ where T: Into, { fn rem_assign(&mut self, rhs: T) { - todo!() + *self = self.clone().rem(rhs); } } @@ -558,7 +558,7 @@ where T: Into, { fn bitand_assign(&mut self, rhs: T) { - todo!() + *self = self.clone().bitand(rhs); } } @@ -577,7 +577,7 @@ where T: Into, { fn bitor_assign(&mut self, rhs: T) { - todo!() + *self = self.clone().bitor(rhs); } } @@ -596,7 +596,7 @@ where T: Into, { fn bitxor_assign(&mut self, rhs: T) { - todo!() + *self = self.clone().bitxor(rhs); } } @@ -615,7 +615,7 @@ where T: Into, { fn shl_assign(&mut self, rhs: T) { - todo!() + *self = self.clone().shl(rhs); } } @@ -634,7 +634,7 @@ where T: Into, { fn shr_assign(&mut self, rhs: T) { - todo!() + *self = self.clone().shr(rhs); } } From f97b5293af08c42ce1962ea74106f0d417598a01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Fri, 25 Jul 2025 21:44:34 +0200 Subject: [PATCH 120/243] change minor things --- src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 63763d1..f37b84e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -304,10 +304,9 @@ where } let mut result = BigInt { - positive: true, + positive: self.positive, numbers: vec![], }; - result.positive = self.positive; let mut carry = 0; let mut left_iterator = self.numbers.iter().rev(); From 85c7b314c2e442b90905462ef042455c97512a54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sat, 26 Jul 2025 09:33:40 +0200 Subject: [PATCH 121/243] fix tests --- src/tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 7ddb865..d02a647 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -487,14 +487,14 @@ mod tests { let z: BigInt = x.clone() * y.clone(); x *= y.clone(); assert_eq!(z, 24); - assert_eq!(x, y); + assert_eq!(x, z); let mut x = BigInt::from(-15); let y = BigInt::from(-4); let z: BigInt = x.clone() * y.clone(); x *= y.clone(); assert_eq!(z, 60); - assert_eq!(x, y); + assert_eq!(x, z); } #[test] From 793d745ecf7e5fc375182d92b3bb8a99a1bacb88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sat, 26 Jul 2025 09:34:07 +0200 Subject: [PATCH 122/243] add implementation of mul trait --- src/lib.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index f37b84e..d82e2fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -491,7 +491,72 @@ where { type Output = Self; fn mul(self, rhs: T) -> Self::Output { - todo!() + let mut right: BigInt = rhs.into(); + + //X * 0 edgecase + if self == 0 || right == 0 { + return BigInt::default(); + } + + if self == 1 { + return right; + } + if right == 1 { + return self; + } + + let mut result = BigInt { + positive: self.positive, + numbers: vec![], + }; + + let mut left_iterator = self.numbers.iter().rev(); + let mut right_iterator = right.numbers.iter().rev(); + + let mut sub_total = BigInt::new(); + let mut offset = -1; + let mut new_digit = 0; + let mut carry = 0; + + //left digit loop + for &left_digit in left_iterator { + offset += 1; + + // *0 edgecase + if left_digit == 0 { + continue; + } + + sub_total = BigInt { + positive: self.positive, + numbers: vec![], + }; + + new_digit = 0; + carry = 0; + + //right digit loop + for &right_digit in right_iterator.clone() { + new_digit = left_digit * right_digit; + new_digit += carry; + + sub_total.numbers.insert(0, new_digit % 10); + carry = new_digit / 10; + } + + if !carry.is_zero() { + sub_total.numbers.insert(0, carry); + } + + for i in (0..offset) { + sub_total.numbers.push(0); + } + + result += sub_total; + } + + result.positive = !(self.positive ^ right.positive); + result } } From d5ee7f3ef4f2cd1b3250f04887b1c3015591db9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sat, 26 Jul 2025 18:39:36 +0200 Subject: [PATCH 123/243] add more tests --- src/tests.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index d02a647..d491619 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -570,6 +570,14 @@ mod tests { assert_eq!(x, y); } + #[test] + #[should_panic] + fn div_by_zero() { + let mut x = BigInt::from(10); + let y = BigInt::from(0); + let z = x / y; + } + #[test] fn reminder() { let mut x: BigInt = BigInt::from(10000); From e6eba6ac31121f853d129a1e92799c3fe6079729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sat, 26 Jul 2025 18:39:51 +0200 Subject: [PATCH 124/243] remove useless remove keywords --- src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d82e2fc..2b26e48 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -256,11 +256,11 @@ impl PartialOrd for BigInt { } //return value - return if greater { + if greater { Some(Ordering::Greater) } else { Some(Ordering::Less) - }; + } } } @@ -272,7 +272,7 @@ impl PartialOrd<&str> for BigInt { return None; } - return self.partial_cmp(&other.unwrap()); + self.partial_cmp(&other.unwrap()) } } From 5b220cc46376c1938e7f4f1d585cd1dbb3e335c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sat, 26 Jul 2025 21:52:01 +0200 Subject: [PATCH 125/243] change insert(0) -> push() for better performance --- src/lib.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2b26e48..36ff87a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -93,10 +93,12 @@ macro_rules! from_int { //transformation of digits while original_number != 0{ - numbers.insert(0, (original_number % 10).to_u8().unwrap()); + numbers.push((original_number % 10).to_u8().unwrap()) original_number /= 10; } + numbers.reverse(); + //return value BigInt {positive, numbers} @@ -121,10 +123,12 @@ macro_rules! from_uint { //transformation of digits while original_number != 0{ - numbers.insert(0, (original_number % 10).to_u8().unwrap()); + numbers.push((original_number % 10).to_u8().unwrap()) original_number /= 10; } + numbers.reverse(); + //return value BigInt { positive: true, numbers} @@ -355,9 +359,11 @@ where num /= 10; carry = num; - result.numbers.insert(0, last_digit); + result.numbers.push(last_digit); } + result.numbers.reverse(); + result } } @@ -465,9 +471,11 @@ where carry += 1; } - result.numbers.insert(0, new_digit as u8); + result.numbers.push(new_digit as u8); } + result.numbers.reverse(); + while result.numbers.first().unwrap_or(&1).is_zero() { result.numbers.remove(0); } @@ -540,14 +548,16 @@ where new_digit = left_digit * right_digit; new_digit += carry; - sub_total.numbers.insert(0, new_digit % 10); + sub_total.numbers.push(new_digit % 10); carry = new_digit / 10; } if !carry.is_zero() { - sub_total.numbers.insert(0, carry); + sub_total.numbers.push(carry); } + sub_total.numbers.reverse(); + for i in (0..offset) { sub_total.numbers.push(0); } From dfc6771b7275e7649377d460f4bb20aa291e800a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sat, 26 Jul 2025 21:56:10 +0200 Subject: [PATCH 126/243] fix missing ; --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 36ff87a..ddc707e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -93,7 +93,7 @@ macro_rules! from_int { //transformation of digits while original_number != 0{ - numbers.push((original_number % 10).to_u8().unwrap()) + numbers.push((original_number % 10).to_u8().unwrap()); original_number /= 10; } @@ -123,7 +123,7 @@ macro_rules! from_uint { //transformation of digits while original_number != 0{ - numbers.push((original_number % 10).to_u8().unwrap()) + numbers.push((original_number % 10).to_u8().unwrap()); original_number /= 10; } From e9fb2ee5b9f7c60ddf4011db8407d6805b344d2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sat, 26 Jul 2025 21:56:21 +0200 Subject: [PATCH 127/243] change return type of number_to_word --- src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ddc707e..d1b6ef1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -752,7 +752,7 @@ impl BigInt { } } - fn number_to_word(number: u8) -> String { + fn number_to_word(number: u8) -> &'static str { match number { 0 => "zero", 1 => "one", @@ -766,7 +766,6 @@ impl BigInt { 9 => "nine", _ => "zero", } - .to_string() } fn parse_word_digits(string_of_numbers: String) -> Result { From b32132da339154bb2f841bb62fc0b27316a7f194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sat, 26 Jul 2025 22:09:53 +0200 Subject: [PATCH 128/243] additional changes --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d1b6ef1..8f47187 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -718,14 +718,14 @@ impl BigInt { } pub fn to_words(&self) -> String { - let mut fin_str = String::new(); + let mut fin_str: String; let mut nmr_iter = self.numbers.iter(); //print minus or first digit if !self.positive { fin_str = "minus".to_string(); } else { - fin_str = BigInt::number_to_word(*nmr_iter.next().unwrap_or(&0)); + fin_str = BigInt::number_to_word(*nmr_iter.next().unwrap_or(&0)).to_string(); } //print all digits From 9616c7a8958ee7c62a40f3603bb697df4ef64411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sat, 26 Jul 2025 22:19:29 +0200 Subject: [PATCH 129/243] add more tests --- src/tests.rs | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index d491619..7942ff3 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -513,6 +513,18 @@ mod tests { assert_eq!(z, 1); assert_eq!(z, x); + let mut x = BigInt::from_str( + "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999", + ).unwrap(); + let y = BigInt::from(2); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!( + z, + "499999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" + ); + assert_eq!(z, x); + let mut x = BigInt::from(0); let y = BigInt::from(2); let z = x.clone() / y.clone(); @@ -520,15 +532,29 @@ mod tests { assert_eq!(z, 0); assert_eq!(z, x); + let mut x = BigInt::from(1); + let y = BigInt::from(2); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x = BigInt::from(1); + let y = BigInt::from(1); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!(z, 1); + assert_eq!(z, x); + let mut x = BigInt::from(0); - let y = BigInt::from(0); + let y = BigInt::from(666); let z = x.clone() / y.clone(); x /= y; assert_eq!(z, BigInt::default()); assert_eq!(z, x); - let mut x = BigInt::from(10000); - let y = BigInt::from(0); + let mut x = BigInt::from(0); + let y = BigInt::from(100); let z = x.clone() / y.clone(); x /= y; assert_eq!(z, BigInt::default()); From bab175228779f14ca054fb9de83fa677f1cb6cd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 28 Jul 2025 10:11:56 +0200 Subject: [PATCH 130/243] change some tests --- src/tests.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 7942ff3..1a4f6fa 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -513,16 +513,11 @@ mod tests { assert_eq!(z, 1); assert_eq!(z, x); - let mut x = BigInt::from_str( - "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999", - ).unwrap(); + let mut x = BigInt::from(9999u128); let y = BigInt::from(2); let z = x.clone() / y.clone(); x /= y; - assert_eq!( - z, - "499999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" - ); + assert_eq!(z, 4999u128); assert_eq!(z, x); let mut x = BigInt::from(0); From d478b44ba69a5bed981dc518eb61b6b6a52924f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 28 Jul 2025 10:12:02 +0200 Subject: [PATCH 131/243] fix some tests --- src/tests.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 1a4f6fa..9df6bcc 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -545,21 +545,21 @@ mod tests { let y = BigInt::from(666); let z = x.clone() / y.clone(); x /= y; - assert_eq!(z, BigInt::default()); + assert_eq!(z, 0); assert_eq!(z, x); let mut x = BigInt::from(0); let y = BigInt::from(100); let z = x.clone() / y.clone(); x /= y; - assert_eq!(z, BigInt::default()); + assert_eq!(z, 0); assert_eq!(z, x); let mut x = BigInt::from(20100000100u64); - let y = BigInt::from(200); + let y = BigInt::from(20000000); let z = x.clone() / y.clone(); x /= y; - assert_eq!(z, 100500); + assert_eq!(z, 1005); assert_eq!(z, x); let mut x = BigInt::from(10); @@ -581,14 +581,14 @@ mod tests { let z = x.clone() / y.clone(); x /= y.clone(); assert_eq!(z, 3); - assert_eq!(x, y); + assert_eq!(x, z); let mut x = BigInt::from(-15); let y = BigInt::from(-5); let z = x.clone() / y.clone(); x /= y.clone(); assert_eq!(z, 3); - assert_eq!(x, y); + assert_eq!(x, z); } #[test] From e96d5680bd4fa692f1d8786cb03fda0ec26e102f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 28 Jul 2025 10:12:24 +0200 Subject: [PATCH 132/243] implement slow version of division --- src/lib.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8f47187..414ad2c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -584,8 +584,38 @@ where T: Into, { type Output = Self; - fn div(self, rhs: T) -> Self::Output { - todo!() + fn div(mut self, rhs: T) -> Self::Output { + let mut right: BigInt = rhs.into(); + + if right == 0 { + panic!("division by zero!"); + } + if self == 0 { + return BigInt::default(); + } + if right == 1 { + return 1.into(); + } + if self == 1 { + if right == 1 { + return 1.into(); + } + return BigInt::default(); + } + + let mut result = BigInt::default(); + let positive = !(self.positive ^ right.positive); + self.positive = true; + right.positive = true; + + while self >= right && self != 0 { + self -= right.clone(); + result += 1; + } + + result.positive = positive; + + result } } From c40519b22b30525261f18bd1bbcb216f08d7be1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 28 Jul 2025 10:27:30 +0200 Subject: [PATCH 133/243] move slow division to another function --- src/lib.rs | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 414ad2c..3afa0d7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -579,6 +579,26 @@ where } } +fn slow_div(mut left: BigInt, mut right: BigInt) -> BigInt { + let mut result = BigInt::default(); + let positive = !(left.positive ^ right.positive); + left.positive = true; + right.positive = true; + + while left >= right && left != 0 { + left -= right.clone(); + result += 1; + } + + result.positive = positive; + + result +} + +fn fast_div(mut left: BigInt, mut right: BigInt) -> BigInt { + todo!() +} + impl Div for BigInt where T: Into, @@ -603,19 +623,7 @@ where return BigInt::default(); } - let mut result = BigInt::default(); - let positive = !(self.positive ^ right.positive); - self.positive = true; - right.positive = true; - - while self >= right && self != 0 { - self -= right.clone(); - result += 1; - } - - result.positive = positive; - - result + slow_div(self, right) } } From 56e98768e751bd319bacd61579332e6a130c43c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 28 Jul 2025 11:10:32 +0200 Subject: [PATCH 134/243] add sub trait -1 edgecase --- src/lib.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 3afa0d7..91f7a39 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -513,6 +513,13 @@ where return self; } + if self == -1 { + return -right; + } + if right == -1 { + return -self; + } + let mut result = BigInt { positive: self.positive, numbers: vec![], From e667b7e917a5b33ac7d7246cef1c650ecf90f79d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 28 Jul 2025 20:34:22 +0200 Subject: [PATCH 135/243] add more tests --- src/tests.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index 9df6bcc..20dc02a 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -287,6 +287,13 @@ mod tests { #[test] fn add() { + let mut x: BigInt = BigInt::from(1000); + let y = BigInt::from(10); + let z = x.clone() + y.clone(); + x += y; + assert_eq!(z, 1010); + assert_eq!(x, z); + let mut x: BigInt = BigInt::from(10); let y = BigInt::from(10); let z = x.clone() + y.clone(); From 81285c6286595ce3bd65bb65a07ad9b27a6dc692 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 28 Jul 2025 20:35:03 +0200 Subject: [PATCH 136/243] rewrite add trait iterative way --- src/lib.rs | 64 +++++++++++++++++++++--------------------------------- 1 file changed, 25 insertions(+), 39 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 91f7a39..ea0c715 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -313,53 +313,39 @@ where }; let mut carry = 0; - let mut left_iterator = self.numbers.iter().rev(); - let mut right_iterator = right.numbers.iter().rev(); + let (mut longer, mut shorter) = { + if self.numbers.len() >= right.numbers.len() { + (self.numbers, right.numbers) + } else { + (right.numbers, self.numbers) + } + }; - let mut left_iterator_valid = true; - let mut right_iterator_valid = true; + let extend_count = longer.len() - shorter.len(); - while left_iterator_valid || right_iterator_valid || !carry.is_zero() { - //check if there are some digits left - let mut left_digit: Option<&u8> = None; - let mut right_digit: Option<&u8> = None; + longer.reverse(); + shorter.reverse(); - if left_iterator_valid { - left_digit = left_iterator.next(); - } - if right_iterator_valid { - right_digit = right_iterator.next(); - } + shorter.extend(std::iter::repeat(0).take(extend_count)); - if left_digit.is_none() { - left_iterator_valid = false; - } - if right_digit.is_none() { - right_iterator_valid = false; - } - if !left_iterator_valid && !right_iterator_valid && carry.is_zero() { - break; - } + let numbers_set = longer.iter().zip(shorter.iter()); + let mut new_number; - //move carry - let mut num = 0; - num += carry; - carry = 0; + for number_set in numbers_set { + new_number = 0; - if left_iterator_valid { - num += left_digit.unwrap(); - } - if right_iterator_valid { - num += right_digit.unwrap(); - } + new_number += number_set.0; + new_number += number_set.1; + new_number += carry; - //prepare one digit for sum and reminder hide in carry - let last_digit = num % 10; - num -= last_digit; - num /= 10; - carry = num; + let new_digit = new_number % 10; + carry = new_number / 10; + + result.numbers.push(new_digit); + } - result.numbers.push(last_digit); + if !carry.is_zero() { + result.numbers.push(carry); } result.numbers.reverse(); From 7a7a1661b9443019f2285c1947af53a5cdc77177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 28 Jul 2025 20:40:38 +0200 Subject: [PATCH 137/243] move extending logic to separate function --- src/lib.rs | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ea0c715..eed90d4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -280,6 +280,25 @@ impl PartialOrd<&str> for BigInt { } } +fn create_numbers_set(left: BigInt, right: BigInt) -> (Vec, Vec) { + let (mut longer, mut shorter) = { + if left.numbers.len() >= right.numbers.len() { + (left.numbers, right.numbers) + } else { + (right.numbers, left.numbers) + } + }; + + let extend_count = longer.len() - shorter.len(); + + longer.reverse(); + shorter.reverse(); + + shorter.extend(std::iter::repeat(0).take(extend_count)); + + (shorter, longer) +} + impl Add for BigInt where T: Into, @@ -313,20 +332,7 @@ where }; let mut carry = 0; - let (mut longer, mut shorter) = { - if self.numbers.len() >= right.numbers.len() { - (self.numbers, right.numbers) - } else { - (right.numbers, self.numbers) - } - }; - - let extend_count = longer.len() - shorter.len(); - - longer.reverse(); - shorter.reverse(); - - shorter.extend(std::iter::repeat(0).take(extend_count)); + let (longer, shorter) = create_numbers_set(self, right); let numbers_set = longer.iter().zip(shorter.iter()); let mut new_number; From f8007337f9a47e51b183c329c3f4bd74553a2eb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 28 Jul 2025 21:40:52 +0200 Subject: [PATCH 138/243] minor line change --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index eed90d4..a165a11 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -330,12 +330,13 @@ where positive: self.positive, numbers: vec![], }; - let mut carry = 0; let (longer, shorter) = create_numbers_set(self, right); let numbers_set = longer.iter().zip(shorter.iter()); + let mut new_number; + let mut carry: u8 = 0; for number_set in numbers_set { new_number = 0; From 6664b2c24512a851394bc4d11fe02ce20228adcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 28 Jul 2025 21:45:42 +0200 Subject: [PATCH 139/243] rewrite sub trait iterative way --- src/lib.rs | 68 ++++++++++++++++++++---------------------------------- 1 file changed, 25 insertions(+), 43 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a165a11..b885515 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -334,7 +334,7 @@ where let (longer, shorter) = create_numbers_set(self, right); let numbers_set = longer.iter().zip(shorter.iter()); - + let mut new_number; let mut carry: u8 = 0; @@ -415,56 +415,38 @@ where numbers: vec![], }; - let mut carry = 0; - - let mut left_iterator = self.numbers.iter().rev(); - let mut right_iterator = right.numbers.iter().rev(); - - let mut left_iterator_valid = true; - let mut right_iterator_valid = true; + self.numbers.reverse(); + right.numbers.reverse(); - while left_iterator_valid || right_iterator_valid || !carry.is_zero() { - //check if there are some digits left - let mut left_digit: Option<&u8> = None; - let mut right_digit: Option<&u8> = None; + if self.numbers.len() >= right.numbers.len() { + right + .numbers + .extend(std::iter::repeat(0).take(self.numbers.len() - right.numbers.len())); + } else { + self.numbers + .extend(std::iter::repeat(0).take(right.numbers.len() - self.numbers.len())); + } - if left_iterator_valid { - left_digit = left_iterator.next(); - } - if right_iterator_valid { - right_digit = right_iterator.next(); - } + let numbers_set = self.numbers.iter().zip(right.numbers.iter()); - if left_digit.is_none() { - left_iterator_valid = false; - } - if right_digit.is_none() { - right_iterator_valid = false; - } - if !left_iterator_valid && !right_iterator_valid && carry.is_zero() { - break; - } - - let mut new_digit: i8 = 0; + let mut new_number: i8; + let mut carry = 0; - if left_iterator_valid { - let left_digit_unwrap = *left_digit.unwrap() as i8; - new_digit += left_digit_unwrap; - } - if right_iterator_valid { - let right_digit_unwrap: i8 = *right_digit.unwrap() as i8; - new_digit -= right_digit_unwrap; - } + for number_set in numbers_set { + new_number = 0; - new_digit -= carry; - carry = 0; + new_number += *number_set.0 as i8; + new_number -= *number_set.1 as i8; + new_number -= carry; - if new_digit < 0 { - new_digit += 10; - carry += 1; + if new_number < 0 { + new_number += 10; + carry = 1; + } else { + carry = 0; } - result.numbers.push(new_digit as u8); + result.numbers.push(new_number as u8); } result.numbers.reverse(); From 02b185a1461981b6534cced6f9f8fec1657bd418 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 28 Jul 2025 22:46:31 +0200 Subject: [PATCH 140/243] rewrite mul trait --- src/lib.rs | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b885515..2e75b2c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -495,41 +495,28 @@ where return -self; } - let mut result = BigInt { - positive: self.positive, - numbers: vec![], - }; - - let mut left_iterator = self.numbers.iter().rev(); - let mut right_iterator = right.numbers.iter().rev(); - - let mut sub_total = BigInt::new(); - let mut offset = -1; - let mut new_digit = 0; + let mut result = BigInt::default(); + let mut sub_total; + let mut new_digit; let mut carry = 0; - //left digit loop - for &left_digit in left_iterator { - offset += 1; - + for (position, &left) in self.numbers.iter().rev().enumerate() { // *0 edgecase - if left_digit == 0 { + if left == 0 { continue; } sub_total = BigInt { - positive: self.positive, + positive: true, numbers: vec![], }; - new_digit = 0; carry = 0; //right digit loop - for &right_digit in right_iterator.clone() { - new_digit = left_digit * right_digit; + for &right in right.numbers.iter().rev() { + new_digit = left * right; new_digit += carry; - sub_total.numbers.push(new_digit % 10); carry = new_digit / 10; } @@ -540,14 +527,13 @@ where sub_total.numbers.reverse(); - for i in (0..offset) { - sub_total.numbers.push(0); - } + //zero offset + sub_total.numbers.extend(vec![0; position]); result += sub_total; } - result.positive = !(self.positive ^ right.positive); + result.positive = self.positive == right.positive; result } } From 3a1b7a9a4faf2b1769275fca1928557eebf73bce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 28 Jul 2025 23:23:07 +0200 Subject: [PATCH 141/243] change some tests --- src/tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 20dc02a..1be3ed1 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -506,11 +506,11 @@ mod tests { #[test] fn div() { - let mut x: BigInt = BigInt::from(10000); + let mut x: BigInt = BigInt::from(10000000); let y = BigInt::from(10); let z = x.clone() / y.clone(); x /= y; - assert_eq!(z, 1000); + assert_eq!(z, 1000000); assert_eq!(z, x); let mut x = BigInt::from(101); From 920ca4002e33571b9b6519c5115e6a3cf2b5ecc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 00:09:12 +0200 Subject: [PATCH 142/243] fix partialOrd --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 2e75b2c..dbf761d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -255,6 +255,7 @@ impl PartialOrd for BigInt { for (left, right) in numbers_iterator { if left != right { greater = left > right; + break; } } } From 02f4eb1f510871d04e87acc156c20dc7dde0a2ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 00:09:17 +0200 Subject: [PATCH 143/243] add more tests --- src/tests.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index 1be3ed1..3597fb0 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -283,6 +283,9 @@ mod tests { let x = BigInt::from(11); let y = BigInt::from(99999999999u64); assert!(x < y); + let x = BigInt::from(10000010); + let y = BigInt::from(20000000); + assert!(x < y); } #[test] From 8f1363bd7beedefbaeef1824e1c8b91a59328297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 00:14:00 +0200 Subject: [PATCH 144/243] add time consuming test of div --- src/tests.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index 3597fb0..5b16e8b 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -530,6 +530,16 @@ mod tests { assert_eq!(z, 4999u128); assert_eq!(z, x); + let mut x = BigInt::from_str("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999").unwrap(); + let y = BigInt::from(2); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!( + z, + "499999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" + ); + assert_eq!(z, x); + let mut x = BigInt::from(0); let y = BigInt::from(2); let z = x.clone() / y.clone(); From e54dae55846fc084ce260c40cc885e64a20817ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 00:14:50 +0200 Subject: [PATCH 145/243] change div trait to be fast --- src/lib.rs | 61 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index dbf761d..804cd16 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -548,26 +548,6 @@ where } } -fn slow_div(mut left: BigInt, mut right: BigInt) -> BigInt { - let mut result = BigInt::default(); - let positive = !(left.positive ^ right.positive); - left.positive = true; - right.positive = true; - - while left >= right && left != 0 { - left -= right.clone(); - result += 1; - } - - result.positive = positive; - - result -} - -fn fast_div(mut left: BigInt, mut right: BigInt) -> BigInt { - todo!() -} - impl Div for BigInt where T: Into, @@ -592,7 +572,46 @@ where return BigInt::default(); } - slow_div(self, right) + let mut result = BigInt { + positive: self.positive == right.positive, + numbers: vec![], + }; + + self.positive = true; + right.positive = true; + + let mut left = BigInt { + positive: true, + numbers: vec![], + }; + + let mut new_digit; + + for digit in self.numbers.iter() { + left.numbers.push(*digit); + new_digit = 0; + + while &left >= &right { + left -= right.clone(); + new_digit += 1; + + if new_digit > 10 { + assert!(false); + } + } + + result.numbers.push(new_digit); + + if left.numbers == vec![0] { + left.numbers = vec![]; + } + } + + while result.numbers.first().unwrap_or(&1).is_zero() { + result.numbers.remove(0); + } + + result } } From 613987e751a06946e29e3472d54d164cd6a976ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 00:31:20 +0200 Subject: [PATCH 146/243] move one test to new segment --- src/tests.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 5b16e8b..b458b56 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -628,13 +628,6 @@ mod tests { assert_eq!(z, 0); assert_eq!(z, x); - let mut x: BigInt = BigInt::from(10); - let y = BigInt::from(0); - let z = x.clone() % y.clone(); - x %= y; - assert_eq!(z, 0); - assert_eq!(z, x); - let mut x: BigInt = BigInt::from(10); let y = BigInt::from(7); let z = x.clone() % y.clone(); @@ -671,6 +664,17 @@ mod tests { assert_eq!(z, x); } + #[test] + #[should_panic] + fn rem_of_zero() { + let mut x: BigInt = BigInt::from(10); + let y = BigInt::from(0); + let z = x.clone() % y.clone(); + x %= y; + assert_eq!(z, 0); + assert_eq!(z, x); + } + #[test] fn binary() { let x = BigInt::from(4); From 52f76013d22e5ccd320f91f9db3d5f95f97152d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 00:31:30 +0200 Subject: [PATCH 147/243] fix one test --- src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests.rs b/src/tests.rs index b458b56..b07efeb 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -646,7 +646,7 @@ mod tests { let y = BigInt::from(10); let z = x.clone() % y.clone(); x %= y; - assert_eq!(z, 4); + assert_eq!(z, -4); assert_eq!(z, x); let mut x: BigInt = BigInt::from(24); From d5d67b95e6d577e53fe4d6d6ab2bb986851113ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 00:31:42 +0200 Subject: [PATCH 148/243] implement rem trait --- src/lib.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 804cd16..8cc7066 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -630,7 +630,19 @@ where { type Output = Self; fn rem(self, rhs: T) -> Self::Output { - todo!() + let mut right: BigInt = rhs.into(); + + if right == 0 { + panic!("division by zero!"); + } + if self == 0 || right == 1 || right == 1 { + return 0.into(); + } + + let mut divide_result = self.clone() / right.clone(); + + //result + self - divide_result * right } } From b2990bff32824aae50949d21d284cf098fce8dae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 00:49:09 +0200 Subject: [PATCH 149/243] add tests for power of --- src/tests.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index b07efeb..b6adac0 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,6 +1,8 @@ mod tests { use std::str::FromStr; + use num_traits::Pow; + use crate::BigInt; use crate::BigIntError; @@ -664,6 +666,29 @@ mod tests { assert_eq!(z, x); } + #[test] + fn power_of() { + let mut x: BigInt = BigInt::from(2); + let y = BigInt::from(2); + let z = x.pow(y); + assert_eq!(z, 4); + + let mut x: BigInt = BigInt::from(2); + let y = BigInt::from(1); + let z = x.pow(y); + assert_eq!(z, 2); + + let mut x: BigInt = BigInt::from(1); + let y = BigInt::from(1000000); + let z = x.pow(y); + assert_eq!(z, 1); + + let mut x: BigInt = BigInt::from(3); + let y = BigInt::from(3); + let z = x.pow(y); + assert_eq!(z, 27); + } + #[test] #[should_panic] fn rem_of_zero() { From 097037695aedf7cec56df8e0332a1fd4a0b179b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 00:49:44 +0200 Subject: [PATCH 150/243] implement Pow trait --- src/lib.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 8cc7066..3e81b6f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ #![allow(unused)] -use num_traits::{ToPrimitive, Zero}; +use num_traits::{Pow, ToPrimitive, Zero}; use std::cmp::Ordering; use std::error::Error; @@ -655,6 +655,34 @@ where } } +impl Pow for BigInt +where + T: Into, +{ + type Output = Self; + + fn pow(self, rhs: T) -> Self::Output { + let mut right: BigInt = rhs.into(); + + if self == 0 || self == 1 || right == 1 { + return self; + } + if right == 0 { + return 1.into(); + } + + let mut result: BigInt = 1.into(); + + while right != 0 { + result *= self.clone(); + + right -= 1; + } + + result + } +} + impl BitAnd for BigInt where T: Into, From 3a9faecbd526a6f23266a89c54f493462274a114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 00:56:26 +0200 Subject: [PATCH 151/243] fix division edgecase --- src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 3e81b6f..a8d22c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -611,6 +611,10 @@ where result.numbers.remove(0); } + if result.numbers == vec![] { + return BigInt::default(); + } + result } } From f0392fd865d1e64058d7b8f2caf58615191af627 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 00:56:39 +0200 Subject: [PATCH 152/243] implement bit_shift_left --- src/lib.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index a8d22c5..db9113a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -750,7 +750,15 @@ where { type Output = Self; fn shl(self, rhs: T) -> Self::Output { - todo!() + let mut right: BigInt = rhs.into(); + + if right == 0 { + return self; + } + + let base: BigInt = 2.into(); + + self * base.pow(right) } } From bc3b9f54eb9358766e2f6b1edd923ec9da53be9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 00:56:45 +0200 Subject: [PATCH 153/243] implement bit_shift_right --- src/lib.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index db9113a..a8404be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -777,7 +777,15 @@ where { type Output = Self; fn shr(self, rhs: T) -> Self::Output { - todo!() + let mut right: BigInt = rhs.into(); + + if right == 0 { + return self; + } + + let base: BigInt = 2.into(); + + self / base.pow(right) } } From bdb621c7909005e895a0e2034706d49ab9573996 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 10:38:49 +0200 Subject: [PATCH 154/243] add more tests --- src/tests.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index b6adac0..ca29bfe 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -112,6 +112,12 @@ mod tests { #[test] fn display() { + let x = BigInt::from(1003); + assert_eq!(format!("Number is: {x:0^10}"), "Number is: 0001003000"); + let x = BigInt::from(1003); + assert_eq!(format!("Number is: {x:0>10}"), "Number is: 0000001003"); + let x = BigInt::from(1003); + assert_eq!(format!("Number is: {x:0<10}"), "Number is: 1003000000"); let x = BigInt::from(1003); assert_eq!(format!("Number is: {x}"), "Number is: 1003"); let x = BigInt::from(0); @@ -127,6 +133,20 @@ mod tests { format!("Number is: {x}"), "Number is: 320020000981234567890" ); + let x = BigInt::from(1003); + assert_eq!(format!("Number is: {x:->10}"), "Number is: ------1003"); + let x = BigInt::from(1003); + assert_eq!(format!("Number is: {x:9}"), "Number is: 1003"); + let x = BigInt::from(0); + assert_eq!(format!("Number is: {x:2}"), "Number is: 0"); + let x = BigInt::from(-100); + assert_eq!(format!("Number is: {x}"), "Number is: -100"); + let x = BigInt::from(-0); + assert_eq!(format!("Number is: {x}"), "Number is: 0"); + let x = BigInt::from(-100); + assert_eq!(format!("Number is: {x:X>5}"), "Number is: X-100"); + let x = BigInt::from(-0); + assert_eq!(format!("Number is: {x:+}"), "Number is: +0"); } #[test] From c2623f20d5e2992567066662bd261fb43dca6b3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 10:39:24 +0200 Subject: [PATCH 155/243] change tests line order --- src/tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index ca29bfe..2ba04d0 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -722,11 +722,11 @@ mod tests { #[test] fn binary() { - let x = BigInt::from(4); - assert_eq!(format!("{x:b}"), "100"); let x = BigInt::from(4); assert_eq!(format!("{x:#b}"), "0b100"); let x = BigInt::from(4); + assert_eq!(format!("{x:b}"), "100"); + let x = BigInt::from(4); assert_eq!(format!("{x:010b}"), "0000000100"); let x = BigInt::from(10); assert_eq!(format!("{x:#110b}"), "0b11111010"); From e1d145025b3f2e6a13afc25bf271a4bc83c08d9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 10:41:05 +0200 Subject: [PATCH 156/243] add formating options for Display trait --- src/lib.rs | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a8404be..016710d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ use num_traits::{Pow, ToPrimitive, Zero}; use std::cmp::Ordering; use std::error::Error; -use std::fmt::{self, Binary, Display, LowerHex, UpperHex}; +use std::fmt::{self, Alignment, Binary, Display, LowerHex, UpperHex}; use std::ops::*; use std::str::FromStr; @@ -144,13 +144,44 @@ from_uint!(u8, u16, u32, u64, u128); impl Display for BigInt { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut output = String::new(); + if !self.positive { - write!(f, "-")?; + output.push_str("-"); + } + if f.sign_plus() && self.positive { + output.push_str("+"); } for digit in &self.numbers { - write!(f, "{digit}")?; + output.push_str(&digit.to_string()); + } + + //plní se odprava + + let mut right_fill = true; + + if f.width().is_some() { + while output.len() < f.width().unwrap() { + match f.align() { + Some(Alignment::Left) => output.push(f.fill()), + Some(Alignment::Center) => { + if right_fill { + output.push(f.fill()); + right_fill = false; + } else { + output.insert(0, f.fill()); + right_fill = true; + } + } + Some(Alignment::Right) => output.insert(0, f.fill()), + _ => output.insert(0, f.fill()), + } + } } + + write!(f, "{output}")?; + Ok(()) } } From 91763f9167d13675eb3fa8b2295373ae52224da3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 11:30:04 +0200 Subject: [PATCH 157/243] add helper function .is_even() --- src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 016710d..ad58830 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -917,6 +917,14 @@ impl BigInt { Ok(BigInt { positive, numbers }) } + + fn is_even(&self) -> bool { + if (*self.numbers.last().unwrap() % 2).is_zero() { + true + } else { + false + } + } } #[cfg(test)] From e01dbc675df6f2b926208209a3f867ee935d333a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 11:30:28 +0200 Subject: [PATCH 158/243] add helper function .to_binary() --- src/lib.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index ad58830..a667ad1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -925,6 +925,27 @@ impl BigInt { false } } + + fn to_binary(&self) -> Vec { + let mut final_vec = vec![]; + + let mut number = self.clone(); + + while number != 1 { + if number.is_even() { + final_vec.push(false); + } else { + final_vec.push(true); + } + number /= 2; + } + + final_vec.push(true); + + final_vec.reverse(); + + final_vec + } } #[cfg(test)] From 7262d9b1cf1f4347b722d82e687139cd2eaf7ad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 11:39:32 +0200 Subject: [PATCH 159/243] useless commentary removal --- src/lib.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a667ad1..d566413 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -157,8 +157,6 @@ impl Display for BigInt { output.push_str(&digit.to_string()); } - //plní se odprava - let mut right_fill = true; if f.width().is_some() { From 9b461d915e3953834cd16ee12dadff6751a3a310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 12:08:24 +0200 Subject: [PATCH 160/243] add 1 to binary if negative --- src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index d566413..91a5b1f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -929,6 +929,10 @@ impl BigInt { let mut number = self.clone(); + if !number.positive { + final_vec.push(true); + } + while number != 1 { if number.is_even() { final_vec.push(false); From 32201dd756e6b361d3afdf712ce21c72fdf844e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 17:37:51 +0200 Subject: [PATCH 161/243] simplify .extend() lines --- src/lib.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 91a5b1f..d5eba6a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -319,12 +319,10 @@ fn create_numbers_set(left: BigInt, right: BigInt) -> (Vec, Vec) { } }; - let extend_count = longer.len() - shorter.len(); - longer.reverse(); shorter.reverse(); - shorter.extend(std::iter::repeat(0).take(extend_count)); + shorter.extend(vec![0; longer.len() - shorter.len()]); (shorter, longer) } @@ -451,10 +449,10 @@ where if self.numbers.len() >= right.numbers.len() { right .numbers - .extend(std::iter::repeat(0).take(self.numbers.len() - right.numbers.len())); + .extend(vec![0; self.numbers.len() - right.numbers.len()]); } else { self.numbers - .extend(std::iter::repeat(0).take(right.numbers.len() - self.numbers.len())); + .extend(vec![0; right.numbers.len() - self.numbers.len()]); } let numbers_set = self.numbers.iter().zip(right.numbers.iter()); From 6b4acc2afe65790cc7e02006958b965173e5e9b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 21:25:12 +0200 Subject: [PATCH 162/243] add more tests --- src/tests.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 2ba04d0..8afa4b5 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -722,14 +722,18 @@ mod tests { #[test] fn binary() { + let x = BigInt::from(10); + assert_eq!(format!("{x:#10b}"), " 0b1010"); let x = BigInt::from(4); assert_eq!(format!("{x:#b}"), "0b100"); let x = BigInt::from(4); assert_eq!(format!("{x:b}"), "100"); let x = BigInt::from(4); - assert_eq!(format!("{x:010b}"), "0000000100"); + assert_eq!(format!("{x:0>10b}"), "0000000100"); + let x = BigInt::from(10); + assert_eq!(format!("{x:#10b}"), " 0b1010"); let x = BigInt::from(10); - assert_eq!(format!("{x:#110b}"), "0b11111010"); + assert_eq!(format!("{x:x>#010b}"), "xxxx0b1010"); let x = BigInt::from(172); assert_eq!(format!("{x:b}"), "10101100"); let x = BigInt::from(17220003931i64); From 1c0fd8d748473ea3ee5aa6f023f8942602a78d48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 29 Jul 2025 21:25:34 +0200 Subject: [PATCH 163/243] implement binary trait --- src/lib.rs | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index d5eba6a..cbb3cd7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -186,7 +186,45 @@ impl Display for BigInt { impl Binary for BigInt { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - todo!() + let binary = self.to_binary(); + let mut output = String::new(); + + if f.alternate() { + output.push_str("0b"); + } + + for bit in binary { + if bit { + output.push('1'); + } else { + output.push('0'); + } + } + + let mut right_fill = true; + + if f.width().is_some() { + while output.len() < f.width().unwrap() { + match f.align() { + Some(Alignment::Left) => output.push(f.fill()), + Some(Alignment::Center) => { + if right_fill { + output.push(f.fill()); + right_fill = false; + } else { + output.insert(0, f.fill()); + right_fill = true; + } + } + Some(Alignment::Right) => output.insert(0, f.fill()), + _ => output.insert(0, f.fill()), + } + } + } + + write!(f, "{output}")?; + + Ok(()) } } From a25735742d0fdc6fa490e39803f518b1901cfdcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 30 Jul 2025 11:42:57 +0200 Subject: [PATCH 164/243] rewrite parse_word_digits with iterators --- src/lib.rs | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cbb3cd7..b305d48 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -920,32 +920,26 @@ impl BigInt { } fn parse_word_digits(string_of_numbers: String) -> Result { - //create lowercase iterator - let mut parsed = string_of_numbers + let mut parsed: Vec = string_of_numbers .split_whitespace() .map(str::to_lowercase) - .peekable(); - let mut positive = true; - let mut numbers: Vec = Vec::new(); + .collect(); - //if empty string - if parsed.peek().is_none() { - return Err(BigIntError::NaN); - } + let mut positive; - //positive/negative - if let Some("-" | "minus") = parsed.peek().map(String::as_str) { + if matches!(parsed.first().map(String::as_str), Some("-" | "minus")) { positive = false; - parsed.next(); + parsed.remove(0); + } else { + positive = true; } - //loop for translating words to u8 - for word in parsed { - numbers.push(BigInt::word_to_number(&word)?); - } + let numbers: Result, _> = + parsed.iter().map(|w| BigInt::word_to_number(w)).collect(); + let numbers = numbers?; //additional check - if numbers.is_empty() { + if numbers.clone().is_empty() { return Err(BigIntError::NaN); } From 6ac097214e779c714f8820539ab7d704112cbe37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 30 Jul 2025 14:17:05 +0200 Subject: [PATCH 165/243] add 0 if positive number --- src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index b305d48..ef634b4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -974,6 +974,12 @@ impl BigInt { final_vec.push(true); + if positive { + final_vec.push(false); + } else { + final_vec.push(true); + } + final_vec.reverse(); final_vec From 10d9d1a178b0ea5248725a8fc23d3e2cae758b9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 30 Jul 2025 14:17:23 +0200 Subject: [PATCH 166/243] fix binary transformation logic --- src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ef634b4..7255288 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -959,9 +959,8 @@ impl BigInt { let mut number = self.clone(); - if !number.positive { - final_vec.push(true); - } + let positive = number.positive; + number.positive = true; while number != 1 { if number.is_even() { From d22977ba65c9d3afef17c695ad06ec5414e656ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 30 Jul 2025 14:30:42 +0200 Subject: [PATCH 167/243] change tests corresponding to binary trait changes --- src/tests.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 8afa4b5..dfdbc47 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -722,22 +722,23 @@ mod tests { #[test] fn binary() { - let x = BigInt::from(10); - assert_eq!(format!("{x:#10b}"), " 0b1010"); + let x = BigInt::from(11); + assert_eq!(format!("{x:#10b}"), " 0b01011"); let x = BigInt::from(4); - assert_eq!(format!("{x:#b}"), "0b100"); + assert_eq!(format!("{x:#b}"), "0b0100"); let x = BigInt::from(4); - assert_eq!(format!("{x:b}"), "100"); + assert_eq!(format!("{x:b}"), "0100"); let x = BigInt::from(4); assert_eq!(format!("{x:0>10b}"), "0000000100"); let x = BigInt::from(10); - assert_eq!(format!("{x:#10b}"), " 0b1010"); + assert_eq!(format!("{x:#10b}"), " 0b01010"); let x = BigInt::from(10); - assert_eq!(format!("{x:x>#010b}"), "xxxx0b1010"); + assert_eq!(format!("{x:x>#010b}"), "xxx0b01010"); let x = BigInt::from(172); - assert_eq!(format!("{x:b}"), "10101100"); - let x = BigInt::from(17220003931i64); - assert_eq!(format!("{x:#b}"), "0b10000000010011001000110100001011011"); + assert_eq!(format!("{x:b}"), "010101100"); + let x = BigInt::from(-17220003931i64); + assert_eq!(format!("{x:#b}"), "0b110000000010011001000110100001011011"); + } } #[test] From 8d8c97ee1326645957f001835a8b54b1b403ff4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 30 Jul 2025 14:30:56 +0200 Subject: [PATCH 168/243] add tests for transformation from binary --- src/tests.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index dfdbc47..cc7fb23 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -739,6 +739,32 @@ mod tests { let x = BigInt::from(-17220003931i64); assert_eq!(format!("{x:#b}"), "0b110000000010011001000110100001011011"); } + + #[test] + fn binary_transformation() { + let x: BigInt = BigInt::from(10); + let bin = x.to_binary(); + assert_eq!(x, BigInt::from_binary(bin)); + + let x: BigInt = BigInt::from(-42); + let bin = x.to_binary(); + assert_eq!(x, BigInt::from_binary(bin)); + + let x: BigInt = BigInt::from(-0); + let bin = x.to_binary(); + assert_eq!(x, BigInt::from_binary(bin)); + + let x: BigInt = BigInt::from(0); + let bin = x.to_binary(); + assert_eq!(x, BigInt::from_binary(bin)); + + let x: BigInt = BigInt::from(-9999); + let bin = x.to_binary(); + assert_eq!(x, BigInt::from_binary(bin)); + + let x: BigInt = BigInt::from(666); + let bin = x.to_binary(); + assert_eq!(x, BigInt::from_binary(bin)); } #[test] From 34a2e3cba2ae86a00688eb9642d20beed2115182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 30 Jul 2025 14:53:39 +0200 Subject: [PATCH 169/243] fix zero edgecase in to_binary --- src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 7255288..23378a1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -955,6 +955,10 @@ impl BigInt { } fn to_binary(&self) -> Vec { + if *self == 0 { + return vec![false]; + } + let mut final_vec = vec![]; let mut number = self.clone(); From bcebf73147e5908945e77ef5a453b00f1a8ebfe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 30 Jul 2025 14:53:50 +0200 Subject: [PATCH 170/243] add from_binary --- src/lib.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 23378a1..09ea37c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -987,6 +987,26 @@ impl BigInt { final_vec } + + fn from_binary(binary: Vec) -> BigInt { + if binary.is_empty() { + return BigInt::default(); + } + + let positive = !binary.first().unwrap(); + + let mut result = BigInt::default(); + + for (position, bit) in binary.iter().skip(1).rev().enumerate() { + if *bit { + result += 2.pow(position); + } + } + + result.positive = positive; + + result + } } #[cfg(test)] From f76141e8143e1321ba7cca6bf749ec0488b8e62e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 30 Jul 2025 23:17:14 +0200 Subject: [PATCH 171/243] change while cycle in binary transformation --- src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 09ea37c..7c6c5c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -966,7 +966,7 @@ impl BigInt { let positive = number.positive; number.positive = true; - while number != 1 { + while number != 0 { if number.is_even() { final_vec.push(false); } else { @@ -975,8 +975,6 @@ impl BigInt { number /= 2; } - final_vec.push(true); - if positive { final_vec.push(false); } else { From 58eeca36595bdcaf2a4f319ed5408a383d7bc173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 30 Jul 2025 23:49:52 +0200 Subject: [PATCH 172/243] add and fix hexadecimal tests --- src/tests.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index cc7fb23..85ce8e0 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -962,9 +962,14 @@ mod tests { let x = BigInt::from(4); assert_eq!(format!("{x:X}"), "4"); let x = BigInt::from(16); - assert_eq!(format!("{x:#X}"), "0xF"); - let x = BigInt::from(10); assert_eq!(format!("{x:#X}"), "0x10"); + let x = BigInt::from(-4); + assert_eq!(format!("{x:X}"), "-4"); + let x = BigInt::from(-16); + assert_eq!(format!("{x:#X}"), "-0x10"); + + let x = BigInt::from(10); + assert_eq!(format!("{x:#X}"), "0xA"); let x = BigInt::from(172); assert_eq!(format!("{x:X}"), "AC"); let x = BigInt::from(17220003931u128); @@ -973,13 +978,30 @@ mod tests { let x = BigInt::from(4); assert_eq!(format!("{x:x}"), "4"); let x = BigInt::from(16); - assert_eq!(format!("{x:#x}"), "0xf"); - let x = BigInt::from(10); assert_eq!(format!("{x:#x}"), "0x10"); + let x = BigInt::from(10); + assert_eq!(format!("{x:#x}"), "0xa"); let x = BigInt::from(172); assert_eq!(format!("{x:x}"), "ac"); let x = BigInt::from(17220003931u128); assert_eq!(format!("{x:#x}"), "0x40264685b"); + + let x = BigInt::from(11); + assert_eq!(format!("{x:#10X}"), " 0xB"); + let x = BigInt::from(4); + assert_eq!(format!("{x:#x}"), "0x4"); + let x = BigInt::from(4); + assert_eq!(format!("{x:X}"), "4"); + let x = BigInt::from(4); + assert_eq!(format!("{x:0>10x}"), "0000000004"); + let x = BigInt::from(10); + assert_eq!(format!("{x:#10x}"), " 0xa"); + let x = BigInt::from(10); + assert_eq!(format!("{x:x>#010X}"), "xxxxxxx0xA"); + let x = BigInt::from(172); + assert_eq!(format!("{x:X}"), "AC"); + let x = BigInt::from(-17220003931i64); + assert_eq!(format!("{x:#x}"), "-0x40264685b"); } #[test] From 1dac3c4406a19e3345619e95f41f25fabf843deb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 30 Jul 2025 23:50:24 +0200 Subject: [PATCH 173/243] add function to transform number to hexa digit --- src/lib.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 7c6c5c6..2b870cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1005,6 +1005,44 @@ impl BigInt { result } + + fn number_to_hexa(number: BigInt) -> char { + if number == 0 { + '0' + } else if number == 1 { + '1' + } else if number == 2 { + '2' + } else if number == 3 { + '3' + } else if number == 4 { + '4' + } else if number == 5 { + '5' + } else if number == 6 { + '6' + } else if number == 7 { + '7' + } else if number == 8 { + '8' + } else if number == 9 { + '9' + } else if number == 10 { + 'A' + } else if number == 11 { + 'B' + } else if number == 12 { + 'C' + } else if number == 13 { + 'D' + } else if number == 14 { + 'E' + } else if number == 15 { + 'F' + } else { + '0' + } + } } #[cfg(test)] From 227b39e8171a1f6cd6272038829a8bbe201bb4ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 30 Jul 2025 23:51:02 +0200 Subject: [PATCH 174/243] add function to transform BigInt into hexa number --- src/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 2b870cb..c0458fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1043,6 +1043,22 @@ impl BigInt { '0' } } + + fn to_hexa(&self) -> Vec { + let mut final_vec = vec![]; + let mut number = self.clone(); + + number.positive = true; + + while number > 0 { + final_vec.push(BigInt::number_to_hexa(number.clone() % 16)); + number /= 16; + } + + final_vec.reverse(); + + final_vec + } } #[cfg(test)] From bcc568102cd9fe386c71cc0a962c3dba0e6afa59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 30 Jul 2025 23:51:30 +0200 Subject: [PATCH 175/243] add function to transform BigInt into formated string --- src/lib.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index c0458fd..323c5cd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1059,6 +1059,46 @@ impl BigInt { final_vec } + + fn create_hexa_string(&self, f: &mut fmt::Formatter<'_>) -> String { + let hexa = self.to_hexa(); + let mut output = String::new(); + + if !self.positive { + output.push('-'); + } + + if f.alternate() { + output.push_str("0x"); + } + + for hex in hexa { + output.push_str(&hex.to_uppercase().to_string()); + } + + let mut right_fill = true; + + if f.width().is_some() { + while output.len() < f.width().unwrap() { + match f.align() { + Some(Alignment::Left) => output.push(f.fill()), + Some(Alignment::Center) => { + if right_fill { + output.push(f.fill()); + right_fill = false; + } else { + output.insert(0, f.fill()); + right_fill = true; + } + } + Some(Alignment::Right) => output.insert(0, f.fill()), + _ => output.insert(0, f.fill()), + } + } + } + + output + } } #[cfg(test)] From 9c7e8c3f04f37162e867f013c582d32036fd29ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 30 Jul 2025 23:51:59 +0200 Subject: [PATCH 176/243] add implementation of LowerHex trait --- src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 323c5cd..1d44843 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -236,7 +236,11 @@ impl UpperHex for BigInt { impl LowerHex for BigInt { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - todo!() + let output = self.create_hexa_string(f).to_ascii_lowercase().to_string(); + + write!(f, "{output}")?; + + Ok(()) } } From 8153bc130ddcc3a1c8d726046f4ce8fba1a8ba2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 30 Jul 2025 23:52:07 +0200 Subject: [PATCH 177/243] add implementation of UpperHex trait --- src/lib.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 1d44843..d6ee487 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -230,7 +230,15 @@ impl Binary for BigInt { impl UpperHex for BigInt { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - todo!() + let output = self + .create_hexa_string(f) + .to_uppercase() + .to_string() + .replace("X", "x"); + + write!(f, "{output}")?; + + Ok(()) } } From 0e3417e38cb7dcecc6a1ff7194541fbbef081ec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 31 Jul 2025 00:25:31 +0200 Subject: [PATCH 178/243] add implementation of & --- src/lib.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index d6ee487..e27e098 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -770,7 +770,32 @@ where { type Output = Self; fn bitand(self, rhs: T) -> Self::Output { - todo!() + let mut right: BigInt = rhs.into(); + + let mut left = self.to_binary(); + let mut right = right.to_binary(); + let mut result = vec![]; + + left.reverse(); + right.reverse(); + + let (longer, mut shorter) = if left.len() > right.len() { + (left, right) + } else { + (right, left) + }; + + shorter.extend(vec![false; longer.len() - shorter.len()]); + + let binary_set = longer.iter().zip(shorter.iter()); + + for (left, right) in binary_set { + result.push(left & right); + } + + result.reverse(); + + BigInt::from_binary(result) } } From 6462fa3facd20b5b84b777bf610d70dcb0646fa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 31 Jul 2025 00:25:35 +0200 Subject: [PATCH 179/243] add implementation of | --- src/lib.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index e27e098..b55832e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -814,7 +814,32 @@ where { type Output = Self; fn bitor(self, rhs: T) -> Self::Output { - todo!() + let mut right: BigInt = rhs.into(); + + let mut left = self.to_binary(); + let mut right = right.to_binary(); + let mut result = vec![]; + + left.reverse(); + right.reverse(); + + let (longer, mut shorter) = if left.len() > right.len() { + (left, right) + } else { + (right, left) + }; + + shorter.extend(vec![false; longer.len() - shorter.len()]); + + let binary_set = longer.iter().zip(shorter.iter()); + + for (left, right) in binary_set { + result.push(left | right); + } + + result.reverse(); + + BigInt::from_binary(result) } } From f6d3b5b73dcabcf2945cfa1a40a5b42cae6991e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 31 Jul 2025 00:25:40 +0200 Subject: [PATCH 180/243] add implementation of ^ --- src/lib.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index b55832e..fb71f3b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -858,7 +858,32 @@ where { type Output = Self; fn bitxor(self, rhs: T) -> Self::Output { - todo!() + let mut right: BigInt = rhs.into(); + + let mut left = self.to_binary(); + let mut right = right.to_binary(); + let mut result = vec![]; + + left.reverse(); + right.reverse(); + + let (longer, mut shorter) = if left.len() > right.len() { + (left, right) + } else { + (right, left) + }; + + shorter.extend(vec![false; longer.len() - shorter.len()]); + + let binary_set = longer.iter().zip(shorter.iter()); + + for (left, right) in binary_set { + result.push(left ^ right); + } + + result.reverse(); + + BigInt::from_binary(result) } } From c25e2af49473edd00245e1bac7459428218b6752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 31 Jul 2025 00:41:48 +0200 Subject: [PATCH 181/243] fix some tests --- src/tests.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 85ce8e0..03f80d4 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1074,19 +1074,17 @@ mod tests { assert!(a >= 73786976294838206464u128); assert!(a == 73786976294838206464u128); assert!(!(a != 73786976294838206464i128)); - assert!(a < 73786976294838206464i128); + assert!(!(a < 73786976294838206464i128)); assert!(a <= 73786976294838206464u128); assert!(!(a > 73786976294838206464i128)); - assert!(!(a >= 73786976294838206464u128)); - assert!(!(a == 73786976294838206465u128)); - assert!(a != 73786976294838206465i128); + assert!(a >= 73786976294838206464u128); a = BigInt::from_str("2147483648").unwrap_or(BigInt::new()); - assert!(!(a < -2147483648)); - assert!(!(a <= -2147483648)); - assert!(a > -2147483648); - assert!(a >= -2147483648); - assert!(!(a == -2147483648)); - assert!(a != -2147483648); + assert!(!(a < -2147483648 as i128)); + assert!(!(a <= -2147483648 as i128)); + assert!(a > -2147483648 as i128); + assert!(a >= -2147483648 as i128); + assert!(!(a == -2147483648 as i128)); + assert!(a != -2147483648 as i128); a = BigInt::from_str("-12345678").unwrap_or(BigInt::new()); assert!(!(a < -87654321)); assert!(!(a <= -87654321)); From 90217dd8a1438f422e3c958e16d856237d08745c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 31 Jul 2025 00:42:12 +0200 Subject: [PATCH 182/243] fix PartialOrd trait --- src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index fb71f3b..c712fb8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -337,6 +337,10 @@ impl PartialOrd for BigInt { break; } } + + if !self.positive { + greater = !greater; + } } //return value From 3198ada8bb0a8af8870b3c3a5f60315f02b30fed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 31 Jul 2025 10:34:11 +0200 Subject: [PATCH 183/243] change lib.rs to make Clippy happy again --- src/lib.rs | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c712fb8..aaca418 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,14 +57,14 @@ impl FromStr for BigInt { return BigInt::parse_word_digits(if positive { string_of_numbers.to_string() } else { - format!("-{}", string_of_numbers) + format!("-{string_of_numbers}") }); } numbers.push(char.to_digit(10).unwrap().to_u8().unwrap()); } - if numbers.as_slice() == &[0] { + if numbers.as_slice() == [0] { positive = true; } @@ -147,10 +147,10 @@ impl Display for BigInt { let mut output = String::new(); if !self.positive { - output.push_str("-"); + output.push('-'); } if f.sign_plus() && self.positive { - output.push_str("+"); + output.push('+'); } for digit in &self.numbers { @@ -354,7 +354,7 @@ impl PartialOrd for BigInt { impl PartialOrd<&str> for BigInt { fn partial_cmp(&self, other: &&str) -> Option { - let other = BigInt::from_str(&other); + let other = BigInt::from_str(other); if other.is_err() { return None; @@ -672,13 +672,9 @@ where left.numbers.push(*digit); new_digit = 0; - while &left >= &right { + while left >= right { left -= right.clone(); new_digit += 1; - - if new_digit > 10 { - assert!(false); - } } result.numbers.push(new_digit); @@ -720,7 +716,7 @@ where if right == 0 { panic!("division by zero!"); } - if self == 0 || right == 1 || right == 1 { + if self == 0 || right == 1 { return 0.into(); } @@ -1038,11 +1034,7 @@ impl BigInt { } fn is_even(&self) -> bool { - if (*self.numbers.last().unwrap() % 2).is_zero() { - true - } else { - false - } + (*self.numbers.last().unwrap() % 2).is_zero() } fn to_binary(&self) -> Vec { From 5317725f2164a634b27b871a4e28d58b07deb6b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Thu, 31 Jul 2025 10:46:53 +0200 Subject: [PATCH 184/243] change tests.rs to make Clippy happy again --- src/tests.rs | 2177 +++++++++++++++++++++++++------------------------- 1 file changed, 1082 insertions(+), 1095 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 03f80d4..e490986 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,1096 +1,1083 @@ -mod tests { - use std::str::FromStr; - - use num_traits::Pow; - - use crate::BigInt; - use crate::BigIntError; - - #[test] - fn default() { - let def = BigInt::default(); - assert_eq!(def.positive, true); - assert_eq!(def.numbers, [0].to_vec()); - } - - #[test] - fn new() { - let new = BigInt::new(); - assert_eq!(new.positive, true); - assert_eq!(new.numbers, [0].to_vec()); - } - - #[test] - fn from() { - let x = BigInt::from(20); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [2, 0].to_vec()); - let x = BigInt::from(-20); - assert_eq!(x.positive, false); - assert_eq!(x.numbers, [2, 0].to_vec()); - let x = BigInt::from(-320020000981234567890i128); - assert_eq!(x.positive, false); - assert_eq!( - x.numbers, - [ - 3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 - ] - .to_vec() - ); - let x = BigInt::from(0); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [0].to_vec()); - let x = BigInt::from(-0); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [0].to_vec()); - } - - #[test] - fn from_string_numbers() { - let x = BigInt::from_str("20").unwrap(); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [2, 0].to_vec()); - let x = BigInt::from_str("666").unwrap(); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [6, 6, 6].to_vec()); - let x = BigInt::from_str("-20").unwrap(); - assert_eq!(x.positive, false); - assert_eq!(x.numbers, [2, 0].to_vec()); - let x = BigInt::from_str("-320020000981234567890").unwrap(); - assert_eq!(x.positive, false); - assert_eq!( - x.numbers, - [ - 3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 - ] - .to_vec() - ); - let x = BigInt::from_str("-0").unwrap(); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [0].to_vec()); - let x = BigInt::from_str("0sw"); - assert!(x.is_err()); - let x = BigInt::from_str("0 2020000"); - assert!(x.is_err()); - let x = BigInt::from_str("--200"); - assert!(x.is_err()); - let x = BigInt::from_str("+2000303"); - assert!(x.is_err()); - let x = BigInt::from_str("minus20003002"); - assert!(x.is_err()); - } - - #[test] - fn from_string_words_from_str_digits() { - let x = BigInt::from_str("two zero ").unwrap(); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [2, 0].to_vec()); - let x = BigInt::from_str("minus two four").unwrap(); - assert_eq!(x.positive, false); - assert_eq!(x.numbers, [2, 4].to_vec()); - let x = BigInt::from_str("two five five zero zero two one").unwrap(); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [2, 5, 5, 0, 0, 2, 1].to_vec()); - let x = BigInt::from_str("minus two zero zero zero zero zero one").unwrap(); - assert_eq!(x.positive, false); - assert_eq!(x.numbers, [2, 0, 0, 0, 0, 0, 1].to_vec()); - let x = BigInt::from_str("zero").unwrap(); - assert_eq!(x.positive, true); - assert_eq!(x.numbers, [0].to_vec()); - let x = BigInt::from_str("onse"); - assert!(x.is_err()); - let x = BigInt::from_str(" "); - assert!(x.is_err()); - let x = BigInt::from_str("twenty thousand thousand"); - assert!(x.is_err()); - let x: Result = BigInt::from_str("twenty thousand hundred"); - assert!(x.is_err()); - let x = BigInt::from_str("- five four").unwrap(); - assert_eq!(x.positive, false); - assert_eq!(x.numbers, [5, 4].to_vec()); - } - - #[test] - fn display() { - let x = BigInt::from(1003); - assert_eq!(format!("Number is: {x:0^10}"), "Number is: 0001003000"); - let x = BigInt::from(1003); - assert_eq!(format!("Number is: {x:0>10}"), "Number is: 0000001003"); - let x = BigInt::from(1003); - assert_eq!(format!("Number is: {x:0<10}"), "Number is: 1003000000"); - let x = BigInt::from(1003); - assert_eq!(format!("Number is: {x}"), "Number is: 1003"); - let x = BigInt::from(0); - assert_eq!(format!("Number is: {x}"), "Number is: 0"); - let x = BigInt::from(-100); - assert_eq!(format!("Number is: {x}"), "Number is: -100"); - let x = BigInt::from(-0); - assert_eq!(format!("Number is: {x}"), "Number is: 0"); - let x = BigInt::from(-1); - assert_eq!(format!("Number is: {x}"), "Number is: -1"); - let x = BigInt::from(320020000981234567890i128); - assert_eq!( - format!("Number is: {x}"), - "Number is: 320020000981234567890" - ); - let x = BigInt::from(1003); - assert_eq!(format!("Number is: {x:->10}"), "Number is: ------1003"); - let x = BigInt::from(1003); - assert_eq!(format!("Number is: {x:9}"), "Number is: 1003"); - let x = BigInt::from(0); - assert_eq!(format!("Number is: {x:2}"), "Number is: 0"); - let x = BigInt::from(-100); - assert_eq!(format!("Number is: {x}"), "Number is: -100"); - let x = BigInt::from(-0); - assert_eq!(format!("Number is: {x}"), "Number is: 0"); - let x = BigInt::from(-100); - assert_eq!(format!("Number is: {x:X>5}"), "Number is: X-100"); - let x = BigInt::from(-0); - assert_eq!(format!("Number is: {x:+}"), "Number is: +0"); - } - - #[test] - fn to_words() { - let x = BigInt::from(20); - assert_eq!(x.to_words(), "two zero"); - let x = BigInt::from(-24); - assert_eq!(x.to_words(), "minus two four"); - let x = BigInt::from(2550021); - assert_eq!(x.to_words(), "two five five zero zero two one"); - let x = BigInt::from(-2000000000001i128); - assert_eq!( - x.to_words(), - "minus two zero zero zero zero zero zero zero zero zero zero zero one" - ); - } - - #[test] - fn not() { - let mut x = BigInt::from(1); - assert_eq!(x.positive, true); - assert_eq!(x.to_string(), "1"); - x = -x; - assert_eq!(x.positive, false); - assert_eq!(x.to_string(), "-1"); - x = -x; - assert_eq!(x.positive, true); - assert_eq!(x.to_string(), "1"); - let mut x = BigInt::from(-22); - assert_eq!(x.positive, false); - assert_eq!(x.to_string(), "-22"); - x = -x; - assert_eq!(x.positive, true); - assert_eq!(x.to_string(), "22"); - } - - #[test] - fn equal() { - let x = BigInt::from(10); - let y = BigInt::from(10); - assert!(x == y); - let x = BigInt::from(101010); - let y = BigInt::from(101010); - assert!(x == y); - let x = BigInt::from(101010); - let y = BigInt::from(101210); - assert!(!(x == y)); - let x = BigInt::from(101010); - let y = BigInt::from(1); - assert!(!(x == y)); - let x = BigInt::from(0); - let y = BigInt::from(0); - assert!(x == y); - let x = BigInt::from(10); - let y = BigInt::from(-10); - assert!(x != y); - let x = BigInt::from(11); - let y = BigInt::from(10); - assert!(x != y); - let x = BigInt::from(-0); - let y = BigInt::from(0); - assert!(x == y); - } - - #[test] - fn equal_int() { - let x = BigInt::from(10); - assert!(x == 10); - let x = BigInt::from(101010); - assert!(x == 101010); - let x = BigInt::from(101010); - assert!(!(x == 101210)); - let x = BigInt::from(101010); - assert!(!(x == 1)); - let x = BigInt::from(0); - assert!(x == 0); - let x = BigInt::from(10); - assert!(x != -10); - let x = BigInt::from(11); - assert!(x != 10); - let x = BigInt::from(-0); - assert!(x == 0); - } - - #[test] - fn equal_str() { - let x = BigInt::from(10); - assert!(x == "10"); - let x = BigInt::from(101010); - assert!(x == "101010"); - let x = BigInt::from(101010); - assert!(!(x == "101210")); - let x = BigInt::from(101010); - assert!(!(x == "1")); - let x = BigInt::from(0); - assert!(x == "0"); - let x = BigInt::from(10); - assert!(x != "-10"); - let x = BigInt::from(11); - assert!(x != "10"); - let x = BigInt::from(-0); - assert!(x == "0"); - } - - #[test] - fn greater() { - let x: BigInt = BigInt::from(15); - let y = BigInt::from(10); - assert!(x > y); - let x: BigInt = BigInt::from(8); - let y = BigInt::from(7); - assert!(x > y); - let x = BigInt::from(10); - let y = BigInt::from(10); - assert!(!(x > y)); - let x = BigInt::from(10); - let y = BigInt::from(10); - assert!(x >= y); - let x = BigInt::from(101010); - let y = BigInt::from(101010); - assert!(x >= y); - let x = BigInt::from(0); - let y = BigInt::from(0); - assert!(!(x > y)); - let x = BigInt::from(10); - let y = BigInt::from(-10); - assert!(x > y); - let x = BigInt::from(11); - let y = BigInt::from(10); - assert!(x > y); - } - - #[test] - fn lesser() { - let x: BigInt = BigInt::from(0); - let y = BigInt::from(10); - assert!(x < y); - let x: BigInt = BigInt::from(8); - let y = BigInt::from(9); - assert!(x < y); - let x = BigInt::from(10); - let y = BigInt::from(10); - assert!(!(x < y)); - let x = BigInt::from(10); - let y = BigInt::from(10); - assert!(x <= y); - let x = BigInt::from(99999999999i64); - let y = BigInt::from(99999999999i128); - assert!(x <= y); - let x = BigInt::from(0); - let y = BigInt::from(0); - assert!(!(x < y)); - let x = BigInt::from(-10); - let y = BigInt::from(10); - assert!(x < y); - let x = BigInt::from(11); - let y = BigInt::from(99999999999u64); - assert!(x < y); - let x = BigInt::from(10000010); - let y = BigInt::from(20000000); - assert!(x < y); - } - - #[test] - fn add() { - let mut x: BigInt = BigInt::from(1000); - let y = BigInt::from(10); - let z = x.clone() + y.clone(); - x += y; - assert_eq!(z, 1010); - assert_eq!(x, z); - - let mut x: BigInt = BigInt::from(10); - let y = BigInt::from(10); - let z = x.clone() + y.clone(); - x += y; - assert_eq!(z, 20); - assert_eq!(x, z); - - let mut x = BigInt::from(101010); - let y = BigInt::from(101010); - let z = x.clone() + y.clone(); - x += y; - assert_eq!(z, 202020); - assert_eq!(x, z); - - let mut x = BigInt::from(0); - let y = BigInt::from(0); - let z = x.clone() + y.clone(); - x += y; - assert_eq!(z, 0); - assert_eq!(x, z); - - let mut x = BigInt::from(10); - let y = BigInt::from(-10); - let z = x.clone() + y.clone(); - x += y.clone(); - assert_eq!(z, 0); - assert_ne!(x, y); - - let mut x = BigInt::from(11); - let y = BigInt::from(10); - let z = x.clone() + y.clone(); - x += y.clone(); - assert_eq!(z, 21); - assert_ne!(x, y); - - let mut x = BigInt::from(-0); - let y = BigInt::from(0); - let z = x.clone() + y.clone(); - x += y.clone(); - assert_eq!(z, 0); - assert_eq!(x, z); - - let mut x = BigInt::from(6); - let y = BigInt::from(4); - let z = x.clone() + y.clone(); - x += y.clone(); - assert_eq!(z, 10); - assert_eq!(x, z); - - let mut x = BigInt::from(-15); - let y = BigInt::from(-4); - let z = x.clone() + y.clone(); - x += y.clone(); - assert_eq!(z, -19); - assert_eq!(x, z); - } - - #[test] - fn sub() { - let mut x: BigInt = BigInt::from(10); - let y = BigInt::from(10); - let z = x.clone() - y.clone(); - x -= y; - assert_eq!(z, 0); - assert_eq!(x, z); - - let mut x = BigInt::from(101010); - let y = BigInt::from(10); - let z = x.clone() - y.clone(); - x -= y; - assert_eq!(z, 101000); - assert_eq!(x, z); - - let mut x = BigInt::from(0); - let y = BigInt::from(0); - let z = x.clone() - y.clone(); - x -= y; - assert_eq!(z, 0); - assert_eq!(x, z); - - let mut x = BigInt::from(10); - let y = BigInt::from(-10); - let z = x.clone() - y.clone(); - x -= y.clone(); - assert_eq!(z, 20); - assert_ne!(x, y); - - let mut x = BigInt::from(-10); - let y = BigInt::from(10); - let z = x.clone() - y.clone(); - x -= y.clone(); - assert_eq!(z, -20); - assert_ne!(x, y); - - let mut x = BigInt::from(11); - let y = BigInt::from(10); - let z = x.clone() - y.clone(); - x -= y.clone(); - assert_eq!(z, 1); - assert_ne!(x, y); - - let mut x = BigInt::from(-0); - let y = BigInt::from(0); - let z = x.clone() - y.clone(); - x -= y.clone(); - assert_eq!(z, 0); - assert_eq!(x, z); - - let mut x = BigInt::from(6); - let y = BigInt::from(4); - let z = x.clone() - y.clone(); - x -= y.clone(); - assert_eq!(z, 2); - assert_eq!(x, z); - - let mut x = BigInt::from(4); - let y = BigInt::from(15); - let z = x.clone() - y.clone(); - x -= y.clone(); - assert_eq!(z, -11); - assert_eq!(x, z); - - let mut x = BigInt::from(-15); - let y = BigInt::from(-4); - let z = x.clone() - y.clone(); - x -= y.clone(); - assert_eq!(z, -11); - assert_eq!(x, z); - - let mut x = BigInt::from(987654); - let y = BigInt::from(987653); - let z = x.clone() - y.clone(); - x -= y.clone(); - assert_eq!(z, 1); - assert_eq!(x, z); - - let mut x = BigInt::from(1); - let y = BigInt::from(1000); - let z = x.clone() - y.clone(); - x -= y.clone(); - assert_eq!(z, -999); - assert_eq!(x, z); - } - - #[test] - fn mul() { - let mut x: BigInt = BigInt::from(10); - let y = BigInt::from(10); - let z: BigInt = x.clone() * y.clone(); - x *= y; - assert_eq!(z, 100); - assert_eq!(z, x); - - let mut x = BigInt::from(101); - let y = BigInt::from(101); - let z = x.clone() * y.clone(); - x *= y; - assert_eq!(z, 10201); - assert_eq!(z, x); - - let mut x = BigInt::from(0); - let y = BigInt::from(0); - let z: BigInt = x.clone() * y.clone(); - x *= y; - assert_eq!(z, 0); - assert_eq!(z, x); - - let mut x = BigInt::from(20100000100u64); - let y = BigInt::from(0); - let z: BigInt = x.clone() * y.clone(); - x *= y; - assert_eq!(z, 0); - assert_eq!(z, x); - - let mut x = BigInt::from(10); - let y = BigInt::from(-10); - let z: BigInt = x.clone() * y.clone(); - x *= y.clone(); - assert_eq!(z, -100); - assert_ne!(x, y); - - let mut x = BigInt::from(11); - let y = BigInt::from(10); - let z: BigInt = x.clone() * y.clone(); - x *= y.clone(); - assert_eq!(z, 110); - assert_ne!(x, y); - - let mut x = BigInt::from(-0); - let y = BigInt::from(0); - let z: BigInt = x.clone() * y.clone(); - x *= y.clone(); - assert_eq!(z, 0); - assert_eq!(x, y); - - let mut x = BigInt::from(6); - let y = BigInt::from(4); - let z: BigInt = x.clone() * y.clone(); - x *= y.clone(); - assert_eq!(z, 24); - assert_eq!(x, z); - - let mut x = BigInt::from(-15); - let y = BigInt::from(-4); - let z: BigInt = x.clone() * y.clone(); - x *= y.clone(); - assert_eq!(z, 60); - assert_eq!(x, z); - } - - #[test] - fn div() { - let mut x: BigInt = BigInt::from(10000000); - let y = BigInt::from(10); - let z = x.clone() / y.clone(); - x /= y; - assert_eq!(z, 1000000); - assert_eq!(z, x); - - let mut x = BigInt::from(101); - let y = BigInt::from(101); - let z = x.clone() / y.clone(); - x /= y; - assert_eq!(z, 1); - assert_eq!(z, x); - - let mut x = BigInt::from(9999u128); - let y = BigInt::from(2); - let z = x.clone() / y.clone(); - x /= y; - assert_eq!(z, 4999u128); - assert_eq!(z, x); - - let mut x = BigInt::from_str("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999").unwrap(); - let y = BigInt::from(2); - let z = x.clone() / y.clone(); - x /= y; - assert_eq!( - z, - "499999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" - ); - assert_eq!(z, x); - - let mut x = BigInt::from(0); - let y = BigInt::from(2); - let z = x.clone() / y.clone(); - x /= y; - assert_eq!(z, 0); - assert_eq!(z, x); - - let mut x = BigInt::from(1); - let y = BigInt::from(2); - let z = x.clone() / y.clone(); - x /= y; - assert_eq!(z, 0); - assert_eq!(z, x); - - let mut x = BigInt::from(1); - let y = BigInt::from(1); - let z = x.clone() / y.clone(); - x /= y; - assert_eq!(z, 1); - assert_eq!(z, x); - - let mut x = BigInt::from(0); - let y = BigInt::from(666); - let z = x.clone() / y.clone(); - x /= y; - assert_eq!(z, 0); - assert_eq!(z, x); - - let mut x = BigInt::from(0); - let y = BigInt::from(100); - let z = x.clone() / y.clone(); - x /= y; - assert_eq!(z, 0); - assert_eq!(z, x); - - let mut x = BigInt::from(20100000100u64); - let y = BigInt::from(20000000); - let z = x.clone() / y.clone(); - x /= y; - assert_eq!(z, 1005); - assert_eq!(z, x); - - let mut x = BigInt::from(10); - let y = BigInt::from(-10); - let z = x.clone() / y.clone(); - x /= y.clone(); - assert_eq!(z, -1); - assert_ne!(x, y); - - let mut x = BigInt::from(110); - let y = BigInt::from(10); - let z = x.clone() / y.clone(); - x /= y.clone(); - assert_eq!(z, 11); - assert_ne!(x, y); - - let mut x = BigInt::from(6); - let y = BigInt::from(2); - let z = x.clone() / y.clone(); - x /= y.clone(); - assert_eq!(z, 3); - assert_eq!(x, z); - - let mut x = BigInt::from(-15); - let y = BigInt::from(-5); - let z = x.clone() / y.clone(); - x /= y.clone(); - assert_eq!(z, 3); - assert_eq!(x, z); - } - - #[test] - #[should_panic] - fn div_by_zero() { - let mut x = BigInt::from(10); - let y = BigInt::from(0); - let z = x / y; - } - - #[test] - fn reminder() { - let mut x: BigInt = BigInt::from(10000); - let y = BigInt::from(10); - let z = x.clone() % y.clone(); - x %= y; - assert_eq!(z, 0); - assert_eq!(z, x); - - let mut x: BigInt = BigInt::from(10); - let y = BigInt::from(7); - let z = x.clone() % y.clone(); - x %= y; - assert_eq!(z, 3); - assert_eq!(z, x); - - let mut x: BigInt = BigInt::from(10000); - let y = BigInt::from(10); - let z = x.clone() % y.clone(); - x %= y; - assert_eq!(z, 0); - assert_eq!(z, x); - - let mut x: BigInt = BigInt::from(-104); - let y = BigInt::from(10); - let z = x.clone() % y.clone(); - x %= y; - assert_eq!(z, -4); - assert_eq!(z, x); - - let mut x: BigInt = BigInt::from(24); - let y = BigInt::from(3); - let z = x.clone() % y.clone(); - x %= y; - assert_eq!(z, 0); - assert_eq!(z, x); - - let mut x: BigInt = BigInt::from(33); - let y = BigInt::from(7); - let z = x.clone() % y.clone(); - x %= y; - assert_eq!(z, 5); - assert_eq!(z, x); - } - - #[test] - fn power_of() { - let mut x: BigInt = BigInt::from(2); - let y = BigInt::from(2); - let z = x.pow(y); - assert_eq!(z, 4); - - let mut x: BigInt = BigInt::from(2); - let y = BigInt::from(1); - let z = x.pow(y); - assert_eq!(z, 2); - - let mut x: BigInt = BigInt::from(1); - let y = BigInt::from(1000000); - let z = x.pow(y); - assert_eq!(z, 1); - - let mut x: BigInt = BigInt::from(3); - let y = BigInt::from(3); - let z = x.pow(y); - assert_eq!(z, 27); - } - - #[test] - #[should_panic] - fn rem_of_zero() { - let mut x: BigInt = BigInt::from(10); - let y = BigInt::from(0); - let z = x.clone() % y.clone(); - x %= y; - assert_eq!(z, 0); - assert_eq!(z, x); - } - - #[test] - fn binary() { - let x = BigInt::from(11); - assert_eq!(format!("{x:#10b}"), " 0b01011"); - let x = BigInt::from(4); - assert_eq!(format!("{x:#b}"), "0b0100"); - let x = BigInt::from(4); - assert_eq!(format!("{x:b}"), "0100"); - let x = BigInt::from(4); - assert_eq!(format!("{x:0>10b}"), "0000000100"); - let x = BigInt::from(10); - assert_eq!(format!("{x:#10b}"), " 0b01010"); - let x = BigInt::from(10); - assert_eq!(format!("{x:x>#010b}"), "xxx0b01010"); - let x = BigInt::from(172); - assert_eq!(format!("{x:b}"), "010101100"); - let x = BigInt::from(-17220003931i64); - assert_eq!(format!("{x:#b}"), "0b110000000010011001000110100001011011"); - } - - #[test] - fn binary_transformation() { - let x: BigInt = BigInt::from(10); - let bin = x.to_binary(); - assert_eq!(x, BigInt::from_binary(bin)); - - let x: BigInt = BigInt::from(-42); - let bin = x.to_binary(); - assert_eq!(x, BigInt::from_binary(bin)); - - let x: BigInt = BigInt::from(-0); - let bin = x.to_binary(); - assert_eq!(x, BigInt::from_binary(bin)); - - let x: BigInt = BigInt::from(0); - let bin = x.to_binary(); - assert_eq!(x, BigInt::from_binary(bin)); - - let x: BigInt = BigInt::from(-9999); - let bin = x.to_binary(); - assert_eq!(x, BigInt::from_binary(bin)); - - let x: BigInt = BigInt::from(666); - let bin = x.to_binary(); - assert_eq!(x, BigInt::from_binary(bin)); - } - - #[test] - fn bit_and() { - let mut x = BigInt::from(4); //100 - let y = BigInt::from(12); //1100 - let z = x.clone() & y.clone(); //0100 - x &= y; - assert_eq!(x, z); - assert_eq!(z, 4); - - let mut x = BigInt::from(10); //1010 - let y = BigInt::from(13); //1101 - let z = x.clone() & y.clone(); //1000 - x &= y; - assert_eq!(x, z); - assert_eq!(z, 8); - - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(223); //11011111 - let z = x.clone() & y.clone(); //10001100 - x &= y; - assert_eq!(x, z); - assert_eq!(z, 140); - - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(1); //1 - let z = x.clone() & y.clone(); //0 - x &= y; - assert_eq!(x, z); - assert_eq!(z, 0); - - let mut x = BigInt::from(173); //10101101 - let y = BigInt::from(1); //1 - let z = x.clone() & y.clone(); //1 - x &= y; - assert_eq!(x, z); - assert_eq!(z, 1); - } - - #[test] - fn bit_or() { - let mut x = BigInt::from(4); //100 - let y = BigInt::from(12); //1100 - let z = x.clone() | y.clone(); //1100 - x |= y; - assert_eq!(x, z); - assert_eq!(z, 12); - - let mut x = BigInt::from(10); //1010 - let y = BigInt::from(13); //1101 - let z = x.clone() | y.clone(); //1111 - x |= y; - assert_eq!(x, z); - assert_eq!(z, 15); - - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(223); //11011111 - let z = x.clone() | y.clone(); //11111111 - x |= y; - assert_eq!(x, z); - assert_eq!(z, 255); - - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(1); //1 - let z = x.clone() | y.clone(); //10101101 - x |= y; - assert_eq!(x, z); - assert_eq!(z, 173); - - let mut x = BigInt::from(173); //10101101 - let y = BigInt::from(1); //1 - let z = x.clone() | y.clone(); //10101101 - x |= y; - assert_eq!(x, z); - assert_eq!(z, 173); - } - - #[test] - fn bit_xor() { - let mut x = BigInt::from(4); //100 - let y = BigInt::from(12); //1100 - let z = x.clone() ^ y.clone(); //1000 - x ^= y; - assert_eq!(x, z); - assert_eq!(z, 8); - - let mut x = BigInt::from(10); //1010 - let y = BigInt::from(13); //1101 - let z = x.clone() ^ y.clone(); //0111 - x ^= y; - assert_eq!(x, z); - assert_eq!(z, 7); - - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(223); //11011111 - let z = x.clone() ^ y.clone(); //01110011 - x ^= y; - assert_eq!(x, z); - assert_eq!(z, 115); - - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(1); //1 - let z = x.clone() ^ y.clone(); //10101101 - x ^= y; - assert_eq!(x, z); - assert_eq!(z, 173); - - let mut x = BigInt::from(173); //10101101 - let y = BigInt::from(1); //1 - let z = x.clone() ^ y.clone(); //10101100 - x ^= y; - assert_eq!(x, z); - assert_eq!(z, 172); - } - - #[test] - fn bit_shift_left() { - let mut x = BigInt::from(4); //100 - let y = BigInt::from(2); - let z = x.clone() << y.clone(); //10000 - x <<= y; - assert_eq!(x, z); - assert_eq!(z, 16); - - let mut x = BigInt::from(10); //1010 - let y = BigInt::from(1); - let z = x.clone() << y.clone(); //10100 - x <<= y; - assert_eq!(x, z); - assert_eq!(z, 20); - - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(0); - let z = x.clone() << y.clone(); - x <<= y; - assert_eq!(x, z); - assert_eq!(z, 172); - - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(10); - let z = x.clone() << y.clone(); //101011000000000000 - x <<= y; - assert_eq!(x, z); - assert_eq!(z, 176128); - - let mut x = BigInt::from(173); //10101101 - let y = BigInt::from(1); - let z = x.clone() << y.clone(); //101011010 - x <<= y; - assert_eq!(x, z); - assert_eq!(z, 346); - } - - #[test] - fn bit_shift_right() { - let mut x = BigInt::from(4); //100 - let y = BigInt::from(2); - let z = x.clone() >> y.clone(); //1 - x >>= y; - assert_eq!(x, z); - assert_eq!(z, 1); - - let mut x = BigInt::from(10); //1010 - let y = BigInt::from(1); - let z = x.clone() >> y.clone(); //101 - x >>= y; - assert_eq!(x, z); - assert_eq!(z, 5); - - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(0); - let z = x.clone() >> y.clone(); //10101100 - x >>= y; - assert_eq!(x, z); - assert_eq!(z, 172); - - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(10); - let z = x.clone() >> y.clone(); //0 - x >>= y; - assert_eq!(x, z); - assert_eq!(z, 0); - - let mut x = BigInt::from(173); //10101101 - let y = BigInt::from(1); - let z = x.clone() >> y.clone(); //1010110 - x >>= y; - assert_eq!(x, z); - assert_eq!(z, 86); - } - - #[test] - fn hexadecimal() { - let x = BigInt::from(4); - assert_eq!(format!("{x:X}"), "4"); - let x = BigInt::from(16); - assert_eq!(format!("{x:#X}"), "0x10"); - let x = BigInt::from(-4); - assert_eq!(format!("{x:X}"), "-4"); - let x = BigInt::from(-16); - assert_eq!(format!("{x:#X}"), "-0x10"); - - let x = BigInt::from(10); - assert_eq!(format!("{x:#X}"), "0xA"); - let x = BigInt::from(172); - assert_eq!(format!("{x:X}"), "AC"); - let x = BigInt::from(17220003931u128); - assert_eq!(format!("{x:#X}"), "0x40264685B"); - - let x = BigInt::from(4); - assert_eq!(format!("{x:x}"), "4"); - let x = BigInt::from(16); - assert_eq!(format!("{x:#x}"), "0x10"); - let x = BigInt::from(10); - assert_eq!(format!("{x:#x}"), "0xa"); - let x = BigInt::from(172); - assert_eq!(format!("{x:x}"), "ac"); - let x = BigInt::from(17220003931u128); - assert_eq!(format!("{x:#x}"), "0x40264685b"); - - let x = BigInt::from(11); - assert_eq!(format!("{x:#10X}"), " 0xB"); - let x = BigInt::from(4); - assert_eq!(format!("{x:#x}"), "0x4"); - let x = BigInt::from(4); - assert_eq!(format!("{x:X}"), "4"); - let x = BigInt::from(4); - assert_eq!(format!("{x:0>10x}"), "0000000004"); - let x = BigInt::from(10); - assert_eq!(format!("{x:#10x}"), " 0xa"); - let x = BigInt::from(10); - assert_eq!(format!("{x:x>#010X}"), "xxxxxxx0xA"); - let x = BigInt::from(172); - assert_eq!(format!("{x:X}"), "AC"); - let x = BigInt::from(-17220003931i64); - assert_eq!(format!("{x:#x}"), "-0x40264685b"); - } - - #[test] - fn progtest_tests() { - let mut a = BigInt::new(); - let mut b = BigInt::new(); - a = BigInt::from(10); - a += BigInt::from(20); - assert_eq!(a, 30); - a *= BigInt::from(5); - assert_eq!(a, 150); - b = a.clone() + BigInt::from(3); - assert_eq!(b, 153); - b = a.clone() * BigInt::from(7); - assert_eq!(b, 1050); - assert_eq!(a, 150); - assert_eq!(format!("{a:X}"), "96"); - - a = BigInt::from(10); - a += BigInt::from(-20); - assert_eq!(a, -10); - a *= BigInt::from(5); - assert_eq!(a, -50); - b = a.clone() + BigInt::from(73); - assert_eq!(b, 23); - b = a.clone() * BigInt::from(-7); - assert_eq!(b, 350); - assert_eq!(a, -50); - assert_eq!(format!("{a:X}"), "-32"); - - a = BigInt::from(12345678901234567890i128); - a += BigInt::from(-99999999999999999999i128); - assert_eq!(a, -87654321098765432109i128); - a *= BigInt::from(54321987654321987654i128); - assert_eq!(a, "-4761556948575111126880627366067073182286"); - a *= BigInt::from(0); - assert_eq!(a, 0); - a = BigInt::from(10); - b = a.clone() + 400; - assert_eq!(b, "410"); - b = a.clone() * BigInt::from_str("15").unwrap_or_default(); - assert_eq!(b, "150"); - assert_eq!(a, "10"); - assert_eq!(format!("{a:X}"), "A"); - - b = BigInt::from_str("1234").unwrap_or_default(); - assert_eq!(format!("{b}"), "1234"); - assert!(BigInt::from_str(" 12 34").is_err()); - assert!(BigInt::from_str("999z").is_err()); - assert!(BigInt::from_str("abcd").is_err()); - assert!(BigInt::from_str("-xyz").is_err()); - assert!(BigInt::from_str(":").is_err()); - assert!(BigInt::from_str("%").is_err()); - assert!(BigInt::from_str("- 758").is_err()); - a = BigInt::from(42); - assert_eq!(a, 42); - - a = BigInt::from(73786976294838206464i128); - assert_eq!(a, 73786976294838206464u128); - assert_eq!(format!("{a:X}"), "40000000000000000"); - assert!(a < "1361129467683753853853498429727072845824"); - assert!(a <= "1361129467683753853853498429727072845824"); - assert!(!(a > "1361129467683753853853498429727072845824")); - assert!(!(a >= "1361129467683753853853498429727072845824")); - assert!(!(a == "1361129467683753853853498429727072845824")); - assert!(a != "1361129467683753853853498429727072845824"); - assert!(!(a < 73786976294838206464i128)); - assert!(a <= 73786976294838206464u128); - assert!(!(a > 73786976294838206464i128)); - assert!(a >= 73786976294838206464u128); - assert!(a == 73786976294838206464u128); - assert!(!(a != 73786976294838206464i128)); - assert!(!(a < 73786976294838206464i128)); - assert!(a <= 73786976294838206464u128); - assert!(!(a > 73786976294838206464i128)); - assert!(a >= 73786976294838206464u128); - a = BigInt::from_str("2147483648").unwrap_or(BigInt::new()); - assert!(!(a < -2147483648 as i128)); - assert!(!(a <= -2147483648 as i128)); - assert!(a > -2147483648 as i128); - assert!(a >= -2147483648 as i128); - assert!(!(a == -2147483648 as i128)); - assert!(a != -2147483648 as i128); - a = BigInt::from_str("-12345678").unwrap_or(BigInt::new()); - assert!(!(a < -87654321)); - assert!(!(a <= -87654321)); - assert!(a > -87654321); - assert!(a >= -87654321); - assert!(!(a == -87654321)); - assert!(a != -87654321); - } +use std::str::FromStr; + +use num_traits::Pow; + +use crate::BigInt; +use crate::BigIntError; + +#[test] +fn default() { + let def = BigInt::default(); + assert!(def.positive); + assert_eq!(def.numbers, [0].to_vec()); +} + +#[test] +fn new() { + let new = BigInt::new(); + assert!(new.positive); + assert_eq!(new.numbers, [0].to_vec()); +} + +#[test] +fn from() { + let x = BigInt::from(20); + assert!(x.positive); + assert_eq!(x.numbers, [2, 0].to_vec()); + let x = BigInt::from(-20); + assert!(!x.positive); + assert_eq!(x.numbers, [2, 0].to_vec()); + let x = BigInt::from(-320020000981234567890_i128); + assert!(!x.positive); + assert_eq!( + x.numbers, + [ + 3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 + ] + .to_vec() + ); + let x = BigInt::from(0); + assert!(x.positive); + assert_eq!(x.numbers, [0].to_vec()); + let x = BigInt::from(-0); + assert!(x.positive); + assert_eq!(x.numbers, [0].to_vec()); +} + +#[test] +fn from_string_numbers() { + let x = BigInt::from_str("20").unwrap(); + assert!(x.positive); + assert_eq!(x.numbers, [2, 0].to_vec()); + let x = BigInt::from_str("666").unwrap(); + assert!(x.positive); + assert_eq!(x.numbers, [6, 6, 6].to_vec()); + let x = BigInt::from_str("-20").unwrap(); + assert!(!x.positive); + assert_eq!(x.numbers, [2, 0].to_vec()); + let x = BigInt::from_str("-320020000981234567890").unwrap(); + assert!(!x.positive); + assert_eq!( + x.numbers, + [ + 3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 + ] + .to_vec() + ); + let x = BigInt::from_str("-0").unwrap(); + assert!(x.positive); + assert_eq!(x.numbers, [0].to_vec()); + let x = BigInt::from_str("0sw"); + assert!(x.is_err()); + let x = BigInt::from_str("0 2020000"); + assert!(x.is_err()); + let x = BigInt::from_str("--200"); + assert!(x.is_err()); + let x = BigInt::from_str("+2000303"); + assert!(x.is_err()); + let x = BigInt::from_str("minus20003002"); + assert!(x.is_err()); +} + +#[test] +fn from_string_words_from_str_digits() { + let x = BigInt::from_str("two zero ").unwrap(); + assert!(x.positive); + assert_eq!(x.numbers, [2, 0].to_vec()); + let x = BigInt::from_str("minus two four").unwrap(); + assert!(!x.positive); + assert_eq!(x.numbers, [2, 4].to_vec()); + let x = BigInt::from_str("two five five zero zero two one").unwrap(); + assert!(x.positive); + assert_eq!(x.numbers, [2, 5, 5, 0, 0, 2, 1].to_vec()); + let x = BigInt::from_str("minus two zero zero zero zero zero one").unwrap(); + assert!(!x.positive); + assert_eq!(x.numbers, [2, 0, 0, 0, 0, 0, 1].to_vec()); + let x = BigInt::from_str("zero").unwrap(); + assert!(x.positive); + assert_eq!(x.numbers, [0].to_vec()); + let x = BigInt::from_str("onse"); + assert!(x.is_err()); + let x = BigInt::from_str(" "); + assert!(x.is_err()); + let x = BigInt::from_str("twenty thousand thousand"); + assert!(x.is_err()); + let x: Result = BigInt::from_str("twenty thousand hundred"); + assert!(x.is_err()); + let x = BigInt::from_str("- five four").unwrap(); + assert!(!x.positive); + assert_eq!(x.numbers, [5, 4].to_vec()); +} + +#[test] +fn display() { + let x = BigInt::from(1003); + assert_eq!(format!("Number is: {x:0^10}"), "Number is: 0001003000"); + let x = BigInt::from(1003); + assert_eq!(format!("Number is: {x:0>10}"), "Number is: 0000001003"); + let x = BigInt::from(1003); + assert_eq!(format!("Number is: {x:0<10}"), "Number is: 1003000000"); + let x = BigInt::from(1003); + assert_eq!(format!("Number is: {x}"), "Number is: 1003"); + let x = BigInt::from(0); + assert_eq!(format!("Number is: {x}"), "Number is: 0"); + let x = BigInt::from(-100); + assert_eq!(format!("Number is: {x}"), "Number is: -100"); + let x = BigInt::from(-0); + assert_eq!(format!("Number is: {x}"), "Number is: 0"); + let x = BigInt::from(-1); + assert_eq!(format!("Number is: {x}"), "Number is: -1"); + let x = BigInt::from(320020000981234567890_i128); + assert_eq!( + format!("Number is: {x}"), + "Number is: 320020000981234567890" + ); + let x = BigInt::from(1003); + assert_eq!(format!("Number is: {x:->10}"), "Number is: ------1003"); + let x = BigInt::from(1003); + assert_eq!(format!("Number is: {x:9}"), "Number is: 1003"); + let x = BigInt::from(0); + assert_eq!(format!("Number is: {x:2}"), "Number is: 0"); + let x = BigInt::from(-100); + assert_eq!(format!("Number is: {x}"), "Number is: -100"); + let x = BigInt::from(-0); + assert_eq!(format!("Number is: {x}"), "Number is: 0"); + let x = BigInt::from(-100); + assert_eq!(format!("Number is: {x:X>5}"), "Number is: X-100"); + let x = BigInt::from(-0); + assert_eq!(format!("Number is: {x:+}"), "Number is: +0"); +} + +#[test] +fn to_words() { + let x = BigInt::from(20); + assert_eq!(x.to_words(), "two zero"); + let x = BigInt::from(-24); + assert_eq!(x.to_words(), "minus two four"); + let x = BigInt::from(2550021); + assert_eq!(x.to_words(), "two five five zero zero two one"); + let x = BigInt::from(-2000000000001_i128); + assert_eq!( + x.to_words(), + "minus two zero zero zero zero zero zero zero zero zero zero zero one" + ); +} + +#[test] +fn not() { + let mut x = BigInt::from(1); + assert!(x.positive); + assert_eq!(x.to_string(), "1"); + x = -x; + assert!(!x.positive); + assert_eq!(x.to_string(), "-1"); + x = -x; + assert!(x.positive); + assert_eq!(x.to_string(), "1"); + let mut x = BigInt::from(-22); + assert!(!x.positive); + assert_eq!(x.to_string(), "-22"); + x = -x; + assert!(x.positive); + assert_eq!(x.to_string(), "22"); +} + +#[test] +fn equal() { + let x = BigInt::from(10); + let y = BigInt::from(10); + assert!(x == y); + let x = BigInt::from(101010); + let y = BigInt::from(101010); + assert!(x == y); + let x = BigInt::from(101010); + let y = BigInt::from(101210); + assert!(!(x == y)); + let x = BigInt::from(101010); + let y = BigInt::from(1); + assert!(!(x == y)); + let x = BigInt::from(0); + let y = BigInt::from(0); + assert!(x == y); + let x = BigInt::from(10); + let y = BigInt::from(-10); + assert!(x != y); + let x = BigInt::from(11); + let y = BigInt::from(10); + assert!(x != y); + let x = BigInt::from(-0); + let y = BigInt::from(0); + assert!(x == y); +} + +#[test] +fn equal_int() { + let x = BigInt::from(10); + assert!(x == 10); + let x = BigInt::from(101010); + assert!(x == 101010); + let x = BigInt::from(101010); + assert!(!(x == 101210)); + let x = BigInt::from(101010); + assert!(!(x == 1)); + let x = BigInt::from(0); + assert!(x == 0); + let x = BigInt::from(10); + assert!(x != -10); + let x = BigInt::from(11); + assert!(x != 10); + let x = BigInt::from(-0); + assert!(x == 0); +} + +#[test] +fn equal_str() { + let x = BigInt::from(10); + assert!(x == "10"); + let x = BigInt::from(101010); + assert!(x == "101010"); + let x = BigInt::from(101010); + assert!(!(x == "101210")); + let x = BigInt::from(101010); + assert!(!(x == "1")); + let x = BigInt::from(0); + assert!(x == "0"); + let x = BigInt::from(10); + assert!(x != "-10"); + let x = BigInt::from(11); + assert!(x != "10"); + let x = BigInt::from(-0); + assert!(x == "0"); +} + +#[test] +fn greater() { + let x: BigInt = BigInt::from(15); + let y = BigInt::from(10); + assert!(x > y); + let x: BigInt = BigInt::from(8); + let y = BigInt::from(7); + assert!(x > y); + let x = BigInt::from(10); + let y = BigInt::from(10); + assert!(x <= y); + let x = BigInt::from(10); + let y = BigInt::from(10); + assert!(x >= y); + let x = BigInt::from(101010); + let y = BigInt::from(101010); + assert!(x >= y); + let x = BigInt::from(0); + let y = BigInt::from(0); + assert!(x >= y); + let x = BigInt::from(10); + let y = BigInt::from(-10); + assert!(x > y); + let x = BigInt::from(11); + let y = BigInt::from(10); + assert!(x > y); +} + +#[test] +fn lesser() { + let x: BigInt = BigInt::from(0); + let y = BigInt::from(10); + assert!(x < y); + let x: BigInt = BigInt::from(8); + let y = BigInt::from(9); + assert!(x < y); + let x = BigInt::from(10); + let y = BigInt::from(10); + assert!(x >= y); + let x = BigInt::from(10); + let y = BigInt::from(10); + assert!(x <= y); + let x = BigInt::from(99999999999_i64); + let y = BigInt::from(99999999999_i128); + assert!(x <= y); + let x = BigInt::from(0); + let y = BigInt::from(0); + assert!(x <= y); + let x = BigInt::from(-10); + let y = BigInt::from(10); + assert!(x < y); + let x = BigInt::from(11); + let y = BigInt::from(99999999999_u64); + assert!(x < y); + let x = BigInt::from(10000010); + let y = BigInt::from(20000000); + assert!(x < y); +} + +#[test] +fn add() { + let mut x: BigInt = BigInt::from(1000); + let y = BigInt::from(10); + let z = x.clone() + y.clone(); + x += y; + assert_eq!(z, 1010); + assert_eq!(x, z); + + let mut x: BigInt = BigInt::from(10); + let y = BigInt::from(10); + let z = x.clone() + y.clone(); + x += y; + assert_eq!(z, 20); + assert_eq!(x, z); + + let mut x = BigInt::from(101010); + let y = BigInt::from(101010); + let z = x.clone() + y.clone(); + x += y; + assert_eq!(z, 202020); + assert_eq!(x, z); + + let mut x = BigInt::from(0); + let y = BigInt::from(0); + let z = x.clone() + y.clone(); + x += y; + assert_eq!(z, 0); + assert_eq!(x, z); + + let mut x = BigInt::from(10); + let y = BigInt::from(-10); + let z = x.clone() + y.clone(); + x += y.clone(); + assert_eq!(z, 0); + assert_ne!(x, y); + + let mut x = BigInt::from(11); + let y = BigInt::from(10); + let z = x.clone() + y.clone(); + x += y.clone(); + assert_eq!(z, 21); + assert_ne!(x, y); + + let mut x = BigInt::from(-0); + let y = BigInt::from(0); + let z = x.clone() + y.clone(); + x += y.clone(); + assert_eq!(z, 0); + assert_eq!(x, z); + + let mut x = BigInt::from(6); + let y = BigInt::from(4); + let z = x.clone() + y.clone(); + x += y.clone(); + assert_eq!(z, 10); + assert_eq!(x, z); + + let mut x = BigInt::from(-15); + let y = BigInt::from(-4); + let z = x.clone() + y.clone(); + x += y.clone(); + assert_eq!(z, -19); + assert_eq!(x, z); +} + +#[test] +fn sub() { + let mut x: BigInt = BigInt::from(10); + let y = BigInt::from(10); + let z = x.clone() - y.clone(); + x -= y; + assert_eq!(z, 0); + assert_eq!(x, z); + + let mut x = BigInt::from(101010); + let y = BigInt::from(10); + let z = x.clone() - y.clone(); + x -= y; + assert_eq!(z, 101000); + assert_eq!(x, z); + + let mut x = BigInt::from(0); + let y = BigInt::from(0); + let z = x.clone() - y.clone(); + x -= y; + assert_eq!(z, 0); + assert_eq!(x, z); + + let mut x = BigInt::from(10); + let y = BigInt::from(-10); + let z = x.clone() - y.clone(); + x -= y.clone(); + assert_eq!(z, 20); + assert_ne!(x, y); + + let mut x = BigInt::from(-10); + let y = BigInt::from(10); + let z = x.clone() - y.clone(); + x -= y.clone(); + assert_eq!(z, -20); + assert_ne!(x, y); + + let mut x = BigInt::from(11); + let y = BigInt::from(10); + let z = x.clone() - y.clone(); + x -= y.clone(); + assert_eq!(z, 1); + assert_ne!(x, y); + + let mut x = BigInt::from(-0); + let y = BigInt::from(0); + let z = x.clone() - y.clone(); + x -= y.clone(); + assert_eq!(z, 0); + assert_eq!(x, z); + + let mut x = BigInt::from(6); + let y = BigInt::from(4); + let z = x.clone() - y.clone(); + x -= y.clone(); + assert_eq!(z, 2); + assert_eq!(x, z); + + let mut x = BigInt::from(4); + let y = BigInt::from(15); + let z = x.clone() - y.clone(); + x -= y.clone(); + assert_eq!(z, -11); + assert_eq!(x, z); + + let mut x = BigInt::from(-15); + let y = BigInt::from(-4); + let z = x.clone() - y.clone(); + x -= y.clone(); + assert_eq!(z, -11); + assert_eq!(x, z); + + let mut x = BigInt::from(987654); + let y = BigInt::from(987653); + let z = x.clone() - y.clone(); + x -= y.clone(); + assert_eq!(z, 1); + assert_eq!(x, z); + + let mut x = BigInt::from(1); + let y = BigInt::from(1000); + let z = x.clone() - y.clone(); + x -= y.clone(); + assert_eq!(z, -999); + assert_eq!(x, z); +} + +#[test] +fn mul() { + let mut x: BigInt = BigInt::from(10); + let y = BigInt::from(10); + let z: BigInt = x.clone() * y.clone(); + x *= y; + assert_eq!(z, 100); + assert_eq!(z, x); + + let mut x = BigInt::from(101); + let y = BigInt::from(101); + let z = x.clone() * y.clone(); + x *= y; + assert_eq!(z, 10201); + assert_eq!(z, x); + + let mut x = BigInt::from(0); + let y = BigInt::from(0); + let z: BigInt = x.clone() * y.clone(); + x *= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x = BigInt::from(20100000100_u64); + let y = BigInt::from(0); + let z: BigInt = x.clone() * y.clone(); + x *= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x = BigInt::from(10); + let y = BigInt::from(-10); + let z: BigInt = x.clone() * y.clone(); + x *= y.clone(); + assert_eq!(z, -100); + assert_ne!(x, y); + + let mut x = BigInt::from(11); + let y = BigInt::from(10); + let z: BigInt = x.clone() * y.clone(); + x *= y.clone(); + assert_eq!(z, 110); + assert_ne!(x, y); + + let mut x = BigInt::from(-0); + let y = BigInt::from(0); + let z: BigInt = x.clone() * y.clone(); + x *= y.clone(); + assert_eq!(z, 0); + assert_eq!(x, y); + + let mut x = BigInt::from(6); + let y = BigInt::from(4); + let z: BigInt = x.clone() * y.clone(); + x *= y.clone(); + assert_eq!(z, 24); + assert_eq!(x, z); + + let mut x = BigInt::from(-15); + let y = BigInt::from(-4); + let z: BigInt = x.clone() * y.clone(); + x *= y.clone(); + assert_eq!(z, 60); + assert_eq!(x, z); +} + +#[test] +fn div() { + let mut x: BigInt = BigInt::from(10000000); + let y = BigInt::from(10); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!(z, 1000000); + assert_eq!(z, x); + + let mut x = BigInt::from(101); + let y = BigInt::from(101); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!(z, 1); + assert_eq!(z, x); + + let mut x = BigInt::from(9999_u128); + let y = BigInt::from(2); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!(z, 4999_u128); + assert_eq!(z, x); + + let mut x = BigInt::from_str("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999").unwrap(); + let y = BigInt::from(2); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!( + z, + "499999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" + ); + assert_eq!(z, x); + + let mut x = BigInt::from(0); + let y = BigInt::from(2); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x = BigInt::from(1); + let y = BigInt::from(2); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x = BigInt::from(1); + let y = BigInt::from(1); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!(z, 1); + assert_eq!(z, x); + + let mut x = BigInt::from(0); + let y = BigInt::from(666); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x = BigInt::from(0); + let y = BigInt::from(100); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x = BigInt::from(20100000100_u64); + let y = BigInt::from(20000000); + let z = x.clone() / y.clone(); + x /= y; + assert_eq!(z, 1005); + assert_eq!(z, x); + + let mut x = BigInt::from(10); + let y = BigInt::from(-10); + let z = x.clone() / y.clone(); + x /= y.clone(); + assert_eq!(z, -1); + assert_ne!(x, y); + + let mut x = BigInt::from(110); + let y = BigInt::from(10); + let z = x.clone() / y.clone(); + x /= y.clone(); + assert_eq!(z, 11); + assert_ne!(x, y); + + let mut x = BigInt::from(6); + let y = BigInt::from(2); + let z = x.clone() / y.clone(); + x /= y.clone(); + assert_eq!(z, 3); + assert_eq!(x, z); + + let mut x = BigInt::from(-15); + let y = BigInt::from(-5); + let z = x.clone() / y.clone(); + x /= y.clone(); + assert_eq!(z, 3); + assert_eq!(x, z); +} + +#[test] +#[should_panic] +fn div_by_zero() { + let mut x = BigInt::from(10); + let y = BigInt::from(0); + let z = x / y; +} + +#[test] +fn reminder() { + let mut x: BigInt = BigInt::from(10000); + let y = BigInt::from(10); + let z = x.clone() % y.clone(); + x %= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x: BigInt = BigInt::from(10); + let y = BigInt::from(7); + let z = x.clone() % y.clone(); + x %= y; + assert_eq!(z, 3); + assert_eq!(z, x); + + let mut x: BigInt = BigInt::from(10000); + let y = BigInt::from(10); + let z = x.clone() % y.clone(); + x %= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x: BigInt = BigInt::from(-104); + let y = BigInt::from(10); + let z = x.clone() % y.clone(); + x %= y; + assert_eq!(z, -4); + assert_eq!(z, x); + + let mut x: BigInt = BigInt::from(24); + let y = BigInt::from(3); + let z = x.clone() % y.clone(); + x %= y; + assert_eq!(z, 0); + assert_eq!(z, x); + + let mut x: BigInt = BigInt::from(33); + let y = BigInt::from(7); + let z = x.clone() % y.clone(); + x %= y; + assert_eq!(z, 5); + assert_eq!(z, x); +} + +#[test] +fn power_of() { + let mut x: BigInt = BigInt::from(2); + let y = BigInt::from(2); + let z = x.pow(y); + assert_eq!(z, 4); + + let mut x: BigInt = BigInt::from(2); + let y = BigInt::from(1); + let z = x.pow(y); + assert_eq!(z, 2); + + let mut x: BigInt = BigInt::from(1); + let y = BigInt::from(1000000); + let z = x.pow(y); + assert_eq!(z, 1); + + let mut x: BigInt = BigInt::from(3); + let y = BigInt::from(3); + let z = x.pow(y); + assert_eq!(z, 27); +} + +#[test] +#[should_panic] +fn rem_of_zero() { + let mut x: BigInt = BigInt::from(10); + let y = BigInt::from(0); + let z = x.clone() % y.clone(); + x %= y; + assert_eq!(z, 0); + assert_eq!(z, x); +} + +#[test] +fn binary() { + let x = BigInt::from(11); + assert_eq!(format!("{x:#10b}"), " 0b01011"); + let x = BigInt::from(4); + assert_eq!(format!("{x:#b}"), "0b0100"); + let x = BigInt::from(4); + assert_eq!(format!("{x:b}"), "0100"); + let x = BigInt::from(4); + assert_eq!(format!("{x:0>10b}"), "0000000100"); + let x = BigInt::from(10); + assert_eq!(format!("{x:#10b}"), " 0b01010"); + let x = BigInt::from(10); + assert_eq!(format!("{x:x>#010b}"), "xxx0b01010"); + let x = BigInt::from(172); + assert_eq!(format!("{x:b}"), "010101100"); + let x = BigInt::from(-17220003931_i64); + assert_eq!(format!("{x:#b}"), "0b110000000010011001000110100001011011"); +} + +#[test] +fn binary_transformation() { + let x: BigInt = BigInt::from(10); + let bin = x.to_binary(); + assert_eq!(x, BigInt::from_binary(bin)); + + let x: BigInt = BigInt::from(-42); + let bin = x.to_binary(); + assert_eq!(x, BigInt::from_binary(bin)); + + let x: BigInt = BigInt::from(-0); + let bin = x.to_binary(); + assert_eq!(x, BigInt::from_binary(bin)); + + let x: BigInt = BigInt::from(0); + let bin = x.to_binary(); + assert_eq!(x, BigInt::from_binary(bin)); + + let x: BigInt = BigInt::from(-9999); + let bin = x.to_binary(); + assert_eq!(x, BigInt::from_binary(bin)); + + let x: BigInt = BigInt::from(666); + let bin = x.to_binary(); + assert_eq!(x, BigInt::from_binary(bin)); +} + +#[test] +fn bit_and() { + let mut x = BigInt::from(4); //100 + let y = BigInt::from(12); //1100 + let z = x.clone() & y.clone(); //0100 + x &= y; + assert_eq!(x, z); + assert_eq!(z, 4); + + let mut x = BigInt::from(10); //1010 + let y = BigInt::from(13); //1101 + let z = x.clone() & y.clone(); //1000 + x &= y; + assert_eq!(x, z); + assert_eq!(z, 8); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(223); //11011111 + let z = x.clone() & y.clone(); //10001100 + x &= y; + assert_eq!(x, z); + assert_eq!(z, 140); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(1); //1 + let z = x.clone() & y.clone(); //0 + x &= y; + assert_eq!(x, z); + assert_eq!(z, 0); + + let mut x = BigInt::from(173); //10101101 + let y = BigInt::from(1); //1 + let z = x.clone() & y.clone(); //1 + x &= y; + assert_eq!(x, z); + assert_eq!(z, 1); +} + +#[test] +fn bit_or() { + let mut x = BigInt::from(4); //100 + let y = BigInt::from(12); //1100 + let z = x.clone() | y.clone(); //1100 + x |= y; + assert_eq!(x, z); + assert_eq!(z, 12); + + let mut x = BigInt::from(10); //1010 + let y = BigInt::from(13); //1101 + let z = x.clone() | y.clone(); //1111 + x |= y; + assert_eq!(x, z); + assert_eq!(z, 15); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(223); //11011111 + let z = x.clone() | y.clone(); //11111111 + x |= y; + assert_eq!(x, z); + assert_eq!(z, 255); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(1); //1 + let z = x.clone() | y.clone(); //10101101 + x |= y; + assert_eq!(x, z); + assert_eq!(z, 173); + + let mut x = BigInt::from(173); //10101101 + let y = BigInt::from(1); //1 + let z = x.clone() | y.clone(); //10101101 + x |= y; + assert_eq!(x, z); + assert_eq!(z, 173); +} + +#[test] +fn bit_xor() { + let mut x = BigInt::from(4); //100 + let y = BigInt::from(12); //1100 + let z = x.clone() ^ y.clone(); //1000 + x ^= y; + assert_eq!(x, z); + assert_eq!(z, 8); + + let mut x = BigInt::from(10); //1010 + let y = BigInt::from(13); //1101 + let z = x.clone() ^ y.clone(); //0111 + x ^= y; + assert_eq!(x, z); + assert_eq!(z, 7); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(223); //11011111 + let z = x.clone() ^ y.clone(); //01110011 + x ^= y; + assert_eq!(x, z); + assert_eq!(z, 115); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(1); //1 + let z = x.clone() ^ y.clone(); //10101101 + x ^= y; + assert_eq!(x, z); + assert_eq!(z, 173); + + let mut x = BigInt::from(173); //10101101 + let y = BigInt::from(1); //1 + let z = x.clone() ^ y.clone(); //10101100 + x ^= y; + assert_eq!(x, z); + assert_eq!(z, 172); +} + +#[test] +fn bit_shift_left() { + let mut x = BigInt::from(4); //100 + let y = BigInt::from(2); + let z = x.clone() << y.clone(); //10000 + x <<= y; + assert_eq!(x, z); + assert_eq!(z, 16); + + let mut x = BigInt::from(10); //1010 + let y = BigInt::from(1); + let z = x.clone() << y.clone(); //10100 + x <<= y; + assert_eq!(x, z); + assert_eq!(z, 20); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(0); + let z = x.clone() << y.clone(); + x <<= y; + assert_eq!(x, z); + assert_eq!(z, 172); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(10); + let z = x.clone() << y.clone(); //101011000000000000 + x <<= y; + assert_eq!(x, z); + assert_eq!(z, 176128); + + let mut x = BigInt::from(173); //10101101 + let y = BigInt::from(1); + let z = x.clone() << y.clone(); //101011010 + x <<= y; + assert_eq!(x, z); + assert_eq!(z, 346); +} + +#[test] +fn bit_shift_right() { + let mut x = BigInt::from(4); //100 + let y = BigInt::from(2); + let z = x.clone() >> y.clone(); //1 + x >>= y; + assert_eq!(x, z); + assert_eq!(z, 1); + + let mut x = BigInt::from(10); //1010 + let y = BigInt::from(1); + let z = x.clone() >> y.clone(); //101 + x >>= y; + assert_eq!(x, z); + assert_eq!(z, 5); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(0); + let z = x.clone() >> y.clone(); //10101100 + x >>= y; + assert_eq!(x, z); + assert_eq!(z, 172); + + let mut x = BigInt::from(172); //10101100 + let y = BigInt::from(10); + let z = x.clone() >> y.clone(); //0 + x >>= y; + assert_eq!(x, z); + assert_eq!(z, 0); + + let mut x = BigInt::from(173); //10101101 + let y = BigInt::from(1); + let z = x.clone() >> y.clone(); //1010110 + x >>= y; + assert_eq!(x, z); + assert_eq!(z, 86); +} + +#[test] +fn hexadecimal() { + let x = BigInt::from(4); + assert_eq!(format!("{x:X}"), "4"); + let x = BigInt::from(16); + assert_eq!(format!("{x:#X}"), "0x10"); + let x = BigInt::from(-4); + assert_eq!(format!("{x:X}"), "-4"); + let x = BigInt::from(-16); + assert_eq!(format!("{x:#X}"), "-0x10"); + + let x = BigInt::from(10); + assert_eq!(format!("{x:#X}"), "0xA"); + let x = BigInt::from(172); + assert_eq!(format!("{x:X}"), "AC"); + let x = BigInt::from(17220003931_u128); + assert_eq!(format!("{x:#X}"), "0x40264685B"); + + let x = BigInt::from(4); + assert_eq!(format!("{x:x}"), "4"); + let x = BigInt::from(16); + assert_eq!(format!("{x:#x}"), "0x10"); + let x = BigInt::from(10); + assert_eq!(format!("{x:#x}"), "0xa"); + let x = BigInt::from(172); + assert_eq!(format!("{x:x}"), "ac"); + let x = BigInt::from(17220003931_u128); + assert_eq!(format!("{x:#x}"), "0x40264685b"); + + let x = BigInt::from(11); + assert_eq!(format!("{x:#10X}"), " 0xB"); + let x = BigInt::from(4); + assert_eq!(format!("{x:#x}"), "0x4"); + let x = BigInt::from(4); + assert_eq!(format!("{x:X}"), "4"); + let x = BigInt::from(4); + assert_eq!(format!("{x:0>10x}"), "0000000004"); + let x = BigInt::from(10); + assert_eq!(format!("{x:#10x}"), " 0xa"); + let x = BigInt::from(10); + assert_eq!(format!("{x:x>#010X}"), "xxxxxxx0xA"); + let x = BigInt::from(172); + assert_eq!(format!("{x:X}"), "AC"); + let x = BigInt::from(-17220003931_i64); + assert_eq!(format!("{x:#x}"), "-0x40264685b"); +} + +#[test] +fn progtest_tests() { + let mut a = BigInt::new(); + let mut b = BigInt::new(); + a = BigInt::from(10); + a += BigInt::from(20); + assert_eq!(a, 30); + a *= BigInt::from(5); + assert_eq!(a, 150); + b = a.clone() + BigInt::from(3); + assert_eq!(b, 153); + b = a.clone() * BigInt::from(7); + assert_eq!(b, 1050); + assert_eq!(a, 150); + assert_eq!(format!("{a:X}"), "96"); + + a = BigInt::from(10); + a += BigInt::from(-20); + assert_eq!(a, -10); + a *= BigInt::from(5); + assert_eq!(a, -50); + b = a.clone() + BigInt::from(73); + assert_eq!(b, 23); + b = a.clone() * BigInt::from(-7); + assert_eq!(b, 350); + assert_eq!(a, -50); + assert_eq!(format!("{a:X}"), "-32"); + + a = BigInt::from(12345678901234567890_i128); + a += BigInt::from(-99999999999999999999_i128); + assert_eq!(a, -87654321098765432109_i128); + a *= BigInt::from(54321987654321987654_i128); + assert_eq!(a, "-4761556948575111126880627366067073182286"); + a *= BigInt::from(0); + assert_eq!(a, 0); + a = BigInt::from(10); + b = a.clone() + 400; + assert_eq!(b, "410"); + b = a.clone() * BigInt::from_str("15").unwrap_or_default(); + assert_eq!(b, "150"); + assert_eq!(a, "10"); + assert_eq!(format!("{a:X}"), "A"); + + b = BigInt::from_str("1234").unwrap_or_default(); + assert_eq!(format!("{b}"), "1234"); + assert!(BigInt::from_str(" 12 34").is_err()); + assert!(BigInt::from_str("999z").is_err()); + assert!(BigInt::from_str("abcd").is_err()); + assert!(BigInt::from_str("-xyz").is_err()); + assert!(BigInt::from_str(":").is_err()); + assert!(BigInt::from_str("%").is_err()); + assert!(BigInt::from_str("- 758").is_err()); + a = BigInt::from(42); + assert_eq!(a, 42); + + a = BigInt::from(73786976294838206464_i128); + assert_eq!(a, 73786976294838206464_u128); + assert_eq!(format!("{a:X}"), "40000000000000000"); + assert!(a < "1361129467683753853853498429727072845824"); + assert!(a <= "1361129467683753853853498429727072845824"); + assert!(a != "1361129467683753853853498429727072845824"); + assert!(a <= 73786976294838206464_u128); + assert!(a >= 73786976294838206464_u128); + assert!(a == 73786976294838206464_u128); + assert!(!(a != 73786976294838206464_i128)); + assert!(a <= 73786976294838206464_u128); + assert!(a >= 73786976294838206464_u128); + a = BigInt::from_str("2147483648").unwrap_or(BigInt::new()); + assert!(a > -2147483648_i128); + assert!(a >= -2147483648_i128); + assert!(!(a == -2147483648_i128)); + assert!(a != -2147483648_i128); + a = BigInt::from_str("-12345678").unwrap_or(BigInt::new()); + assert!(a > -87654321); + assert!(a >= -87654321); + assert!(!(a == -87654321)); + assert!(a != -87654321); } From 8db630fc00d6a1053b40fe3f1d23921ff610f870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 3 Aug 2025 22:13:11 +0200 Subject: [PATCH 185/243] remove useless .clone --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index aaca418..088f740 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1026,7 +1026,7 @@ impl BigInt { let numbers = numbers?; //additional check - if numbers.clone().is_empty() { + if numbers.is_empty() { return Err(BigIntError::NaN); } From daf2e88022c45ed0a3d9271ccf481dfb3c2a4b20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 3 Aug 2025 22:13:19 +0200 Subject: [PATCH 186/243] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index ad67955..9a850e5 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ target # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +.DS_Store From 839c6e4485d7654ed251cb7c0bdd3e0701100f91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 3 Aug 2025 22:15:30 +0200 Subject: [PATCH 187/243] remove useless comment --- src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 088f740..c7e8e55 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -722,7 +722,6 @@ where let mut divide_result = self.clone() / right.clone(); - //result self - divide_result * right } } From d299caed1bcdfc35ab59a253db109328d2ca3f8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 3 Aug 2025 22:16:16 +0200 Subject: [PATCH 188/243] replace == vec by .is_empty() --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index c7e8e55..d79f8f8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -688,7 +688,7 @@ where result.numbers.remove(0); } - if result.numbers == vec![] { + if result.numbers.is_empty() { return BigInt::default(); } From 3f65b57350839a3d6e365e83f85ef0b23d09a489 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 3 Aug 2025 22:17:33 +0200 Subject: [PATCH 189/243] replace = vec by .clear() --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index d79f8f8..21f5561 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -680,7 +680,7 @@ where result.numbers.push(new_digit); if left.numbers == vec![0] { - left.numbers = vec![]; + left.numbers.clear(); } } From 27a72a12bf68a4b6f3209eb29c584fa6fd86dcec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 3 Aug 2025 22:18:30 +0200 Subject: [PATCH 190/243] change function arguments --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 21f5561..298fb13 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1005,7 +1005,7 @@ impl BigInt { } } - fn parse_word_digits(string_of_numbers: String) -> Result { + fn parse_word_digits(string_of_numbers: &str) -> Result { let mut parsed: Vec = string_of_numbers .split_whitespace() .map(str::to_lowercase) From 837565dd4482e830993b8dee36e8aed8bd02d3a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 3 Aug 2025 22:19:32 +0200 Subject: [PATCH 191/243] change naming of some variables --- src/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 298fb13..56148b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -955,22 +955,22 @@ impl BigInt { } pub fn to_words(&self) -> String { - let mut fin_str: String; - let mut nmr_iter = self.numbers.iter(); + let mut final_string: String; + let mut number_iterator = self.numbers.iter(); //print minus or first digit if !self.positive { - fin_str = "minus".to_string(); + final_string = "minus".to_string(); } else { - fin_str = BigInt::number_to_word(*nmr_iter.next().unwrap_or(&0)).to_string(); + final_string = BigInt::number_to_word(*number_iterator.next().unwrap_or(&0)).to_string(); } //print all digits - for num in nmr_iter { - fin_str = format!("{} {}", fin_str, BigInt::number_to_word(*num)); + for num in number_iterator { + final_string = format!("{} {}", final_string, BigInt::number_to_word(*num)); } - fin_str + final_string } fn word_to_number(word: &str) -> Result { From 0a82c61fb500f7bd1602c3cfabb1cddffcfa43f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 3 Aug 2025 22:36:52 +0200 Subject: [PATCH 192/243] fix string by &str replacement --- src/lib.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 56148b6..eefdbb9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,11 +54,13 @@ impl FromStr for BigInt { for char in string_of_numbers.chars() { if !char.is_ascii_digit() { - return BigInt::parse_word_digits(if positive { + let x = if positive { string_of_numbers.to_string() } else { format!("-{string_of_numbers}") - }); + }; + + return BigInt::parse_word_digits(x.as_str()); } numbers.push(char.to_digit(10).unwrap().to_u8().unwrap()); @@ -962,7 +964,8 @@ impl BigInt { if !self.positive { final_string = "minus".to_string(); } else { - final_string = BigInt::number_to_word(*number_iterator.next().unwrap_or(&0)).to_string(); + final_string = + BigInt::number_to_word(*number_iterator.next().unwrap_or(&0)).to_string(); } //print all digits From 4e112b700de277ff0c03bfb99ceab0a5a0f4caa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 3 Aug 2025 22:49:07 +0200 Subject: [PATCH 193/243] remove allow of unused code --- src/lib.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index eefdbb9..bea3313 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,3 @@ -#![allow(unused)] - use num_traits::{Pow, ToPrimitive, Zero}; use std::cmp::Ordering; From 7b5b3abc28568fe8c1b01f64baaa06a266507311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 3 Aug 2025 22:49:25 +0200 Subject: [PATCH 194/243] remove unused code in lib.rs --- src/lib.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bea3313..db33dbc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -329,7 +329,7 @@ impl PartialOrd for BigInt { } //compare digits else { - let mut numbers_iterator = self.numbers.iter().zip(other.numbers.iter()); + let numbers_iterator = self.numbers.iter().zip(other.numbers.iter()); for (left, right) in numbers_iterator { if left != right { @@ -556,7 +556,7 @@ where { type Output = Self; fn mul(self, rhs: T) -> Self::Output { - let mut right: BigInt = rhs.into(); + let right: BigInt = rhs.into(); //X * 0 edgecase if self == 0 || right == 0 { @@ -580,7 +580,7 @@ where let mut result = BigInt::default(); let mut sub_total; let mut new_digit; - let mut carry = 0; + let mut carry; for (position, &left) in self.numbers.iter().rev().enumerate() { // *0 edgecase @@ -711,7 +711,7 @@ where { type Output = Self; fn rem(self, rhs: T) -> Self::Output { - let mut right: BigInt = rhs.into(); + let right: BigInt = rhs.into(); if right == 0 { panic!("division by zero!"); @@ -720,7 +720,7 @@ where return 0.into(); } - let mut divide_result = self.clone() / right.clone(); + let divide_result = self.clone() / right.clone(); self - divide_result * right } @@ -769,7 +769,7 @@ where { type Output = Self; fn bitand(self, rhs: T) -> Self::Output { - let mut right: BigInt = rhs.into(); + let right: BigInt = rhs.into(); let mut left = self.to_binary(); let mut right = right.to_binary(); @@ -813,7 +813,7 @@ where { type Output = Self; fn bitor(self, rhs: T) -> Self::Output { - let mut right: BigInt = rhs.into(); + let right: BigInt = rhs.into(); let mut left = self.to_binary(); let mut right = right.to_binary(); @@ -857,7 +857,7 @@ where { type Output = Self; fn bitxor(self, rhs: T) -> Self::Output { - let mut right: BigInt = rhs.into(); + let right: BigInt = rhs.into(); let mut left = self.to_binary(); let mut right = right.to_binary(); @@ -901,7 +901,7 @@ where { type Output = Self; fn shl(self, rhs: T) -> Self::Output { - let mut right: BigInt = rhs.into(); + let right: BigInt = rhs.into(); if right == 0 { return self; @@ -928,7 +928,7 @@ where { type Output = Self; fn shr(self, rhs: T) -> Self::Output { - let mut right: BigInt = rhs.into(); + let right: BigInt = rhs.into(); if right == 0 { return self; @@ -1012,7 +1012,7 @@ impl BigInt { .map(str::to_lowercase) .collect(); - let mut positive; + let positive; if matches!(parsed.first().map(String::as_str), Some("-" | "minus")) { positive = false; From c59075d82dc620137ca704a1ccb4e6b4a9a0ed57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Sun, 3 Aug 2025 22:49:49 +0200 Subject: [PATCH 195/243] remove useless mut in tests.rs --- src/tests.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index e490986..286fd40 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -174,7 +174,7 @@ fn not() { x = -x; assert!(x.positive); assert_eq!(x.to_string(), "1"); - let mut x = BigInt::from(-22); + x = BigInt::from(-22); assert!(!x.positive); assert_eq!(x.to_string(), "-22"); x = -x; @@ -637,7 +637,7 @@ fn div() { fn div_by_zero() { let mut x = BigInt::from(10); let y = BigInt::from(0); - let z = x / y; + x /= y; } #[test] @@ -687,22 +687,22 @@ fn reminder() { #[test] fn power_of() { - let mut x: BigInt = BigInt::from(2); + let x: BigInt = BigInt::from(2); let y = BigInt::from(2); let z = x.pow(y); assert_eq!(z, 4); - let mut x: BigInt = BigInt::from(2); + let x: BigInt = BigInt::from(2); let y = BigInt::from(1); let z = x.pow(y); assert_eq!(z, 2); - let mut x: BigInt = BigInt::from(1); + let x: BigInt = BigInt::from(1); let y = BigInt::from(1000000); let z = x.pow(y); assert_eq!(z, 1); - let mut x: BigInt = BigInt::from(3); + let x: BigInt = BigInt::from(3); let y = BigInt::from(3); let z = x.pow(y); assert_eq!(z, 27); @@ -1005,14 +1005,12 @@ fn hexadecimal() { #[test] fn progtest_tests() { - let mut a = BigInt::new(); - let mut b = BigInt::new(); - a = BigInt::from(10); + let mut a = BigInt::from(10); a += BigInt::from(20); assert_eq!(a, 30); a *= BigInt::from(5); assert_eq!(a, 150); - b = a.clone() + BigInt::from(3); + let mut b = a.clone() + BigInt::from(3); assert_eq!(b, 153); b = a.clone() * BigInt::from(7); assert_eq!(b, 1050); From f14c03952f6e27d85752ba4eaed97cd80d93519b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 4 Aug 2025 18:16:21 +0200 Subject: [PATCH 196/243] fix from_str --- src/lib.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index db33dbc..4915d43 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,7 +36,9 @@ impl Default for BigInt { impl FromStr for BigInt { type Err = BigIntError; - fn from_str(mut string_of_numbers: &str) -> Result { + fn from_str(string_of_numbers: &str) -> Result { + let original = string_of_numbers; + //empty string edgecaase if string_of_numbers.is_empty() { return Err(BigIntError::NaN); @@ -46,19 +48,15 @@ impl FromStr for BigInt { let mut numbers: Vec = Vec::new(); //if negative - remove '-' - if !positive { - string_of_numbers = string_of_numbers.split_at(1).1; - } + let string_of_numbers = &string_of_numbers[!positive as usize..]; for char in string_of_numbers.chars() { if !char.is_ascii_digit() { - let x = if positive { - string_of_numbers.to_string() + return BigInt::parse_word_digits(if positive { + string_of_numbers } else { - format!("-{string_of_numbers}") - }; - - return BigInt::parse_word_digits(x.as_str()); + original + }); } numbers.push(char.to_digit(10).unwrap().to_u8().unwrap()); From 2537437e614f45f9f105074ea8f6ad680bd44ede Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 4 Aug 2025 18:22:06 +0200 Subject: [PATCH 197/243] remove useless .to_string() --- src/lib.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4915d43..7f1ed59 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -228,11 +228,7 @@ impl Binary for BigInt { impl UpperHex for BigInt { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let output = self - .create_hexa_string(f) - .to_uppercase() - .to_string() - .replace("X", "x"); + let output = self.create_hexa_string(f).to_uppercase().replace("X", "x"); write!(f, "{output}")?; From 3efc251f40e9b3ba4b70e4e030f1ef74fd11e541 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 4 Aug 2025 19:31:15 +0200 Subject: [PATCH 198/243] replace .is_zero by ==0 --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 7f1ed59..e214371 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -427,7 +427,7 @@ where result.numbers.push(new_digit); } - if !carry.is_zero() { + if carry != 0 { result.numbers.push(carry); } From f7b2ca879e0b8741e4678fa93f4b42f55ecba5cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 4 Aug 2025 19:39:13 +0200 Subject: [PATCH 199/243] simplify hex text functions --- src/lib.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e214371..5bf0be0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -228,7 +228,7 @@ impl Binary for BigInt { impl UpperHex for BigInt { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let output = self.create_hexa_string(f).to_uppercase().replace("X", "x"); + let output = self.create_hexa_string(f, true); write!(f, "{output}")?; @@ -238,7 +238,7 @@ impl UpperHex for BigInt { impl LowerHex for BigInt { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let output = self.create_hexa_string(f).to_ascii_lowercase().to_string(); + let output = self.create_hexa_string(f, false); write!(f, "{output}")?; @@ -1137,7 +1137,7 @@ impl BigInt { final_vec } - fn create_hexa_string(&self, f: &mut fmt::Formatter<'_>) -> String { + fn create_hexa_string(&self, f: &mut fmt::Formatter<'_>, uppercase: bool) -> String { let hexa = self.to_hexa(); let mut output = String::new(); @@ -1150,7 +1150,11 @@ impl BigInt { } for hex in hexa { - output.push_str(&hex.to_uppercase().to_string()); + output.push(if uppercase { + hex.to_ascii_uppercase() + } else { + hex.to_ascii_lowercase() + }) } let mut right_fill = true; From 9205cef7cfeee631592adf3781724f6ecb776378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 4 Aug 2025 22:04:48 +0200 Subject: [PATCH 200/243] add more tests for .pow() --- src/tests.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index 286fd40..587ed5a 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -706,6 +706,31 @@ fn power_of() { let y = BigInt::from(3); let z = x.pow(y); assert_eq!(z, 27); + + let x: BigInt = BigInt::from(-3); + let y = BigInt::from(3); + let z = x.pow(y); + assert_eq!(z, -27); + + let x: BigInt = BigInt::from(-3); + let y = BigInt::from(2); + let z = x.pow(y); + assert_eq!(z, 9); + + let x: BigInt = BigInt::from(2); + let y = BigInt::from(-1); + let z = x.pow(y); + assert_eq!(z, 0); + + let x: BigInt = BigInt::from(1); + let y = BigInt::from(-10); + let z = x.pow(y); + assert_eq!(z, 1); + + let x: BigInt = BigInt::from(5); + let y = BigInt::from(-10); + let z = x.pow(y); + assert_eq!(z, 0); } #[test] From e3e5eeea5be6425acee1ff10cdfd679276375c46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 4 Aug 2025 22:05:07 +0200 Subject: [PATCH 201/243] add negative .pow edgecase --- src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 5bf0be0..20b9da4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -744,6 +744,12 @@ where if right == 0 { return 1.into(); } + if !right.positive { + if self == 1 { + return 1.into(); + } + return 0.into(); + } let mut result: BigInt = 1.into(); From b65626ba385feec85d80d105b2b686117776cc5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 4 Aug 2025 22:35:08 +0200 Subject: [PATCH 202/243] fix 0-X edgecase --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 20b9da4..4ba8c27 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -455,9 +455,9 @@ where fn sub(mut self, rhs: T) -> Self::Output { let mut right: BigInt = rhs.into(); - // X - 0 edgecase + // 0 - X edgecase if self == 0 { - return right; + return -right; } if right == 0 { return self; From f4e773fc70fad3a7cba14354def65b9d8621781e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Mon, 4 Aug 2025 22:36:03 +0200 Subject: [PATCH 203/243] fix x/1 edgecase --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 4ba8c27..27d1925 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -638,7 +638,7 @@ where return BigInt::default(); } if right == 1 { - return 1.into(); + return self; } if self == 1 { if right == 1 { From 206db8fe70ab44a1354c9ddf0acc3dc4cc84884d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 10:59:43 +0200 Subject: [PATCH 204/243] fix negation of 0 edgecase --- src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 27d1925..e049981 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -282,7 +282,9 @@ impl PartialEq<&str> for BigInt { impl Neg for BigInt { type Output = Self; fn neg(mut self) -> Self::Output { - self.positive = !self.positive; + if self != 0 { + self.positive = !self.positive; + } self } } From 7ac73344c8bde20a774ed84435f7d216c3903ff2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 13:12:04 +0200 Subject: [PATCH 205/243] refactor word_to_number parsing --- src/lib.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e049981..0fc76c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1023,8 +1023,11 @@ impl BigInt { positive = true; } - let numbers: Result, _> = - parsed.iter().map(|w| BigInt::word_to_number(w)).collect(); + let numbers: Result, _> = parsed + .iter() + .map(String::as_str) + .map(BigInt::word_to_number) + .collect(); let numbers = numbers?; //additional check From 9b8f1bd31577c2ac6bc9475b73274e6c0c157c32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 17:43:12 +0200 Subject: [PATCH 206/243] refactor .to_words() function --- src/lib.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0fc76c0..5048155 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -957,21 +957,21 @@ impl BigInt { } pub fn to_words(&self) -> String { - let mut final_string: String; - let mut number_iterator = self.numbers.iter(); + let mut final_string = String::new(); - //print minus or first digit if !self.positive { - final_string = "minus".to_string(); - } else { - final_string = - BigInt::number_to_word(*number_iterator.next().unwrap_or(&0)).to_string(); + final_string.push_str("minus "); } - //print all digits - for num in number_iterator { - final_string = format!("{} {}", final_string, BigInt::number_to_word(*num)); - } + let numbers_by_words = self.numbers.iter().fold(String::new(), |mut string, num| { + string.push_str(&BigInt::number_to_word(*num)); + string.push(' '); + string + }); + + final_string.push_str(&numbers_by_words); + + final_string.pop(); final_string } From 54c995dbb1721911e83f49a87f3084d0ef256970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 18:35:54 +0200 Subject: [PATCH 207/243] fix binary tests --- src/tests.rs | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 587ed5a..09e0735 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -747,48 +747,48 @@ fn rem_of_zero() { #[test] fn binary() { let x = BigInt::from(11); - assert_eq!(format!("{x:#10b}"), " 0b01011"); + assert_eq!(format!("{x:#10b}"), " 0b1011"); let x = BigInt::from(4); - assert_eq!(format!("{x:#b}"), "0b0100"); + assert_eq!(format!("{x:#b}"), "0b100"); let x = BigInt::from(4); - assert_eq!(format!("{x:b}"), "0100"); + assert_eq!(format!("{x:b}"), "100"); let x = BigInt::from(4); assert_eq!(format!("{x:0>10b}"), "0000000100"); let x = BigInt::from(10); - assert_eq!(format!("{x:#10b}"), " 0b01010"); + assert_eq!(format!("{x:#10b}"), " 0b1010"); let x = BigInt::from(10); - assert_eq!(format!("{x:x>#010b}"), "xxx0b01010"); + assert_eq!(format!("{x:x>#010b}"), "xxxx0b1010"); let x = BigInt::from(172); - assert_eq!(format!("{x:b}"), "010101100"); + assert_eq!(format!("{x:b}"), "10101100"); let x = BigInt::from(-17220003931_i64); - assert_eq!(format!("{x:#b}"), "0b110000000010011001000110100001011011"); + assert_eq!(format!("{x:#b}"), "-0b10000000010011001000110100001011011"); } #[test] fn binary_transformation() { let x: BigInt = BigInt::from(10); - let bin = x.to_binary(); - assert_eq!(x, BigInt::from_binary(bin)); + let (positive, binary) = x.to_binary(); + assert_eq!(x, BigInt::from_binary(positive, binary)); let x: BigInt = BigInt::from(-42); - let bin = x.to_binary(); - assert_eq!(x, BigInt::from_binary(bin)); + let (positive, binary) = x.to_binary(); + assert_eq!(x, BigInt::from_binary(positive, binary)); let x: BigInt = BigInt::from(-0); - let bin = x.to_binary(); - assert_eq!(x, BigInt::from_binary(bin)); + let (positive, binary) = x.to_binary(); + assert_eq!(x, BigInt::from_binary(positive, binary)); let x: BigInt = BigInt::from(0); - let bin = x.to_binary(); - assert_eq!(x, BigInt::from_binary(bin)); + let (positive, binary) = x.to_binary(); + assert_eq!(x, BigInt::from_binary(positive, binary)); let x: BigInt = BigInt::from(-9999); - let bin = x.to_binary(); - assert_eq!(x, BigInt::from_binary(bin)); + let (positive, binary) = x.to_binary(); + assert_eq!(x, BigInt::from_binary(positive, binary)); let x: BigInt = BigInt::from(666); - let bin = x.to_binary(); - assert_eq!(x, BigInt::from_binary(bin)); + let (positive, binary) = x.to_binary(); + assert_eq!(x, BigInt::from_binary(positive, binary)); } #[test] @@ -874,30 +874,30 @@ fn bit_xor() { let z = x.clone() ^ y.clone(); //1000 x ^= y; assert_eq!(x, z); - assert_eq!(z, 8); + assert_eq!(z, -8); let mut x = BigInt::from(10); //1010 let y = BigInt::from(13); //1101 let z = x.clone() ^ y.clone(); //0111 x ^= y; assert_eq!(x, z); - assert_eq!(z, 7); + assert_eq!(z, -7); - let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(223); //11011111 + let mut x = BigInt::from(-172); //10101100 + let y = BigInt::from(-223); //11011111 let z = x.clone() ^ y.clone(); //01110011 x ^= y; assert_eq!(x, z); - assert_eq!(z, 115); + assert_eq!(z, -115); let mut x = BigInt::from(172); //10101100 - let y = BigInt::from(1); //1 + let y = BigInt::from(-1); //1 let z = x.clone() ^ y.clone(); //10101101 x ^= y; assert_eq!(x, z); assert_eq!(z, 173); - let mut x = BigInt::from(173); //10101101 + let mut x = BigInt::from(-173); //10101101 let y = BigInt::from(1); //1 let z = x.clone() ^ y.clone(); //10101100 x ^= y; From 186a12d21eed986736b7a3a28c64ff9c46f37031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 18:36:30 +0200 Subject: [PATCH 208/243] Change the binary representation --- src/lib.rs | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5048155..a0a3fb3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -184,9 +184,16 @@ impl Display for BigInt { impl Binary for BigInt { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let binary = self.to_binary(); + let (positive, binary) = self.to_binary(); let mut output = String::new(); + if !self.positive { + output.push('-'); + } + if f.sign_plus() && self.positive { + output.push('+'); + } + if f.alternate() { output.push_str("0b"); } @@ -773,8 +780,8 @@ where fn bitand(self, rhs: T) -> Self::Output { let right: BigInt = rhs.into(); - let mut left = self.to_binary(); - let mut right = right.to_binary(); + let (left_positive, mut left) = self.to_binary(); + let (right_positive, mut right) = right.to_binary(); let mut result = vec![]; left.reverse(); @@ -796,7 +803,7 @@ where result.reverse(); - BigInt::from_binary(result) + BigInt::from_binary(left_positive & right_positive, result) } } @@ -817,8 +824,8 @@ where fn bitor(self, rhs: T) -> Self::Output { let right: BigInt = rhs.into(); - let mut left = self.to_binary(); - let mut right = right.to_binary(); + let (left_positive, mut left) = self.to_binary(); + let (right_positive, mut right) = right.to_binary(); let mut result = vec![]; left.reverse(); @@ -840,7 +847,7 @@ where result.reverse(); - BigInt::from_binary(result) + BigInt::from_binary(left_positive | right_positive, result) } } @@ -861,8 +868,8 @@ where fn bitxor(self, rhs: T) -> Self::Output { let right: BigInt = rhs.into(); - let mut left = self.to_binary(); - let mut right = right.to_binary(); + let (left_positive, mut left) = self.to_binary(); + let (right_positive, mut right) = right.to_binary(); let mut result = vec![]; left.reverse(); @@ -884,7 +891,7 @@ where result.reverse(); - BigInt::from_binary(result) + BigInt::from_binary(left_positive ^ right_positive, result) } } @@ -1042,9 +1049,9 @@ impl BigInt { (*self.numbers.last().unwrap() % 2).is_zero() } - fn to_binary(&self) -> Vec { + fn to_binary(&self) -> (bool, Vec) { if *self == 0 { - return vec![false]; + return (true, vec![false]); } let mut final_vec = vec![]; @@ -1063,27 +1070,19 @@ impl BigInt { number /= 2; } - if positive { - final_vec.push(false); - } else { - final_vec.push(true); - } - final_vec.reverse(); - final_vec + (positive, final_vec) } - fn from_binary(binary: Vec) -> BigInt { + fn from_binary(positive: bool, binary: Vec) -> BigInt { if binary.is_empty() { return BigInt::default(); } - let positive = !binary.first().unwrap(); - let mut result = BigInt::default(); - for (position, bit) in binary.iter().skip(1).rev().enumerate() { + for (position, bit) in binary.iter().rev().enumerate() { if *bit { result += 2.pow(position); } @@ -1155,6 +1154,9 @@ impl BigInt { if !self.positive { output.push('-'); } + if f.sign_plus() && self.positive { + output.push('+'); + } if f.alternate() { output.push_str("0x"); From b359e36399c479876481c2ca97b8518b65edc5f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 18:41:54 +0200 Subject: [PATCH 209/243] refactor binary operations --- src/lib.rs | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a0a3fb3..69dded4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -782,7 +782,6 @@ where let (left_positive, mut left) = self.to_binary(); let (right_positive, mut right) = right.to_binary(); - let mut result = vec![]; left.reverse(); right.reverse(); @@ -797,11 +796,7 @@ where let binary_set = longer.iter().zip(shorter.iter()); - for (left, right) in binary_set { - result.push(left & right); - } - - result.reverse(); + let result: Vec = binary_set.rev().map(|(left, right)| left & right).collect(); BigInt::from_binary(left_positive & right_positive, result) } @@ -826,7 +821,6 @@ where let (left_positive, mut left) = self.to_binary(); let (right_positive, mut right) = right.to_binary(); - let mut result = vec![]; left.reverse(); right.reverse(); @@ -841,11 +835,7 @@ where let binary_set = longer.iter().zip(shorter.iter()); - for (left, right) in binary_set { - result.push(left | right); - } - - result.reverse(); + let result: Vec = binary_set.rev().map(|(left, right)| left | right).collect(); BigInt::from_binary(left_positive | right_positive, result) } @@ -870,7 +860,6 @@ where let (left_positive, mut left) = self.to_binary(); let (right_positive, mut right) = right.to_binary(); - let mut result = vec![]; left.reverse(); right.reverse(); @@ -885,11 +874,7 @@ where let binary_set = longer.iter().zip(shorter.iter()); - for (left, right) in binary_set { - result.push(left ^ right); - } - - result.reverse(); + let result: Vec = binary_set.rev().map(|(left, right)| left ^ right).collect(); BigInt::from_binary(left_positive ^ right_positive, result) } From 40ed7a45d4b47965520bd452b67c97562584e68a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 19:35:03 +0200 Subject: [PATCH 210/243] change position and arguments of function --- src/lib.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 69dded4..f836398 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -367,23 +367,6 @@ impl PartialOrd<&str> for BigInt { } } -fn create_numbers_set(left: BigInt, right: BigInt) -> (Vec, Vec) { - let (mut longer, mut shorter) = { - if left.numbers.len() >= right.numbers.len() { - (left.numbers, right.numbers) - } else { - (right.numbers, left.numbers) - } - }; - - longer.reverse(); - shorter.reverse(); - - shorter.extend(vec![0; longer.len() - shorter.len()]); - - (shorter, longer) -} - impl Add for BigInt where T: Into, @@ -416,7 +399,7 @@ where numbers: vec![], }; - let (longer, shorter) = create_numbers_set(self, right); + let (longer, shorter) = BigInt::equalize_vectors_length(self.numbers, right.numbers); let numbers_set = longer.iter().zip(shorter.iter()); @@ -1178,6 +1161,23 @@ impl BigInt { output } + + fn equalize_vectors_length(left: Vec, right: Vec) -> (Vec, Vec) { + let (mut longer, mut shorter) = { + if left.len() > right.len() { + (left, right) + } else { + (right, left) + } + }; + + longer.reverse(); + shorter.reverse(); + + shorter.extend(vec![0; longer.len() - shorter.len()]); + + (shorter, longer) + } } #[cfg(test)] From dec9f9a3ca18f562493cd3b6c31251ff8848540a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 19:48:46 +0200 Subject: [PATCH 211/243] Create function to do all binary operation logic --- src/lib.rs | 97 +++++++++++++++++++----------------------------------- 1 file changed, 34 insertions(+), 63 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f836398..68a6e79 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -761,27 +761,7 @@ where { type Output = Self; fn bitand(self, rhs: T) -> Self::Output { - let right: BigInt = rhs.into(); - - let (left_positive, mut left) = self.to_binary(); - let (right_positive, mut right) = right.to_binary(); - - left.reverse(); - right.reverse(); - - let (longer, mut shorter) = if left.len() > right.len() { - (left, right) - } else { - (right, left) - }; - - shorter.extend(vec![false; longer.len() - shorter.len()]); - - let binary_set = longer.iter().zip(shorter.iter()); - - let result: Vec = binary_set.rev().map(|(left, right)| left & right).collect(); - - BigInt::from_binary(left_positive & right_positive, result) + BigInt::binary_operation(self, rhs, |left, right| left & right) } } @@ -800,27 +780,7 @@ where { type Output = Self; fn bitor(self, rhs: T) -> Self::Output { - let right: BigInt = rhs.into(); - - let (left_positive, mut left) = self.to_binary(); - let (right_positive, mut right) = right.to_binary(); - - left.reverse(); - right.reverse(); - - let (longer, mut shorter) = if left.len() > right.len() { - (left, right) - } else { - (right, left) - }; - - shorter.extend(vec![false; longer.len() - shorter.len()]); - - let binary_set = longer.iter().zip(shorter.iter()); - - let result: Vec = binary_set.rev().map(|(left, right)| left | right).collect(); - - BigInt::from_binary(left_positive | right_positive, result) + BigInt::binary_operation(self, rhs, |left, right| left | right) } } @@ -839,27 +799,7 @@ where { type Output = Self; fn bitxor(self, rhs: T) -> Self::Output { - let right: BigInt = rhs.into(); - - let (left_positive, mut left) = self.to_binary(); - let (right_positive, mut right) = right.to_binary(); - - left.reverse(); - right.reverse(); - - let (longer, mut shorter) = if left.len() > right.len() { - (left, right) - } else { - (right, left) - }; - - shorter.extend(vec![false; longer.len() - shorter.len()]); - - let binary_set = longer.iter().zip(shorter.iter()); - - let result: Vec = binary_set.rev().map(|(left, right)| left ^ right).collect(); - - BigInt::from_binary(left_positive ^ right_positive, result) + BigInt::binary_operation(self, rhs, |left, right| left ^ right) } } @@ -1178,6 +1118,37 @@ impl BigInt { (shorter, longer) } + + fn binary_operation(self, rhs: T, bit_operation: F) -> BigInt + where + F: Fn(bool, bool) -> bool, + T: Into, + { + let right: BigInt = rhs.into(); + + let (left_positive, mut left) = self.to_binary(); + let (right_positive, mut right) = right.to_binary(); + + left.reverse(); + right.reverse(); + + let (longer, mut shorter) = if left.len() > right.len() { + (left, right) + } else { + (right, left) + }; + + shorter.extend(vec![false; longer.len() - shorter.len()]); + + let binary_set = longer.iter().zip(shorter.iter()); + + let result: Vec = binary_set + .rev() + .map(|(left, right)| bit_operation(*left, *right)) + .collect(); + + BigInt::from_binary(bit_operation(left_positive, right_positive), result) + } } #[cfg(test)] From 154d724cba541c1ab97faa1bab246dd0c889cb3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 20:01:12 +0200 Subject: [PATCH 212/243] remove useless mut --- src/lib.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 68a6e79..b57a6a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -372,8 +372,8 @@ where T: Into, { type Output = Self; - fn add(mut self, rhs: T) -> Self::Output { - let mut right: BigInt = rhs.into(); + fn add(self, rhs: T) -> Self::Output { + let right: BigInt = rhs.into(); // X + 0 edgecase if self == 0 { @@ -386,12 +386,12 @@ where // negative numbers edgecases if !self.positive && right.positive { // -X + Y => Y - X => -(X - Y) - self.positive = true; - return right - self; + //self.positive = true; + return right - (-self); } else if self.positive && !right.positive { //X - Y - right.positive = true; - return self - right; + //right.positive = true; + return self - (-right); } let mut result = BigInt { From a2c48b654a5d0e9eda890dba0b07a5ac4b76fef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 20:24:25 +0200 Subject: [PATCH 213/243] move div and rem logic to one function --- src/lib.rs | 98 +++++++++++++++++++++++++++++------------------------- 1 file changed, 53 insertions(+), 45 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b57a6a6..302f08a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -620,8 +620,8 @@ where T: Into, { type Output = Self; - fn div(mut self, rhs: T) -> Self::Output { - let mut right: BigInt = rhs.into(); + fn div(self, rhs: T) -> Self::Output { + let right: BigInt = rhs.into(); if right == 0 { panic!("division by zero!"); @@ -639,46 +639,7 @@ where return BigInt::default(); } - let mut result = BigInt { - positive: self.positive == right.positive, - numbers: vec![], - }; - - self.positive = true; - right.positive = true; - - let mut left = BigInt { - positive: true, - numbers: vec![], - }; - - let mut new_digit; - - for digit in self.numbers.iter() { - left.numbers.push(*digit); - new_digit = 0; - - while left >= right { - left -= right.clone(); - new_digit += 1; - } - - result.numbers.push(new_digit); - - if left.numbers == vec![0] { - left.numbers.clear(); - } - } - - while result.numbers.first().unwrap_or(&1).is_zero() { - result.numbers.remove(0); - } - - if result.numbers.is_empty() { - return BigInt::default(); - } - - result + BigInt::divide_with_remainder(self, right).0 } } @@ -706,9 +667,7 @@ where return 0.into(); } - let divide_result = self.clone() / right.clone(); - - self - divide_result * right + BigInt::divide_with_remainder(self, right).1 } } @@ -1149,6 +1108,55 @@ impl BigInt { BigInt::from_binary(bit_operation(left_positive, right_positive), result) } + + fn divide_with_remainder(mut left: BigInt, mut right: BigInt) -> (BigInt, BigInt) { + let mut result = BigInt { + positive: left.positive == right.positive, + numbers: vec![], + }; + + let mut partial_sum = BigInt { + positive: true, + numbers: vec![], + }; + + left.positive = true; + right.positive = true; + + let mut new_digit; + + for digit in left.numbers.iter() { + partial_sum.numbers.push(*digit); + new_digit = 0; + + while partial_sum >= right { + partial_sum -= right.clone(); + new_digit += 1; + } + + result.numbers.push(new_digit); + + if partial_sum.numbers == vec![0] { + partial_sum.numbers.clear(); + } + } + + while result.numbers.first().unwrap_or(&1).is_zero() { + result.numbers.remove(0); + } + + if result.numbers.is_empty() { + return (BigInt::default(), partial_sum); + } + + if partial_sum.numbers.is_empty() { + partial_sum = BigInt::default(); + } + + partial_sum.positive = result.positive; + + (result, partial_sum) + } } #[cfg(test)] From a957529db8808aa356384e4ee652690f6f4d535c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 20:52:47 +0200 Subject: [PATCH 214/243] refactor .to_words function --- src/lib.rs | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 302f08a..5b04d71 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -831,23 +831,17 @@ impl BigInt { } pub fn to_words(&self) -> String { - let mut final_string = String::new(); - - if !self.positive { - final_string.push_str("minus "); - } - - let numbers_by_words = self.numbers.iter().fold(String::new(), |mut string, num| { - string.push_str(&BigInt::number_to_word(*num)); - string.push(' '); - string - }); - - final_string.push_str(&numbers_by_words); - - final_string.pop(); - - final_string + let prefix = if self.positive { vec![] } else { vec!["minus"] }; + + prefix + .into_iter() + .chain( + self.numbers + .iter() + .map(|&number| BigInt::number_to_word(number)), + ) + .collect::>() + .join(" ") } fn word_to_number(word: &str) -> Result { From 514e8f5c2529192a6627057ddf68e80649856de4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 21:20:03 +0200 Subject: [PATCH 215/243] add tests for try_into --- src/tests.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index 09e0735..180e394 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -109,6 +109,36 @@ fn from_string_words_from_str_digits() { assert_eq!(x.numbers, [5, 4].to_vec()); } +#[test] +fn try_into() { + let x = BigInt::from(666); + assert_eq!(666_i128, x.try_into().unwrap()); + let x = BigInt::from(666); + assert_eq!(666_u128, x.try_into().unwrap()); + let x = BigInt::from(-666); + assert_eq!(-666_i128, x.try_into().unwrap()); + let x = BigInt::from(-666); + assert_eq!(666_u128, x.try_into().unwrap()); + let x = BigInt::from(0); + assert_eq!(0_i128, x.try_into().unwrap()); + let x = BigInt::from(0); + assert_eq!(0_u128, x.try_into().unwrap()); +} + +#[test] +#[should_panic] +fn try_into_panic_i128() { + let x = BigInt::from_str("999999999999999999999999999999999999999999999999999999999").unwrap(); + assert_eq!(666_i128, x.try_into().unwrap()); +} + +#[test] +#[should_panic] +fn try_into_panic_u128() { + let x = BigInt::from_str("999999999999999999999999999999999999999999999999999999999").unwrap(); + assert_eq!(666_u128, x.try_into().unwrap()); +} + #[test] fn display() { let x = BigInt::from(1003); From da803a9a9f431dc06fc23a424c85cbfa70fe48cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 21:26:11 +0200 Subject: [PATCH 216/243] add error for too large number --- src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 5b04d71..45c2bab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,13 +9,17 @@ use std::str::FromStr; #[derive(Debug)] enum BigIntError { NaN, + LargeNumber, } impl Error for BigIntError {} impl Display for BigIntError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Not a Number") + match self { + BigIntError::NaN => write!(f, "Not a Number"), + BigIntError::LargeNumber => write!(f, "Too large"), + } } } From 9cced9a4847112ac6eaf068f2c1f16c8d2dd8112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 21:26:34 +0200 Subject: [PATCH 217/243] minor change in Display trait --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 45c2bab..60c81b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -191,10 +191,10 @@ impl Binary for BigInt { let (positive, binary) = self.to_binary(); let mut output = String::new(); - if !self.positive { + if !positive { output.push('-'); } - if f.sign_plus() && self.positive { + if f.sign_plus() && positive { output.push('+'); } From 337cc00a4727cc357bf3c4b76dd2a80f8b93a8ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 21:26:51 +0200 Subject: [PATCH 218/243] Refactor number_to_hexa function --- src/lib.rs | 56 ++++++++++++++++++++---------------------------------- 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 60c81b2..eda07ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -959,41 +959,27 @@ impl BigInt { } fn number_to_hexa(number: BigInt) -> char { - if number == 0 { - '0' - } else if number == 1 { - '1' - } else if number == 2 { - '2' - } else if number == 3 { - '3' - } else if number == 4 { - '4' - } else if number == 5 { - '5' - } else if number == 6 { - '6' - } else if number == 7 { - '7' - } else if number == 8 { - '8' - } else if number == 9 { - '9' - } else if number == 10 { - 'A' - } else if number == 11 { - 'B' - } else if number == 12 { - 'C' - } else if number == 13 { - 'D' - } else if number == 14 { - 'E' - } else if number == 15 { - 'F' - } else { - '0' - } + let number: u8 = number.try_into().unwrap_or(0_i128) as u8; + + return match number { + 0 => '0', + 1 => '1', + 2 => '2', + 3 => '3', + 4 => '4', + 5 => '5', + 6 => '6', + 7 => '7', + 8 => '8', + 9 => '9', + 10 => 'A', + 11 => 'B', + 12 => 'C', + 13 => 'D', + 14 => 'E', + 15 => 'F', + _ => ' ', + }; } fn to_hexa(&self) -> Vec { From e8e7e4d7f8fa64c481daa64e514943c37fa07d28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 22:34:27 +0200 Subject: [PATCH 219/243] add try_into implementations --- src/lib.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index eda07ab..7ad3105 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -144,6 +144,62 @@ macro_rules! from_uint { from_int!(i8, i16, i32, i64, i128); from_uint!(u8, u16, u32, u64, u128); +fn create_new_digit(position: usize, number: &u8) -> u64 { + let number = *number as u64; + + if number == 0 { + return match position { + 0 => 0, + _ => 10.pow(position), + } as u64; + } + number * 10_u64.pow(position as u32) +} + +impl TryInto for BigInt { + type Error = BigIntError; + fn try_into(self) -> Result { + if self > i128::MAX { + return Err(BigIntError::LargeNumber); + } + if self == 0 { + return Ok(0_i128); + } + + let mut result: i128 = 0; + + for (position, number) in self.numbers.iter().rev().enumerate() { + result += create_new_digit(position, number) as i128; + } + + if !self.positive { + result *= -1; + } + + Ok(result) + } +} + +impl TryInto for BigInt { + type Error = BigIntError; + fn try_into(self) -> Result { + if self > u128::MAX { + return Err(BigIntError::LargeNumber); + } + if self == 0 { + return Ok(0_u128); + } + + let mut result: u128 = 0; + + for (position, number) in self.numbers.iter().rev().enumerate() { + result += create_new_digit(position, number) as u128; + } + + Ok(result) + } +} + impl Display for BigInt { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let mut output = String::new(); From 671b8ebf1ab66fa09ba37b13eaefcf8eadc80004 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 22:34:34 +0200 Subject: [PATCH 220/243] Change some tests --- src/tests.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 180e394..5ddabcb 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -115,14 +115,18 @@ fn try_into() { assert_eq!(666_i128, x.try_into().unwrap()); let x = BigInt::from(666); assert_eq!(666_u128, x.try_into().unwrap()); - let x = BigInt::from(-666); - assert_eq!(-666_i128, x.try_into().unwrap()); - let x = BigInt::from(-666); - assert_eq!(666_u128, x.try_into().unwrap()); + let x = BigInt::from(-123); + assert_eq!(-123_i128, x.try_into().unwrap()); + let x = BigInt::from(-123); + assert_eq!(123_u128, x.try_into().unwrap()); let x = BigInt::from(0); assert_eq!(0_i128, x.try_into().unwrap()); let x = BigInt::from(0); assert_eq!(0_u128, x.try_into().unwrap()); + let x = BigInt::from(10); + assert_eq!(10_i128, x.try_into().unwrap()); + let x = BigInt::from(10); + assert_eq!(10_u128, x.try_into().unwrap()); } #[test] From 4a48c03a5539a20a11bb8ea39b5199ad4efaf0c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 22:48:03 +0200 Subject: [PATCH 221/243] add more tests --- src/tests.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index 5ddabcb..c1b207a 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -127,6 +127,18 @@ fn try_into() { assert_eq!(10_i128, x.try_into().unwrap()); let x = BigInt::from(10); assert_eq!(10_u128, x.try_into().unwrap()); + let x = BigInt::from(-123); + assert_eq!(-123_i8, x.try_into().unwrap()); + let x = BigInt::from(-123); + assert_eq!(123_u8, x.try_into().unwrap()); + let x = BigInt::from(-123); + assert_eq!(-123_i16, x.try_into().unwrap()); + let x = BigInt::from(-123); + assert_eq!(123_u32, x.try_into().unwrap()); + let x = BigInt::from(-123); + assert_eq!(-123_i64, x.try_into().unwrap()); + let x = BigInt::from(-123); + assert_eq!(123_u64, x.try_into().unwrap()); } #[test] From 551d3fa28a7c97bfbb50da797adb467c553214a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 22:48:30 +0200 Subject: [PATCH 222/243] generate all TryInto variants by macro --- src/lib.rs | 82 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 33 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7ad3105..506e276 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -156,50 +156,66 @@ fn create_new_digit(position: usize, number: &u8) -> u64 { number * 10_u64.pow(position as u32) } -impl TryInto for BigInt { - type Error = BigIntError; - fn try_into(self) -> Result { - if self > i128::MAX { - return Err(BigIntError::LargeNumber); - } - if self == 0 { - return Ok(0_i128); - } +macro_rules! try_into_uint { + ($($t:ty),*) => { + $(impl TryInto<$t> for BigInt{ + type Error = BigIntError; + fn try_into(self) -> Result<$t, Self::Error> { - let mut result: i128 = 0; + if self > <$t>::MAX { + return Err(BigIntError::LargeNumber); + } + if self == 0 { + return Ok(0 as $t); + } - for (position, number) in self.numbers.iter().rev().enumerate() { - result += create_new_digit(position, number) as i128; - } + let mut result: $t = 0; - if !self.positive { - result *= -1; - } + for (position, number) in self.numbers.iter().rev().enumerate() { + result += create_new_digit(position, number) as $t; + } - Ok(result) - } + Ok(result) + } + })* + + }; } -impl TryInto for BigInt { - type Error = BigIntError; - fn try_into(self) -> Result { - if self > u128::MAX { - return Err(BigIntError::LargeNumber); - } - if self == 0 { - return Ok(0_u128); - } +macro_rules! try_into_int { + ($($t:ty),*) => { + $(impl TryInto<$t> for BigInt{ + type Error = BigIntError; + fn try_into(self) -> Result<$t, Self::Error> { + + if self > <$t>::MAX { + return Err(BigIntError::LargeNumber); + } + if self == 0 { + return Ok(0 as $t); + } - let mut result: u128 = 0; + let mut result: $t = 0; - for (position, number) in self.numbers.iter().rev().enumerate() { - result += create_new_digit(position, number) as u128; - } + for (position, number) in self.numbers.iter().rev().enumerate() { + result += create_new_digit(position, number) as $t; + } - Ok(result) - } + if !self.positive { + result *= -1; + } + + + Ok(result) + } + })* + + }; } +try_into_uint!(u8, u16, u32, u64, u128); +try_into_int!(i8, i16, i32, i64, i128); + impl Display for BigInt { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let mut output = String::new(); From 6f7fc370ea9f99496ca5d90621a1c376c0d67a9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 22:48:42 +0200 Subject: [PATCH 223/243] refactor number_to_hexa --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 506e276..cb62e23 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1031,7 +1031,7 @@ impl BigInt { } fn number_to_hexa(number: BigInt) -> char { - let number: u8 = number.try_into().unwrap_or(0_i128) as u8; + let number: u8 = number.try_into().unwrap_or(0); return match number { 0 => '0', From 151477c1cb0e9ca0fda139a52d27483ef5fe714e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 23:37:50 +0200 Subject: [PATCH 224/243] add macro for assign traits --- src/lib.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index cb62e23..073b9e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -443,6 +443,19 @@ impl PartialOrd<&str> for BigInt { } } +macro_rules! assign_trait { + ($trait:ident, $function:ident, $operation:ident) => { + impl $trait for BigInt + where + T: Into, + { + fn $function(&mut self, rhs: T) { + *self = self.clone().$operation(rhs); + } + } + }; +} + impl Add for BigInt where T: Into, From c28ce18b2103b2cb9a02b01c0a987e3d57f70672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 23:43:39 +0200 Subject: [PATCH 225/243] replace assign functions by macro --- src/lib.rs | 90 ++++++------------------------------------------------ 1 file changed, 10 insertions(+), 80 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 073b9e7..2223090 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -518,14 +518,7 @@ where } } -impl AddAssign for BigInt -where - T: Into, -{ - fn add_assign(&mut self, rhs: T) { - *self = self.clone().add(rhs); - } -} +assign_trait!(AddAssign, add_assign, add); impl Sub for BigInt where @@ -616,14 +609,7 @@ where } } -impl SubAssign for BigInt -where - T: Into, -{ - fn sub_assign(&mut self, rhs: T) { - *self = self.clone().sub(rhs); - } -} +assign_trait!(SubAssign, sub_assign, sub); impl Mul for BigInt where @@ -695,14 +681,7 @@ where } } -impl MulAssign for BigInt -where - T: Into, -{ - fn mul_assign(&mut self, rhs: T) { - *self = self.clone().mul(rhs); - } -} +assign_trait!(MulAssign, mul_assign, mul); impl Div for BigInt where @@ -732,14 +711,7 @@ where } } -impl DivAssign for BigInt -where - T: Into, -{ - fn div_assign(&mut self, rhs: T) { - *self = self.clone().div(rhs); - } -} +assign_trait!(DivAssign, div_assign, div); impl Rem for BigInt where @@ -760,14 +732,7 @@ where } } -impl RemAssign for BigInt -where - T: Into, -{ - fn rem_assign(&mut self, rhs: T) { - *self = self.clone().rem(rhs); - } -} +assign_trait!(RemAssign, rem_assign, rem); impl Pow for BigInt where @@ -813,14 +778,7 @@ where } } -impl BitAndAssign for BigInt -where - T: Into, -{ - fn bitand_assign(&mut self, rhs: T) { - *self = self.clone().bitand(rhs); - } -} +assign_trait!(BitAndAssign, bitand_assign, bitand); impl BitOr for BigInt where @@ -832,14 +790,7 @@ where } } -impl BitOrAssign for BigInt -where - T: Into, -{ - fn bitor_assign(&mut self, rhs: T) { - *self = self.clone().bitor(rhs); - } -} +assign_trait!(BitOrAssign, bitor_assign, bitor); impl BitXor for BigInt where @@ -851,14 +802,7 @@ where } } -impl BitXorAssign for BigInt -where - T: Into, -{ - fn bitxor_assign(&mut self, rhs: T) { - *self = self.clone().bitxor(rhs); - } -} +assign_trait!(BitXorAssign, bitxor_assign, bitxor); impl Shl for BigInt where @@ -878,14 +822,7 @@ where } } -impl ShlAssign for BigInt -where - T: Into, -{ - fn shl_assign(&mut self, rhs: T) { - *self = self.clone().shl(rhs); - } -} +assign_trait!(ShlAssign, shl_assign, shl); impl Shr for BigInt where @@ -905,14 +842,7 @@ where } } -impl ShrAssign for BigInt -where - T: Into, -{ - fn shr_assign(&mut self, rhs: T) { - *self = self.clone().shr(rhs); - } -} +assign_trait!(ShrAssign, shr_assign, shr); impl BigInt { pub fn new() -> BigInt { From 4697d0e9d71d8cf6a9f35368de3152c0ffc00ed3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 23:53:31 +0200 Subject: [PATCH 226/243] change name of to_hexa --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2223090..43343a0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -997,7 +997,7 @@ impl BigInt { }; } - fn to_hexa(&self) -> Vec { + fn to_hexa_vec(&self) -> Vec { let mut final_vec = vec![]; let mut number = self.clone(); @@ -1014,7 +1014,7 @@ impl BigInt { } fn create_hexa_string(&self, f: &mut fmt::Formatter<'_>, uppercase: bool) -> String { - let hexa = self.to_hexa(); + let hexa = self.to_hexa_vec(); let mut output = String::new(); if !self.positive { From 553a65b5a95f72096bbd47de0c39eece851dc37c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Tue, 5 Aug 2025 23:54:16 +0200 Subject: [PATCH 227/243] Refactor to_hexa_vec --- src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 43343a0..0b6fcae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1004,8 +1004,9 @@ impl BigInt { number.positive = true; while number > 0 { - final_vec.push(BigInt::number_to_hexa(number.clone() % 16)); - number /= 16; + let divided = BigInt::divide_with_remainder(number, 16.into()); + final_vec.push(BigInt::number_to_hexa(divided.1)); + number = divided.0; } final_vec.reverse(); From e28b544cc3a52dcb06677989316cfe504e606743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 6 Aug 2025 00:05:06 +0200 Subject: [PATCH 228/243] move allignment logic to separate function --- src/lib.rs | 86 +++++++++++++++++------------------------------------- 1 file changed, 26 insertions(+), 60 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0b6fcae..be384e5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -231,26 +231,7 @@ impl Display for BigInt { output.push_str(&digit.to_string()); } - let mut right_fill = true; - - if f.width().is_some() { - while output.len() < f.width().unwrap() { - match f.align() { - Some(Alignment::Left) => output.push(f.fill()), - Some(Alignment::Center) => { - if right_fill { - output.push(f.fill()); - right_fill = false; - } else { - output.insert(0, f.fill()); - right_fill = true; - } - } - Some(Alignment::Right) => output.insert(0, f.fill()), - _ => output.insert(0, f.fill()), - } - } - } + BigInt::add_allignment(&mut output, f); write!(f, "{output}")?; @@ -282,26 +263,7 @@ impl Binary for BigInt { } } - let mut right_fill = true; - - if f.width().is_some() { - while output.len() < f.width().unwrap() { - match f.align() { - Some(Alignment::Left) => output.push(f.fill()), - Some(Alignment::Center) => { - if right_fill { - output.push(f.fill()); - right_fill = false; - } else { - output.insert(0, f.fill()); - right_fill = true; - } - } - Some(Alignment::Right) => output.insert(0, f.fill()), - _ => output.insert(0, f.fill()), - } - } - } + BigInt::add_allignment(&mut output, f); write!(f, "{output}")?; @@ -1037,26 +999,7 @@ impl BigInt { }) } - let mut right_fill = true; - - if f.width().is_some() { - while output.len() < f.width().unwrap() { - match f.align() { - Some(Alignment::Left) => output.push(f.fill()), - Some(Alignment::Center) => { - if right_fill { - output.push(f.fill()); - right_fill = false; - } else { - output.insert(0, f.fill()); - right_fill = true; - } - } - Some(Alignment::Right) => output.insert(0, f.fill()), - _ => output.insert(0, f.fill()), - } - } - } + BigInt::add_allignment(&mut output, f); output } @@ -1157,6 +1100,29 @@ impl BigInt { (result, partial_sum) } + + fn add_allignment(output: &mut String, f: &mut fmt::Formatter<'_>) { + let mut right_fill = true; + + if f.width().is_some() { + while output.len() < f.width().unwrap() { + match f.align() { + Some(Alignment::Left) => output.push(f.fill()), + Some(Alignment::Center) => { + if right_fill { + output.push(f.fill()); + right_fill = false; + } else { + output.insert(0, f.fill()); + right_fill = true; + } + } + Some(Alignment::Right) => output.insert(0, f.fill()), + _ => output.insert(0, f.fill()), + } + } + } + } } #[cfg(test)] From a8fadbdf09dcd5a17ab73616086f8fae83148c7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 6 Aug 2025 00:13:22 +0200 Subject: [PATCH 229/243] fix double l in alignment --- src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index be384e5..fad4065 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -231,7 +231,7 @@ impl Display for BigInt { output.push_str(&digit.to_string()); } - BigInt::add_allignment(&mut output, f); + BigInt::add_alignment(&mut output, f); write!(f, "{output}")?; @@ -263,7 +263,7 @@ impl Binary for BigInt { } } - BigInt::add_allignment(&mut output, f); + BigInt::add_alignment(&mut output, f); write!(f, "{output}")?; @@ -999,7 +999,7 @@ impl BigInt { }) } - BigInt::add_allignment(&mut output, f); + BigInt::add_alignment(&mut output, f); output } @@ -1101,7 +1101,7 @@ impl BigInt { (result, partial_sum) } - fn add_allignment(output: &mut String, f: &mut fmt::Formatter<'_>) { + fn add_alignment(output: &mut String, f: &mut fmt::Formatter<'_>) { let mut right_fill = true; if f.width().is_some() { From 7543f2f2e985fe6b02ec070b1d087b78685a9469 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 6 Aug 2025 00:35:06 +0200 Subject: [PATCH 230/243] add more tests --- src/tests.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index c1b207a..10b6284 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -160,6 +160,8 @@ fn display() { let x = BigInt::from(1003); assert_eq!(format!("Number is: {x:0^10}"), "Number is: 0001003000"); let x = BigInt::from(1003); + assert_eq!(format!("Number is: {x:0^11}"), "Number is: 00010030000"); + let x = BigInt::from(1003); assert_eq!(format!("Number is: {x:0>10}"), "Number is: 0000001003"); let x = BigInt::from(1003); assert_eq!(format!("Number is: {x:0<10}"), "Number is: 1003000000"); From cd8ce443380ab7d320bed81d4aa868a08a6abc63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 6 Aug 2025 00:35:24 +0200 Subject: [PATCH 231/243] mark unreachable parts of code --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fad4065..91e009f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -853,7 +853,7 @@ impl BigInt { 7 => "seven", 8 => "eight", 9 => "nine", - _ => "zero", + _ => unreachable!(), } } @@ -955,7 +955,7 @@ impl BigInt { 13 => 'D', 14 => 'E', 15 => 'F', - _ => ' ', + _ => unreachable!(), }; } From 713aac447c447250edcaa74971041a5eff8bf0ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 6 Aug 2025 00:35:35 +0200 Subject: [PATCH 232/243] refactor alignment function --- src/lib.rs | 54 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 91e009f..0a4ccc2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1102,26 +1102,46 @@ impl BigInt { } fn add_alignment(output: &mut String, f: &mut fmt::Formatter<'_>) { - let mut right_fill = true; - - if f.width().is_some() { - while output.len() < f.width().unwrap() { - match f.align() { - Some(Alignment::Left) => output.push(f.fill()), - Some(Alignment::Center) => { - if right_fill { - output.push(f.fill()); - right_fill = false; - } else { - output.insert(0, f.fill()); - right_fill = true; - } - } - Some(Alignment::Right) => output.insert(0, f.fill()), - _ => output.insert(0, f.fill()), + let alignment = (f.width().unwrap_or(output.len()) - output.len()) as i32; + + if alignment > 0 { + match f.align() { + Some(Alignment::Left) => { + output.push_str(f.fill().to_string().repeat(alignment as usize).as_str()) + } + Some(Alignment::Center) => { + output.insert_str( + 0, + f.fill() + .to_string() + .repeat((alignment / 2) as usize) + .as_str(), + ); + output.push_str( + f.fill() + .to_string() + .repeat((alignment / 2 + (alignment % 2)) as usize) + .as_str(), + ); } + Some(Alignment::Right) => { + output.insert_str(0, f.fill().to_string().repeat(alignment as usize).as_str()) + } + _ => output.insert_str(0, f.fill().to_string().repeat(alignment as usize).as_str()), } } + + /* + Some(Alignment::Center) => { + if right_fill { + output.push(f.fill()); + right_fill = false; + } else { + output.insert(0, f.fill()); + right_fill = true; + } + } + */ } } From c6ba8e9173d37b0540d996dcaf6b2a3996de2d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 6 Aug 2025 00:37:02 +0200 Subject: [PATCH 233/243] remove useless return --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0a4ccc2..176cd45 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -938,7 +938,7 @@ impl BigInt { fn number_to_hexa(number: BigInt) -> char { let number: u8 = number.try_into().unwrap_or(0); - return match number { + match number { 0 => '0', 1 => '1', 2 => '2', @@ -956,7 +956,7 @@ impl BigInt { 14 => 'E', 15 => 'F', _ => unreachable!(), - }; + } } fn to_hexa_vec(&self) -> Vec { From 3914d34a86b26cc2eec984f3660ef94608ef6291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 6 Aug 2025 00:40:04 +0200 Subject: [PATCH 234/243] remove forgotten comment --- src/lib.rs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 176cd45..30745eb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1130,18 +1130,6 @@ impl BigInt { _ => output.insert_str(0, f.fill().to_string().repeat(alignment as usize).as_str()), } } - - /* - Some(Alignment::Center) => { - if right_fill { - output.push(f.fill()); - right_fill = false; - } else { - output.insert(0, f.fill()); - right_fill = true; - } - } - */ } } From 76772de3a1276559510835da68cef97429f768d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 6 Aug 2025 11:09:24 +0200 Subject: [PATCH 235/243] move method to impl BigInt block --- src/lib.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 30745eb..4586b98 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -144,18 +144,6 @@ macro_rules! from_uint { from_int!(i8, i16, i32, i64, i128); from_uint!(u8, u16, u32, u64, u128); -fn create_new_digit(position: usize, number: &u8) -> u64 { - let number = *number as u64; - - if number == 0 { - return match position { - 0 => 0, - _ => 10.pow(position), - } as u64; - } - number * 10_u64.pow(position as u32) -} - macro_rules! try_into_uint { ($($t:ty),*) => { $(impl TryInto<$t> for BigInt{ @@ -172,7 +160,7 @@ macro_rules! try_into_uint { let mut result: $t = 0; for (position, number) in self.numbers.iter().rev().enumerate() { - result += create_new_digit(position, number) as $t; + result += BigInt::create_new_digit(position, number) as $t; } Ok(result) @@ -198,7 +186,7 @@ macro_rules! try_into_int { let mut result: $t = 0; for (position, number) in self.numbers.iter().rev().enumerate() { - result += create_new_digit(position, number) as $t; + result += BigInt::create_new_digit(position, number) as $t; } if !self.positive { @@ -1131,6 +1119,18 @@ impl BigInt { } } } + + fn create_new_digit(position: usize, number: &u8) -> u64 { + let number = *number as u64; + + if number == 0 { + return match position { + 0 => 0, + _ => 10.pow(position), + } as u64; + } + number * 10_u64.pow(position as u32) + } } #[cfg(test)] From 4ae5eb966690bb23afa50eb849acdafcf99e5232 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Wed, 6 Aug 2025 11:32:20 +0200 Subject: [PATCH 236/243] Create README.md --- README.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..403b9df --- /dev/null +++ b/README.md @@ -0,0 +1,50 @@ +# RustBigInt + +BigIng written in Rust. + +## Description + +My own implementation of BigInt as project to learn Rust. + +## All implemented traits and functions + +- Default +- New +- FromStr +- From +- TryInto +- Display +- Binary +- UpperHex +- LowerHex +- to_words +- PartialEq +- PartialOrd +- Neg (-) +- Add (+) +- AddAssign(+=) +- Sub (-) +- SubAssign (-=) +- Mul (*) +- MulAssign (*=) +- Div (/) +- DivAssign (/=) +- Rem (%) +- RemAssign (%=) +- Pow +- BitAnd (&) +- BitAndAssign (&=) +- BitOr (|) +- BitOrAssign (|=) +- BitXor (^) +- BitXorAssign (^=) +- Shl (<<) +- ShlAssign (<<=) +- Shr (>>) +- ShrAssign (>>=) + + + +## Acknowledgments + +Special thanks to [magnusi](https://github.com/luciusmagn) for leading me throughout this project. \ No newline at end of file From e9444841ad763b1ce14c83e8738c9398823bf140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Fri, 8 Aug 2025 13:24:22 +0200 Subject: [PATCH 237/243] fix typo --- src/tests.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/tests.rs b/src/tests.rs index 10b6284..6d07a71 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,3 +1,7 @@ + + + + use std::str::FromStr; use num_traits::Pow; @@ -689,7 +693,7 @@ fn div_by_zero() { } #[test] -fn reminder() { +fn remainder() { let mut x: BigInt = BigInt::from(10000); let y = BigInt::from(10); let z = x.clone() % y.clone(); From c167c7b74724e177135f945e894b77cb51fb8ba9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Fri, 8 Aug 2025 13:24:36 +0200 Subject: [PATCH 238/243] add LICENSE --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9a7579f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 Adam Sedláček + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file From a3e01614db13da96000d9cb69100f7641446c94d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Fri, 8 Aug 2025 14:42:02 +0200 Subject: [PATCH 239/243] edit README --- README.md | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 101 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 403b9df..8f29c3a 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,110 @@ # RustBigInt -BigIng written in Rust. +BigInt written in Rust. ## Description -My own implementation of BigInt as project to learn Rust. +My own implementation of BigInt (type of integer with unlimited size) as project to learn Rust. + +## Example + +```rs +use RustBigInt::BigInt; + +//from +let x: BigInt = 66.into(); +let y: BigInt = BigInt::from(34); +let z: BigInt = BigInt::from_str("999999").unwrap(); + +//tryInto +let x: BigInt = 32.into(); +let x_num: i32 = x.try_into.unwrap(); +assert_eq!(32, x_num); + +let y: BigInt = i16:MAX.into(); +let y_num: i8 = y.try_into(); +assert!(y_num.is_err()); + +//Display +let x: BigInt = BigInt::from(1003); +assert_eq!(format!("Number is: {x}"), "Number is: 1003"); +assert_eq!(format!("Number is: {x:0^11}"), "Number is: 00010030000"); +assert_eq!(format!("Number is: {x:0>10}"), "Number is: 0000001003"); +assert_eq!(format!("Number is: {x:0<10}"), "Number is: 1003000000"); +assert_eq!(format!("Number is: {x:->10}"), "Number is: ------1003"); +assert_eq!(format!("Number is: {x:9}"), "Number is: 1003"); + +//Binary +let x: BigInt = BigInt::from(11); +assert_eq!(format!("{x:#b}"), "0b1011"); +assert_eq!(format!("{x:b}"), "1011"); +assert_eq!(format!("{x:0^11b}"), "00010110000"); + + +//Hexadecimal +let x: BigInt = BigInt::from(11); +assert_eq!(format!("{x:x}"), "a"); +assert_eq!(format!("{x:X}"), "A"); +assert_eq!(format!("{x:#X}"), "0xA"); +assert_eq!(format!("{x:0^11X}"), "00000A00000"); + +let x: BigInt = BigInt::from(20); +assert_eq!(x.to_words(), "two zero"); + +//Math operations +let x: BigInt = BigInt::from(66); +let y: BigInt = BigInt::from(34); +assert!(x != y); +assert_eq!(x + y, 100); +assert_eq!(x - y, 32); +assert_eq!(x * y, 2244); +assert_eq!(x / y, 1); +assert_eq!(x % y, 32); +assert_eq!(x.pow(2), 4356); + +//Binary operations +let x: BigInt = 11; //Ob1011 +let y: BigInt = 6; //Ob0110 +assert_eq!(x & y, 2); //0b0010 +assert_eq!(x | y, 15); //0b1111 +assert_eq!(x ^ y, 13); //0b1101 +assert_eq!(x >> 2, 2) //0b10 +assert_eq!(x << 2, 44) //0b101100 +``` ## All implemented traits and functions +
+From and Into traits + - Default - New - FromStr - From - TryInto + +
+ +
+ Display traits + - Display - Binary - UpperHex - LowerHex - to_words +
+ +
+ Comparing traits + - PartialEq - PartialOrd +
+ +
+ Math traits + - Neg (-) - Add (+) - AddAssign(+=) @@ -32,6 +117,12 @@ My own implementation of BigInt as project to learn Rust. - Rem (%) - RemAssign (%=) - Pow +
+ + +
+ Bit operation traits + - BitAnd (&) - BitAndAssign (&=) - BitOr (|) @@ -42,9 +133,15 @@ My own implementation of BigInt as project to learn Rust. - ShlAssign (<<=) - Shr (>>) - ShrAssign (>>=) - +
## Acknowledgments -Special thanks to [magnusi](https://github.com/luciusmagn) for leading me throughout this project. \ No newline at end of file +Special thanks to [magnusi](https://github.com/luciusmagn) for leading me throughout this project. + +## License + +Project is licensed under the MIT license. See the LICENSE file for more info. + +Adam Sedláček, 2025 (C) \ No newline at end of file From c1fdc2fa384c11e4a5148f89c474a224da269a91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Fri, 8 Aug 2025 14:42:49 +0200 Subject: [PATCH 240/243] minor formatting fix --- src/tests.rs | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 6d07a71..67a2914 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,7 +1,3 @@ - - - - use std::str::FromStr; use num_traits::Pow; @@ -35,10 +31,7 @@ fn from() { assert!(!x.positive); assert_eq!( x.numbers, - [ - 3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 - ] - .to_vec() + [3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0].to_vec() ); let x = BigInt::from(0); assert!(x.positive); @@ -63,10 +56,7 @@ fn from_string_numbers() { assert!(!x.positive); assert_eq!( x.numbers, - [ - 3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 - ] - .to_vec() + [3, 2, 0, 0, 2, 0, 0, 0, 0, 9, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0].to_vec() ); let x = BigInt::from_str("-0").unwrap(); assert!(x.positive); From 54712c700cfbada70bbc64798115fbb8d28a4804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Fri, 8 Aug 2025 15:29:57 +0200 Subject: [PATCH 241/243] change package name --- Cargo.lock | 14 +++++++------- Cargo.toml | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ff48f55..d610d6a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,13 +8,6 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" -[[package]] -name = "big_int" -version = "0.1.0" -dependencies = [ - "num-traits", -] - [[package]] name = "num-traits" version = "0.2.19" @@ -23,3 +16,10 @@ checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", ] + +[[package]] +name = "rust_big_int" +version = "0.1.0" +dependencies = [ + "num-traits", +] diff --git a/Cargo.toml b/Cargo.toml index a4dede9..0b33848 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "big_int" +name = "rust_big_int" version = "0.1.0" edition = "2024" From ce0e5c574ad8e70e8c9dbd5fdd593790a9b85964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Fri, 8 Aug 2025 17:41:39 +0200 Subject: [PATCH 242/243] fix example in README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8f29c3a..e629a75 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ My own implementation of BigInt (type of integer with unlimited size) as project ```rs use RustBigInt::BigInt; +use std::str::FromStr; //from let x: BigInt = 66.into(); From 73c8889a2f84f6cba45e1e09ed00c2f95c074b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Sedl=C3=A1=C4=8Dek?= Date: Fri, 8 Aug 2025 17:41:52 +0200 Subject: [PATCH 243/243] add integration test --- src/lib.rs | 4 +- tests/integration_tests.rs | 129 +++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 tests/integration_tests.rs diff --git a/src/lib.rs b/src/lib.rs index 4586b98..7973e05 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ use std::ops::*; use std::str::FromStr; #[derive(Debug)] -enum BigIntError { +pub enum BigIntError { NaN, LargeNumber, } @@ -24,7 +24,7 @@ impl Display for BigIntError { } #[derive(Clone, Eq, Debug)] -struct BigInt { +pub struct BigInt { positive: bool, numbers: Vec, } diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs new file mode 100644 index 0000000..242aad0 --- /dev/null +++ b/tests/integration_tests.rs @@ -0,0 +1,129 @@ +use rust_big_int::BigInt; +use std::str::FromStr; + +#[test] +fn catalan_number() { + let mut calculated_numbers: Vec = vec![1.into()]; + + for _i in 1..100 { + let number_pairs = calculated_numbers + .iter() + .zip(calculated_numbers.iter().rev()); + + let result: BigInt = number_pairs.fold(BigInt::default(), |result, (x, y)| { + x.clone() * y.clone() + result + }); + + calculated_numbers.push(result); + } + + let catalan_numbers: Vec = vec![ + BigInt::from_str("1").unwrap(), + BigInt::from_str("1").unwrap(), + BigInt::from_str("2").unwrap(), + BigInt::from_str("5").unwrap(), + BigInt::from_str("14").unwrap(), + BigInt::from_str("42").unwrap(), + BigInt::from_str("132").unwrap(), + BigInt::from_str("429").unwrap(), + BigInt::from_str("1430").unwrap(), + BigInt::from_str("4862").unwrap(), + BigInt::from_str("16796").unwrap(), + BigInt::from_str("58786").unwrap(), + BigInt::from_str("208012").unwrap(), + BigInt::from_str("742900").unwrap(), + BigInt::from_str("2674440").unwrap(), + BigInt::from_str("9694845").unwrap(), + BigInt::from_str("35357670").unwrap(), + BigInt::from_str("129644790").unwrap(), + BigInt::from_str("477638700").unwrap(), + BigInt::from_str("1767263190").unwrap(), + BigInt::from_str("6564120420").unwrap(), + BigInt::from_str("24466267020").unwrap(), + BigInt::from_str("91482563640").unwrap(), + BigInt::from_str("343059613650").unwrap(), + BigInt::from_str("1289904147324").unwrap(), + BigInt::from_str("4861946401452").unwrap(), + BigInt::from_str("18367353072152").unwrap(), + BigInt::from_str("69533550916004").unwrap(), + BigInt::from_str("263747951750360").unwrap(), + BigInt::from_str("1002242216651368").unwrap(), + BigInt::from_str("3814986502092304").unwrap(), + BigInt::from_str("14544636039226909").unwrap(), + BigInt::from_str("55534064877048198").unwrap(), + BigInt::from_str("212336130412243110").unwrap(), + BigInt::from_str("812944042149730764").unwrap(), + BigInt::from_str("3116285494907301262").unwrap(), + BigInt::from_str("11959798385860453492").unwrap(), + BigInt::from_str("45950804324621742364").unwrap(), + BigInt::from_str("176733862787006701400").unwrap(), + BigInt::from_str("680425371729975800390").unwrap(), + BigInt::from_str("2622127042276492108820").unwrap(), + BigInt::from_str("10113918591637898134020").unwrap(), + BigInt::from_str("39044429911904443959240").unwrap(), + BigInt::from_str("150853479205085351660700").unwrap(), + BigInt::from_str("583300119592996693088040").unwrap(), + BigInt::from_str("2257117854077248073253720").unwrap(), + BigInt::from_str("8740328711533173390046320").unwrap(), + BigInt::from_str("33868773757191046886429490").unwrap(), + BigInt::from_str("131327898242169365477991900").unwrap(), + BigInt::from_str("509552245179617138054608572").unwrap(), + BigInt::from_str("1978261657756160653623774456").unwrap(), + BigInt::from_str("7684785670514316385230816156").unwrap(), + BigInt::from_str("29869166945772625950142417512").unwrap(), + BigInt::from_str("116157871455782434250553845880").unwrap(), + BigInt::from_str("451959718027953471447609509424").unwrap(), + BigInt::from_str("1759414616608818870992479875972").unwrap(), + BigInt::from_str("6852456927844873497549658464312").unwrap(), + BigInt::from_str("26700952856774851904245220912664").unwrap(), + BigInt::from_str("104088460289122304033498318812080").unwrap(), + BigInt::from_str("405944995127576985730643443367112").unwrap(), + BigInt::from_str("1583850964596120042686772779038896").unwrap(), + BigInt::from_str("6182127958584855650487080847216336").unwrap(), + BigInt::from_str("24139737743045626825711458546273312").unwrap(), + BigInt::from_str("94295850558771979787935384946380125").unwrap(), + BigInt::from_str("368479169875816659479009042713546950").unwrap(), + BigInt::from_str("1440418573150919668872489894243865350").unwrap(), + BigInt::from_str("5632681584560312734993915705849145100").unwrap(), + BigInt::from_str("22033725021956517463358552614056949950").unwrap(), + BigInt::from_str("86218923998960285726185640663701108500").unwrap(), + BigInt::from_str("337485502510215975556783793455058624700").unwrap(), + BigInt::from_str("1321422108420282270489942177190229544600").unwrap(), + BigInt::from_str("5175569924646105559418940193995065716350").unwrap(), + BigInt::from_str("20276890389709399862928998568254641025700").unwrap(), + BigInt::from_str("79463489365077377841208237632349268884500").unwrap(), + BigInt::from_str("311496878311103321137536291518809134027240").unwrap(), + BigInt::from_str("1221395654430378811828760722007962130791020").unwrap(), + BigInt::from_str("4790408930363303911328386208394864461024520").unwrap(), + BigInt::from_str("18793142726809884575211361279087545193250040").unwrap(), + BigInt::from_str("73745243611532458459690151854647329239335600").unwrap(), + BigInt::from_str("289450081175264899454283846029490767264392230").unwrap(), + BigInt::from_str("1136359577947336271931632877004667456667613940").unwrap(), + BigInt::from_str("4462290049988320482463241297506133183499654740").unwrap(), + BigInt::from_str("17526585015616776834735140517915655636396234280").unwrap(), + BigInt::from_str("68854441132780194707888052034668647142985206100").unwrap(), + BigInt::from_str("270557451039395118028642463289168566420671280440").unwrap(), + BigInt::from_str("1063353702922273835973036658043476458723103404520").unwrap(), + BigInt::from_str("4180080073556524734514695828170907458428751314320").unwrap(), + BigInt::from_str("16435314834665426797069144960762886143367590394940").unwrap(), + BigInt::from_str("64633260585762914370496637486146181462681535261000").unwrap(), + BigInt::from_str("254224158304000796523953440778841647086547372026600").unwrap(), + BigInt::from_str("1000134600800354781929399250536541864362461089950800").unwrap(), + BigInt::from_str("3935312233584004685417853572763349509774031680023800").unwrap(), + BigInt::from_str("15487357822491889407128326963778343232013931127835600").unwrap(), + BigInt::from_str("60960876535340415751462563580829648891969728907438000").unwrap(), + BigInt::from_str("239993345518077005168915776623476723006280827488229600").unwrap(), + BigInt::from_str("944973797977428207852605870454939596837230758234904050").unwrap(), + BigInt::from_str("3721443204405954385563870541379246659709506697378694300").unwrap(), + BigInt::from_str("14657929356129575437016877846657032761712954950899755100").unwrap(), + BigInt::from_str("57743358069601357782187700608042856334020731624756611000").unwrap(), + BigInt::from_str("227508830794229349661819540395688853956041682601541047340").unwrap(), + BigInt::from_str("896519947090131496687170070074100632420837521538745909320").unwrap(), + ]; + + let result_check = calculated_numbers.iter().zip(catalan_numbers.iter()); + + for result in result_check { + assert_eq!(result.0, result.1); + } +}