clarify token delta overflow guard · Entire

clarify token delta overflow guard

ff9b673·

peyton-alt·3w ago·2 files·+54 added/-2 removed

Sessions

65d01316b402View transcript

Changes

2

412 unmodified lines

413
414
415
416
417
416
417
418
419
420
421
422
423
424
425
426
427
428

412 unmodified lines

}

func saturatingIntSub(a, b int) int {
    if b < 0 && a > maxInt()+b {
        return maxInt()
    if b < 0 {
        if b == minInt() {
            if a >= 0 {
                return maxInt()
            }
            return a - b
        }
        if a > maxInt()-(-b) {
            return maxInt()
        }
    }
    if b > 0 && a < minInt()+b {
        return minInt()

Mcmd/entire/cli/checkpoint_tokens.go+10/-2

2158 unmodified lines

2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208

2158 unmodified lines

}
}

func TestSaturatingIntSubHandlesMinIntSubtrahend(t *testing.T) {
    t.Parallel()

maxInt := int(^uint(0) >> 1)
    minInt := -maxInt - 1

tests := []struct {
        name string
        a    int
        want int
    }{
        {
            name: "clamps non-negative minuend",
            a:    0,
            want: maxInt,
        },
        {
            name: "keeps max exact result",
            a:    -1,
            want: maxInt,
        },
        {
            name: "keeps representable result",
            a:    -2,
            want: maxInt - 1,
        },
        {
            name: "keeps zero exact result",
            a:    minInt,
            want: 0,
        },
    }

for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            t.Parallel()

if got := saturatingIntSub(tt.a, minInt); got != tt.want {
                t.Fatalf("saturatingIntSub(%d, minInt) = %d, want %d", tt.a, got, tt.want)
            }
        })
    }
}

func TestCheckpointTokensCmd_ComparisonUnavailableWhenBaselineTokenDataMissing(t *testing.T) {
    repo, _ := runExplainAutoTestRepo(t)
    ctx := context.Background()