Skip to content

Commit 74a16ed

Browse files
committed
Widen tolerance for random tests on WASI
With only 1K elements (vs 1M native), the statistical variance is much higher. The tolerance bounds are now adjusted based on sample size: - WASI: ±0.10 for rand, ±0.50 for randint (1K samples) - Native: ±0.02 for both (1M samples)
1 parent 5277f8f commit 74a16ed

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

Tests/MatftTests/RandomTest.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,22 @@ final class RandomTests: XCTestCase {
1313
func testRand() {
1414
// Use smaller array size on WASM to avoid memory pressure
1515
// 100x100x100 = 1M elements = 4MB per array, too much for WASM
16+
// Smaller sample size requires wider tolerance for mean (higher variance)
1617
#if os(WASI)
1718
let shape = [10, 10, 10] // 1K elements
19+
let tolerance: Float = 0.10 // Wider bounds for smaller sample
1820
#else
1921
let shape = [100, 100, 100] // 1M elements
22+
let tolerance: Float = 0.02 // Tight bounds for large sample
2023
#endif
2124

2225
let a = Matft.random.rand(shape: shape)
2326
let mean_a = a.mean().scalar as! Float
24-
let cond_a = (Float(0.48) < mean_a) && (mean_a < Float(0.52))
27+
let cond_a = (Float(0.5 - tolerance) < mean_a) && (mean_a < Float(0.5 + tolerance))
2528

2629
let b = Matft.random.rand(shape: shape)
2730
let mean_b = b.mean().scalar as! Float
28-
let cond_b = (Float(0.48) < mean_b) && (mean_b < Float(0.52))
31+
let cond_b = (Float(0.5 - tolerance) < mean_b) && (mean_b < Float(0.5 + tolerance))
2932

3033
XCTAssertTrue(cond_a, "The result may be failure due to random generation")
3134
XCTAssertTrue(cond_b, "The result may be failure due to random generation")
@@ -35,19 +38,22 @@ final class RandomTests: XCTestCase {
3538

3639
func testRandint() {
3740
// Use smaller array size on WASM to avoid memory pressure
41+
// Smaller sample size requires wider tolerance for mean (higher variance)
3842
#if os(WASI)
3943
let shape = [10, 10, 10] // 1K elements
44+
let tolerance: Float = 0.50 // Wider bounds for smaller sample
4045
#else
4146
let shape = [100, 100, 100] // 1M elements
47+
let tolerance: Float = 0.02 // Tight bounds for large sample
4248
#endif
4349

4450
let a = Matft.random.randint(low: 0, high: 10, shape: shape)
4551
let mean_a = a.mean().scalar as! Float
46-
let cond_a = (Float(4.48) < mean_a) && (mean_a < Float(4.52))
52+
let cond_a = (Float(4.5 - tolerance) < mean_a) && (mean_a < Float(4.5 + tolerance))
4753

4854
let b = Matft.random.randint(low: 0, high: 10, shape: shape)
4955
let mean_b = b.mean().scalar as! Float
50-
let cond_b = (Float(4.48) < mean_b) && (mean_b < Float(4.52))
56+
let cond_b = (Float(4.5 - tolerance) < mean_b) && (mean_b < Float(4.5 + tolerance))
5157

5258
XCTAssertTrue(cond_a, "The result may be failure due to random generation")
5359
XCTAssertTrue(cond_b, "The result may be failure due to random generation")

0 commit comments

Comments
 (0)