47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package rand_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"apigo.cc/go/rand"
|
|
)
|
|
|
|
func TestGenericInt(t *testing.T) {
|
|
// 1. 普通正数区间
|
|
for range 100 {
|
|
v := rand.Int(10, 20)
|
|
if v < 10 || v > 20 { t.Errorf("Int failed: %d", v) }
|
|
}
|
|
|
|
// 2. 负数区间
|
|
for range 100 {
|
|
v := rand.Int(-50, -10)
|
|
if v < -50 || v > -10 { t.Errorf("Negative range failed: %d", v) }
|
|
}
|
|
|
|
// 3. 防御性测试
|
|
if rand.Int(100, 10) != 100 { t.Error("Reverse range should return min") }
|
|
}
|
|
|
|
func TestFastInt(t *testing.T) {
|
|
for range 100 {
|
|
v := rand.FastInt(1, 100)
|
|
if v < 1 || v > 100 { t.Errorf("FastInt failed: %d", v) }
|
|
}
|
|
}
|
|
|
|
func TestBytes(t *testing.T) {
|
|
// 1. 正常长度
|
|
bs := rand.Bytes(10)
|
|
if len(bs) != 10 { t.Errorf("Expected length 10, got %d", len(bs)) }
|
|
|
|
// 2. 边界长度
|
|
if len(rand.Bytes(0)) != 0 { t.Error("Bytes(0) should return empty") }
|
|
if len(rand.Bytes(-1)) != 0 { t.Error("Bytes(-1) should return empty") }
|
|
|
|
// 3. 随机性简单验证 (不为空)
|
|
bs1 := rand.Bytes(32)
|
|
bs2 := rand.Bytes(32)
|
|
if string(bs1) == string(bs2) { t.Error("Generated bytes should likely be different") }
|
|
}
|