clarify token delta overflow guard · Entire

clarify token delta overflow guard

50dc209·

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

Sessions

65d01316b402View transcript

Changes

2

450 unmodified lines

451
452
453
454
455
454
455
456
457
458
459
460
461
462
463
464
465
466

450 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

2488 unmodified lines

2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538

2488 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()