|
| 1 | +package aichat |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/FloatTech/zbputils/ctxext" |
| 7 | +) |
| 8 | + |
| 9 | +func TestStorage_rate(t *testing.T) { |
| 10 | + s := storage(ctxext.Storage(0)) |
| 11 | + |
| 12 | + // 测试默认值 |
| 13 | + if rate := s.rate(); rate != 0 { |
| 14 | + t.Errorf("default rate() = %v, want 0", rate) |
| 15 | + } |
| 16 | + |
| 17 | + // 设置值并测试 |
| 18 | + s = storage((ctxext.Storage)(s).Set(int64(100), bitmaprate)) |
| 19 | + if rate := s.rate(); rate != 100 { |
| 20 | + t.Errorf("rate() after set = %v, want 100", rate) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func TestStorage_temp(t *testing.T) { |
| 25 | + s := storage(ctxext.Storage(0)) |
| 26 | + |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + setValue int64 |
| 30 | + expected float32 |
| 31 | + }{ |
| 32 | + {"default temp (0)", 0, 0.70}, // 默认值 70/100 |
| 33 | + {"valid temp 50", 50, 0.50}, // 50/100 = 0.50 |
| 34 | + {"valid temp 80", 80, 0.80}, // 80/100 = 0.80 |
| 35 | + {"max temp 100", 100, 1.00}, // 100/100 = 1.00 |
| 36 | + {"over max temp", 127, 1.00}, // 限制为 100/100 = 1.00 |
| 37 | + {"negative temp", -10, 0.70}, // 默认值 70/100 |
| 38 | + } |
| 39 | + |
| 40 | + for _, tt := range tests { |
| 41 | + t.Run(tt.name, func(t *testing.T) { |
| 42 | + s = storage((ctxext.Storage)(s).Set(tt.setValue, bitmaptemp)) |
| 43 | + |
| 44 | + result := s.temp() |
| 45 | + if result != tt.expected { |
| 46 | + t.Errorf("temp() = %v, want %v", result, tt.expected) |
| 47 | + } |
| 48 | + }) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestStorage_noagent(t *testing.T) { |
| 53 | + s := storage(ctxext.Storage(0)) |
| 54 | + |
| 55 | + // 测试默认值 |
| 56 | + if noagent := s.noagent(); noagent != false { |
| 57 | + t.Errorf("default noagent() = %v, want false", noagent) |
| 58 | + } |
| 59 | + |
| 60 | + // 设置为 true 并测试 |
| 61 | + s = storage((ctxext.Storage)(s).Set(1, bitmapnagt)) |
| 62 | + if noagent := s.noagent(); noagent != true { |
| 63 | + t.Errorf("noagent() after set true = %v, want true", noagent) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestStorage_norecord(t *testing.T) { |
| 68 | + s := storage(ctxext.Storage(0)) |
| 69 | + |
| 70 | + // 测试默认值 |
| 71 | + if norecord := s.norecord(); norecord != false { |
| 72 | + t.Errorf("default norecord() = %v, want false", norecord) |
| 73 | + } |
| 74 | + |
| 75 | + // 设置为 true 并测试 |
| 76 | + s = storage((ctxext.Storage)(s).Set(1, bitmapnrec)) |
| 77 | + if norecord := s.norecord(); norecord != true { |
| 78 | + t.Errorf("norecord() after set true = %v, want true", norecord) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestStorage_noreplyat(t *testing.T) { |
| 83 | + s := storage(ctxext.Storage(0)) |
| 84 | + |
| 85 | + // 测试默认值 |
| 86 | + if noreplyat := s.noreplyat(); noreplyat != false { |
| 87 | + t.Errorf("default noreplyat() = %v, want false", noreplyat) |
| 88 | + } |
| 89 | + |
| 90 | + // 设置为 true 并测试 |
| 91 | + s = storage((ctxext.Storage)(s).Set(1, bitmapnrat)) |
| 92 | + if noreplyat := s.noreplyat(); noreplyat != true { |
| 93 | + t.Errorf("noreplyat() after set true = %v, want true", noreplyat) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestStorage_Integration(t *testing.T) { |
| 98 | + s := storage(ctxext.Storage(0)) |
| 99 | + |
| 100 | + // 设置各种值 |
| 101 | + s = storage((ctxext.Storage)(s).Set(int64(75), bitmaprate)) |
| 102 | + s = storage((ctxext.Storage)(s).Set(int64(85), bitmaptemp)) |
| 103 | + s = storage((ctxext.Storage)(s).Set(1, bitmapnagt)) |
| 104 | + s = storage((ctxext.Storage)(s).Set(0, bitmapnrec)) |
| 105 | + s = storage((ctxext.Storage)(s).Set(1, bitmapnrat)) |
| 106 | + |
| 107 | + // 验证所有方法 |
| 108 | + if rate := s.rate(); rate != 75 { |
| 109 | + t.Errorf("rate() = %v, want 75", rate) |
| 110 | + } |
| 111 | + |
| 112 | + if temp := s.temp(); temp != 0.85 { |
| 113 | + t.Errorf("temp() = %v, want 0.85", temp) |
| 114 | + } |
| 115 | + |
| 116 | + if noagent := s.noagent(); !noagent { |
| 117 | + t.Errorf("noagent() = %v, want true", noagent) |
| 118 | + } |
| 119 | + |
| 120 | + if norecord := s.norecord(); norecord { |
| 121 | + t.Errorf("norecord() = %v, want false", norecord) |
| 122 | + } |
| 123 | + |
| 124 | + if noreplyat := s.noreplyat(); !noreplyat { |
| 125 | + t.Errorf("noreplyat() = %v, want true", noreplyat) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func BenchmarkStorage_rate(b *testing.B) { |
| 130 | + s := storage(ctxext.Storage(0)) |
| 131 | + |
| 132 | + s = storage((ctxext.Storage)(s).Set(int64(100), bitmaprate)) |
| 133 | + |
| 134 | + b.ResetTimer() |
| 135 | + for i := 0; i < b.N; i++ { |
| 136 | + s.rate() |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func BenchmarkStorage_temp(b *testing.B) { |
| 141 | + s := storage(ctxext.Storage(0)) |
| 142 | + |
| 143 | + s = storage((ctxext.Storage)(s).Set(int64(80), bitmaptemp)) |
| 144 | + |
| 145 | + b.ResetTimer() |
| 146 | + for i := 0; i < b.N; i++ { |
| 147 | + s.temp() |
| 148 | + } |
| 149 | +} |
0 commit comments