@@ -152,3 +152,109 @@ fun msb_handles_u128_values() {
152152 let mid_bit: u128 = 1u128 << 40 ;
153153 assert_eq !(common::msb (mid_bit as u256 , 128 ), 40 );
154154}
155+
156+ // === sqrt ===
157+
158+ #[test]
159+ fun sqrt_returns_zero_for_zero () {
160+ assert_eq !(common::sqrt_floor (0 ), 0 );
161+ }
162+
163+ #[test]
164+ fun sqrt_handles_perfect_squares () {
165+ assert_eq !(common::sqrt_floor (4 ), 2 );
166+ assert_eq !(common::sqrt_floor (9 ), 3 );
167+ assert_eq !(common::sqrt_floor (16 ), 4 );
168+ assert_eq !(common::sqrt_floor (25 ), 5 );
169+ assert_eq !(common::sqrt_floor (100 ), 10 );
170+ assert_eq !(common::sqrt_floor (256 ), 16 );
171+ assert_eq !(common::sqrt_floor (65536 ), 256 );
172+ assert_eq !(common::sqrt_floor (1 << 64 ), 1 << 32 );
173+ assert_eq !(common::sqrt_floor (1 << 128 ), 1 << 64 );
174+ }
175+
176+ #[test]
177+ fun sqrt_floors_non_perfect_squares () {
178+ assert_eq !(common::sqrt_floor (2 ), 1 ); // 1.414... → 1
179+ assert_eq !(common::sqrt_floor (3 ), 1 ); // 1.732... → 1
180+ assert_eq !(common::sqrt_floor (5 ), 2 ); // 2.236... → 2
181+ assert_eq !(common::sqrt_floor (8 ), 2 ); // 2.828... → 2
182+ assert_eq !(common::sqrt_floor (15 ), 3 ); // 3.873... → 3
183+ assert_eq !(common::sqrt_floor (99 ), 9 ); // 9.950... → 9
184+ assert_eq !(common::sqrt_floor (255 ), 15 ); // 15.969... → 15
185+ }
186+
187+ #[test]
188+ fun sqrt_handles_u8_values () {
189+ let zero: u8 = 0 ;
190+ assert_eq !(common::sqrt_floor (zero as u256 ), 0 );
191+
192+ let one: u8 = 1 ;
193+ assert_eq !(common::sqrt_floor (one as u256 ), 1 );
194+
195+ let four: u8 = 4 ;
196+ assert_eq !(common::sqrt_floor (four as u256 ), 2 );
197+
198+ let max: u8 = std::u8 ::max_value !();
199+ assert_eq !(common::sqrt_floor (max as u256 ), 15 );
200+ }
201+
202+ #[test]
203+ fun sqrt_handles_u16_values () {
204+ let perfect: u16 = 256 ;
205+ assert_eq !(common::sqrt_floor (perfect as u256 ), 16 );
206+
207+ let non_perfect: u16 = 1000 ;
208+ assert_eq !(common::sqrt_floor (non_perfect as u256 ), 31 );
209+
210+ let max: u16 = std::u16 ::max_value !();
211+ assert_eq !(common::sqrt_floor (max as u256 ), 255 );
212+ }
213+
214+ #[test]
215+ fun sqrt_handles_u32_values () {
216+ let perfect: u32 = 65536 ;
217+ assert_eq !(common::sqrt_floor (perfect as u256 ), 256 );
218+
219+ let non_perfect: u32 = 1000000 ;
220+ assert_eq !(common::sqrt_floor (non_perfect as u256 ), 1000 );
221+
222+ let max: u32 = std::u32 ::max_value !();
223+ assert_eq !(common::sqrt_floor (max as u256 ), 65535 );
224+ }
225+
226+ #[test]
227+ fun sqrt_handles_u64_values () {
228+ let perfect: u64 = 1 << 32 ;
229+ assert_eq !(common::sqrt_floor (perfect as u256 ), 1 << 16 );
230+
231+ let non_perfect: u64 = 100000000 ;
232+ assert_eq !(common::sqrt_floor (non_perfect as u256 ), 10000 );
233+
234+ let max: u64 = std::u64 ::max_value !();
235+ assert_eq !(common::sqrt_floor (max as u256 ), 4294967295 );
236+ }
237+
238+ #[test]
239+ fun sqrt_handles_u128_values () {
240+ let perfect: u128 = 1 << 64 ;
241+ assert_eq !(common::sqrt_floor (perfect as u256 ), 1 << 32 );
242+
243+ let large: u128 = 1000000000000000000 ;
244+ assert_eq !(common::sqrt_floor (large as u256 ), 1000000000 );
245+
246+ let max: u128 = std::u128 ::max_value !();
247+ assert_eq !(common::sqrt_floor (max as u256 ), std::u64 ::max_value !() as u256 );
248+ }
249+
250+ #[test]
251+ fun sqrt_handles_u256_values () {
252+ let perfect: u256 = 1 << 128 ;
253+ assert_eq !(common::sqrt_floor (perfect), 1 << 64 );
254+
255+ let large: u256 = 1 << 200 ;
256+ assert_eq !(common::sqrt_floor (large), 1 << 100 );
257+
258+ let max: u256 = std::u256 ::max_value !();
259+ assert_eq !(common::sqrt_floor (max), std::u128 ::max_value !() as u256 );
260+ }
0 commit comments