| new file mode 100644 |
| index 00000000..ca754045 |
| --- /dev/null |
| +++ b/internal/schedule/schedule_test.go |
| @@ -0,0 +1,404 @@ |
| +package schedule |
| + |
| +import ( |
| + "fmt" |
| + "math/rand/v2" |
| + "reflect" |
| + "slices" |
| + "testing" |
| + "time" |
| +) |
| + |
| +var t0 = time.Date(2026, 11, 12, 9, 0, 0, 0, time.UTC) |
| + |
| +func at(d time.Duration) *time.Time { t := t0.Add(d); return &t } |
| + |
| +func sub(id, provider string, used ...float64) Conn { |
| + c := Conn{ID: id, Provider: provider, Family: provider, Account: id, Billing: Subscription, Capable: true, State: StateHealthy} |
| + for i, u := range used { |
| + c.Windows = append(c.Windows, Window{Name: fmt.Sprintf("w%d", i), Used: u, Minutes: int64(300 * (i + 1)), ResetsAt: at(time.Duration(i+1) * time.Hour)}) |
| + } |
| + return c |
| +} |
| + |
| +func meter(id string, day, month int64) Conn { |
| + return Conn{ID: id, Provider: "tensorx", Family: "glm", Account: id, Billing: Metered, Capable: true, State: StateHealthy, SpendDay: day, SpendMonth: month, BudgetDay: 1_000_000, BudgetMonth: 10_000_000} |
| +} |
| + |
| +func in(step Step, cs ...Conn) Input { |
| + if step.Role == "" { |
| + step.Role = RoleEngineering |
| + } |
| + return Input{Version: Version, Now: t0, Step: step, Conns: cs} |
| +} |
| + |
| +// TestDecideRules is one table row per rule of §7 (and the bars). |
| +func TestDecideRules(t *testing.T) { |
| + limited := sub("c-lim", "claude") |
| + limited.State, limited.LimitedUntil = StateLimited, t0.Add(2*time.Hour) |
| + passed := sub("c-back", "claude") |
| + passed.State, passed.LimitedUntil = StateLimited, t0.Add(-time.Minute) |
| + expired := sub("c-exp", "claude", 0.95) |
| + expired.Windows[0].ResetsAt = at(-time.Minute) |
| + busy := meter("t-busy", 0, 0) |
| + busy.Busy = true |
| + unknown := meter("t-unk", 0, 0) |
| + unknown.SpendUnknown = true |
| + refused := sub("c-ref", "claude") |
| + refused.State = "sign_in_again" |
| + cases := []struct { |
| + name string |
| + in Input |
| + run bool |
| + conn string |
| + reason string |
| + }{ |
| + // Bars. |
| + {"not capable", in(Step{}, Conn{ID: "x", Billing: Subscription, State: StateHealthy}), false, "", ReasonNoConnection}, |
| + {"refused is not usable", in(Step{}, refused), false, "", ReasonNoConnection}, |
| + {"limited is not usable", in(Step{}, limited), false, "c-lim", ReasonHeadroom}, |
| + {"limited with its reset passed is usable", in(Step{}, passed), true, "c-back", ReasonHeadroom}, |
| + {"avoided", in(Step{Avoid: []string{"a"}}, sub("a", "claude"), sub("b", "codex")), true, "b", ReasonHeadroom}, |
| + // Thresholds. |
| + {"low holds at 0.61", in(Step{Priority: Low}, sub("a", "claude", 0.61)), false, "a", ReasonHeadroom}, |
| + {"low runs at 0.59", in(Step{Priority: Low}, sub("a", "claude", 0.59)), true, "a", ReasonHeadroom}, |
| + {"normal holds at 0.80", in(Step{}, sub("a", "claude", 0.80)), false, "a", ReasonHeadroom}, |
| + {"normal runs at 0.79", in(Step{Priority: Normal}, sub("a", "claude", 0.79)), true, "a", ReasonHeadroom}, |
| + {"urgent runs at 0.99", in(Step{Priority: Urgent}, sub("a", "claude", 0.99)), true, "a", ReasonHeadroom}, |
| + {"urgent holds at 1.0", in(Step{Priority: Urgent}, sub("a", "claude", 1.0)), false, "a", ReasonHeadroom}, |
| + {"every window counts", in(Step{}, sub("a", "claude", 0.1, 0.85)), false, "a", ReasonHeadroom}, |
| + {"no window reported admits", in(Step{Priority: Low}, sub("a", "claude")), true, "a", ReasonHeadroom}, |
| + {"a window past its reset is not reported", in(Step{Priority: Low}, expired), true, "c-exp", ReasonHeadroom}, |
| + {"a release is urgent for thresholds", in(Step{Priority: Low, Release: "r1"}, sub("a", "claude", 0.9)), true, "a", ReasonHeadroom}, |
| + // Rule 1: the kept session. |
| + {"kept stays whatever the threshold", in(Step{Kept: "a", Priority: Low}, sub("a", "claude", 0.95), sub("b", "codex")), true, "a", ReasonSticky}, |
| + {"kept not usable moves", in(Step{Kept: "c-lim"}, limited, sub("b", "codex")), true, "b", ReasonHeadroom}, |
| + {"kept avoided moves", in(Step{Kept: "a", Avoid: []string{"a"}}, sub("a", "claude"), sub("b", "codex")), true, "b", ReasonHeadroom}, |
| + {"kept metered with leave stays", in(Step{Kept: "t", Priority: Urgent}, meter("t", 0, 0), sub("b", "codex")), true, "t", ReasonSticky}, |
| + {"kept metered busy holds there", in(Step{Kept: "t-busy", Priority: Urgent}, busy, sub("b", "codex")), false, "t-busy", ReasonBusy}, |
| + {"kept metered over budget holds there", in(Step{Kept: "t", Release: "r"}, meter("t", 1_000_000, 0), sub("b", "codex")), false, "t", ReasonBudget}, |
| + {"kept metered spend unknown holds there", in(Step{Kept: "t-unk", Priority: Urgent}, unknown, sub("b", "codex")), false, "t-unk", ReasonSpendUnknown}, |
| + {"kept metered without leave moves", in(Step{Kept: "t"}, meter("t", 0, 0), sub("b", "codex")), true, "b", ReasonHeadroom}, |
| + // Rule 2: the preference. |
| + {"preferred when it admits", Input{Now: t0, Step: Step{Role: RoleEngineering}, Conns: []Conn{sub("a", "claude", 0.5), sub("b", "codex", 0.1)}, Preference: Preference{Provider: "claude"}}, true, "a", ReasonPreference}, |
| + {"preferred over threshold is skipped", Input{Now: t0, Step: Step{Role: RoleEngineering}, Conns: []Conn{sub("a", "claude", 0.85), sub("b", "codex", 0.1)}, Preference: Preference{Provider: "claude"}}, true, "b", ReasonHeadroom}, |
| + {"an expired preference is absent", Input{Now: t0, Step: Step{Role: RoleEngineering}, Conns: []Conn{sub("a", "claude", 0.5), sub("b", "codex", 0.1)}, Preference: Preference{Provider: "claude", Until: at(-time.Second)}}, true, "b", ReasonHeadroom}, |
| + {"a preference in force", Input{Now: t0, Step: Step{Role: RoleEngineering}, Conns: []Conn{sub("a", "claude", 0.5), sub("b", "codex", 0.1)}, Preference: Preference{Provider: "claude", Until: at(time.Hour)}}, true, "a", ReasonPreference}, |
| + {"a metered preference never beats a subscription", Input{Now: t0, Step: Step{Role: RoleEngineering, Priority: Urgent}, Conns: []Conn{meter("t", 0, 0), sub("b", "codex", 0.9)}, Preference: Preference{Provider: "tensorx"}}, true, "b", ReasonHeadroom}, |
| + // Rule 3: the subscription with the most room. |
| + {"reported before unreported", in(Step{}, sub("a", "claude"), sub("b", "codex", 0.7)), true, "b", ReasonHeadroom}, |
| + {"lowest largest used", in(Step{}, sub("a", "claude", 0.1, 0.5), sub("b", "codex", 0.4, 0.4)), true, "b", ReasonHeadroom}, |
| + {"then the first window", in(Step{}, sub("b", "claude", 0.3, 0.5), sub("a", "codex", 0.5, 0.2)), true, "b", ReasonHeadroom}, |
| + {"then the id", in(Step{}, sub("b", "claude", 0.5), sub("a", "codex", 0.5)), true, "a", ReasonHeadroom}, |
| + {"a subscription before metered", in(Step{Priority: Urgent}, meter("t", 0, 0), sub("b", "codex", 0.95)), true, "b", ReasonHeadroom}, |
| + // Rule 4: metered. |
| + {"metered only with urgent", in(Step{Priority: Urgent}, meter("t", 0, 0)), true, "t", ReasonMeteredUrgent}, |
| + {"metered with a release", in(Step{Release: "r1"}, meter("t", 0, 0)), true, "t", ReasonMeteredRelease}, |
| + {"metered never for normal", in(Step{}, meter("t", 0, 0)), false, "", ReasonNoConnection}, |
| + {"metered never for low", in(Step{Priority: Low}, meter("t", 0, 0)), false, "", ReasonNoConnection}, |
| + {"metered most room", in(Step{Priority: Urgent}, meter("t2", 500_000, 0), meter("t1", 0, 6_000_000)), true, "t2", ReasonMeteredUrgent}, |
| + {"metered tie by id", in(Step{Priority: Urgent}, meter("t2", 0, 0), meter("t1", 0, 0)), true, "t1", ReasonMeteredUrgent}, |
| + {"metered at the day budget holds", in(Step{Priority: Urgent}, meter("t", 1_000_000, 0)), false, "t", ReasonBudget}, |
| + {"metered at the month budget holds", in(Step{Priority: Urgent}, meter("t", 0, 10_000_000)), false, "t", ReasonBudget}, |
| + {"metered spend unknown holds", in(Step{Priority: Urgent}, unknown), false, "t-unk", ReasonSpendUnknown}, |
| + {"metered busy holds", in(Step{Priority: Urgent}, busy), false, "t-busy", ReasonBusy}, |
| + {"a subscription over threshold, metered with leave", in(Step{Release: "r"}, sub("a", "claude", 1.0), meter("t", 0, 0)), true, "t", ReasonMeteredRelease}, |
| + // Rule 5: the hold names what it waits for. |
| + {"nothing at all", in(Step{}), false, "", ReasonNoConnection}, |
| + {"headroom names the first reset", in(Step{}, sub("a", "claude", 0.9, 0.9), sub("b", "codex", 0.95)), false, "b", ReasonHeadroom}, |
| + } |
| + for _, c := range cases { |
| + t.Run(c.name, func(t *testing.T) { |
| + d := Decide(c.in) |
| + if d.Run != c.run || d.Connection != c.conn || d.Reason != c.reason { |
| + t.Fatalf("Decide = run %v %q %q, want run %v %q %q", d.Run, d.Connection, d.Reason, c.run, c.conn, c.reason) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// TestDecideReviewFamily: Review prefers another family, then another |
| +// account, then Engineering's own; a release never overrides the order. |
| +func TestDecideReviewFamily(t *testing.T) { |
| + eng := &Engineer{ID: "a", Family: "claude", Account: "a"} |
| + review := func(extra Step, cs ...Conn) Decision { |
| + extra.Role, extra.Engineer = RoleReview, eng |
| + return Decide(in(extra, cs...)) |
| + } |
| + if d := review(Step{}, sub("a", "claude", 0.1), sub("b", "codex", 0.5)); d.Connection != "b" { |
| + t.Fatalf("another family first: %+v", d) |
| + } |
| + if d := review(Step{}, sub("a", "claude", 0.1), sub("a2", "claude", 0.5)); d.Connection != "a2" { |
| + t.Fatalf("another account next: %+v", d) |
| + } |
| + if d := review(Step{}, sub("a", "claude", 0.1)); d.Connection != "a" { |
| + t.Fatalf("the same account last: %+v", d) |
| + } |
| + // Another family over threshold: the same family's other account |
| + // reviews now rather than waiting. |
| + if d := review(Step{}, sub("a", "claude", 0.1), sub("a2", "claude", 0.3), sub("b", "codex", 0.9)); d.Connection != "a2" { |
| + t.Fatalf("other family held: %+v", d) |
| + } |
| + // Metered of another family with a release, before Engineering's own |
| + // subscription. |
| + if d := review(Step{Release: "r"}, sub("a", "claude", 0.1), meter("t", 0, 0)); d.Connection != "t" || d.Reason != ReasonMeteredRelease { |
| + t.Fatalf("other family metered with a release: %+v", d) |
| + } |
| + if d := review(Step{}, sub("a", "claude", 0.1), meter("t", 0, 0)); d.Connection != "a" { |
| + t.Fatalf("no leave: Engineering's own: %+v", d) |
| + } |
| +} |
| + |
| +// TestDecideWakeUps: wake_at is the earliest time strictly after now |
| +// among the resets, the budget boundaries, the preference's end and the |
| +// deadline check; null when none. |
| +func TestDecideWakeUps(t *testing.T) { |
| + a := sub("a", "claude", 0.9) // resets in 1h |
| + lim := sub("l", "codex") |
| + lim.State, lim.LimitedUntil = StateLimited, t0.Add(30*time.Minute) |
| + d := Decide(in(Step{}, a, lim)) |
| + if d.Run || d.WakeAt == nil || !d.WakeAt.Equal(t0.Add(30*time.Minute)) { |
| + t.Fatalf("a limited one's reset: %+v", d) |
| + } |
| + // The preference's end comes first. |
| + i := in(Step{}, a) |
| + i.Preference = Preference{Provider: "claude", Until: at(10 * time.Minute)} |
| + if d := Decide(i); d.WakeAt == nil || !d.WakeAt.Equal(t0.Add(10*time.Minute)) { |
| + t.Fatalf("the preference's end: %+v", d) |
| + } |
| + // The deadline check, unless it fired already. |
| + by := t0.Add(24*time.Hour + 5*time.Minute) |
| + if d := Decide(in(Step{By: &by}, a)); d.WakeAt == nil || !d.WakeAt.Equal(t0.Add(5*time.Minute)) { |
| + t.Fatalf("the deadline check: %+v", d) |
| + } |
| + if d := Decide(in(Step{By: &by, DeadlineNotified: true}, a)); !d.WakeAt.Equal(t0.Add(time.Hour)) { |
| + t.Fatalf("the deadline check fired already: %+v", d) |
| + } |
| + // The budget day boundary for a metered hold. |
| + if d := Decide(in(Step{Priority: Urgent}, meter("t", 1_000_000, 0))); d.WakeAt == nil || !d.WakeAt.Equal(time.Date(2026, 11, 13, 0, 0, 0, 0, time.UTC)) { |
| + t.Fatalf("the next budget day: %+v", d) |
| + } |
| + // Past times never wake: a reset that passed, a preference that ended. |
| + old := sub("o", "claude", 0.9) |
| + old.Windows[0].ResetsAt = at(-time.Hour) |
| + old.Windows = append(old.Windows, Window{Name: "w9", Used: 0.95}, Window{Name: "w8", Used: 0.95, ResetsAt: at(0)}) |
| + i = in(Step{}, old) |
| + i.Preference = Preference{Provider: "claude", Until: at(-time.Minute)} |
| + if d := Decide(i); d.Run || d.WakeAt != nil { |
| + t.Fatalf("no future wake-up: %+v", d) |
| + } |
| +} |
| + |
| +// TestDecideNeedsYou: the deadline check, no reset in sight and |
| +// nothing that can run it ask the person; a reset in time does not. |
| +func TestDecideNeedsYou(t *testing.T) { |
| + a := sub("a", "claude", 0.9) |
| + if d := Decide(in(Step{}, a)); d.NeedsPerson != "" { |
| + t.Fatalf("a reset in an hour asks nobody: %+v", d) |
| + } |
| + far := sub("f", "claude", 0.9) |
| + far.Windows[0].ResetsAt = at(73 * time.Hour) |
| + if d := Decide(in(Step{}, far)); d.NeedsPerson != NeedsNoReset { |
| + t.Fatalf("a reset 73 h away: %+v", d) |
| + } |
| + none := sub("n", "claude", 0.9) |
| + none.Windows[0].ResetsAt = nil |
| + if d := Decide(in(Step{}, none)); d.NeedsPerson != NeedsNoReset || d.WakeAt != nil { |
| + t.Fatalf("no reset reported: %+v", d) |
| + } |
| + if d := Decide(in(Step{})); d.NeedsPerson != NeedsNoConnection { |
| + t.Fatalf("nothing connected: %+v", d) |
| + } |
| + by := t0.Add(23 * time.Hour) |
| + if d := Decide(in(Step{By: &by}, a)); d.NeedsPerson != NeedsDeadline { |
| + t.Fatalf("the deadline check: %+v", d) |
| + } |
| + if d := Decide(in(Step{By: &by, DeadlineNotified: true}, a)); d.NeedsPerson != "" { |
| + t.Fatalf("the deadline check fired already: %+v", d) |
| + } |
| + busy := meter("t", 0, 0) |
| + busy.Busy = true |
| + if d := Decide(in(Step{Priority: Urgent}, busy)); d.NeedsPerson != "" { |
| + t.Fatalf("a busy slot ends by itself: %+v", d) |
| + } |
| + unknown := meter("t", 0, 0) |
| + unknown.SpendUnknown = true |
| + if d := Decide(in(Step{Priority: Urgent}, unknown)); d.NeedsPerson != NeedsNoReset { |
| + t.Fatalf("spend unknown waits for the person: %+v", d) |
| + } |
| +} |
| + |
| +// randomInput is a random Input over a small, adversarial domain: |
| +// thresholds' edges, past and future resets, every bar. |
| +func randomInput(r *rand.Rand) Input { |
| + pick := func(xs ...float64) float64 { return xs[r.IntN(len(xs))] } |
| + dur := func() *time.Time { |
| + switch r.IntN(4) { |
| + case 0: |
| + return nil |
| + case 1: |
| + return at(-time.Duration(r.IntN(120)) * time.Minute) |
| + } |
| + return at(time.Duration(r.IntN(100*60)) * time.Minute) |
| + } |
| + providers := []string{"claude", "codex", "tensorx"} |
| + var cs []Conn |
| + for i := range r.IntN(5) { |
| + p := providers[r.IntN(len(providers))] |
| + c := Conn{ID: fmt.Sprintf("c%d", i), Provider: p, Family: p, Account: fmt.Sprintf("acct%d", r.IntN(3)), Capable: r.IntN(6) > 0} |
| + c.State = []string{StateHealthy, StateHealthy, StateLimited, "sign_in_again", ""}[r.IntN(5)] |
| + if c.State == StateLimited { |
| + if t := dur(); t != nil { |
| + c.LimitedUntil = *t |
| + } |
| + } |
| + if p == "tensorx" { |
| + c.Billing = Metered |
| + c.Busy, c.SpendUnknown = r.IntN(4) == 0, r.IntN(5) == 0 |
| + c.BudgetDay, c.BudgetMonth = 1_000_000, 10_000_000 |
| + c.SpendDay = int64(pick(0, 500_000, 999_999, 1_000_000, 2_000_000)) |
| + c.SpendMonth = int64(pick(0, 9_999_999, 10_000_000)) |
| + } else { |
| + c.Billing = Subscription |
| + for j := range r.IntN(3) { |
| + c.Windows = append(c.Windows, Window{Name: fmt.Sprintf("w%d", j), Used: pick(0, 0.3, 0.59, 0.6, 0.61, 0.79, 0.8, 0.81, 0.99, 1.0), Minutes: int64(pick(0, 300, 10080)), ResetsAt: dur()}) |
| + } |
| + } |
| + cs = append(cs, c) |
| + } |
| + step := Step{Role: []string{RoleEngineering, RoleReview, RoleResearch}[r.IntN(3)], Priority: []string{"", Low, Normal, Urgent, "bogus"}[r.IntN(5)], DeadlineNotified: r.IntN(2) == 0} |
| + if r.IntN(3) == 0 { |
| + step.Release = "r1" |
| + } |
| + if r.IntN(2) == 0 { |
| + step.By = dur() |
| + } |
| + if len(cs) > 0 && r.IntN(3) == 0 { |
| + step.Kept = cs[r.IntN(len(cs))].ID |
| + } |
| + if len(cs) > 0 && r.IntN(3) == 0 { |
| + step.Avoid = []string{cs[r.IntN(len(cs))].ID} |
| + } |
| + if step.Role == RoleReview && len(cs) > 0 { |
| + e := cs[r.IntN(len(cs))] |
| + step.Engineer = &Engineer{ID: e.ID, Family: e.Family, Account: e.Account} |
| + } |
| + pref := Preference{Provider: []string{"", "claude", "codex", "tensorx"}[r.IntN(4)]} |
| + if r.IntN(2) == 0 { |
| + pref.Until = dur() |
| + } |
| + return Input{Version: Version, Now: t0, Step: step, Conns: cs, Preference: pref} |
| +} |
| + |
| +func connOf(i Input, id string) (Conn, bool) { |
| + for _, c := range i.Conns { |
| + if c.ID == id { |
| + return c, true |
| + } |
| + } |
| + return Conn{}, false |
| +} |
| + |
| +// someAdmits reports whether any candidate passes every bar and its |
| +// threshold: rules 1 to 4 would yield it. |
| +func someAdmits(i Input) bool { |
| + for _, c := range i.Conns { |
| + if !eligible(c, i.Step, i.Now) { |
| + continue |
| + } |
| + if c.Billing == Metered { |
| + if leave(i.Step) && spendBar(c) == "" { |
| + return true |
| + } |
| + continue |
| + } |
| + if Admits(c, Effective(i.Step), i.Now) || c.ID == i.Step.Kept { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
| + |
| +// TestDecideInvariants are §7's invariants (a) to (f), and wake-ups |
| +// strictly in the future, over many random inputs. |
| +func TestDecideInvariants(t *testing.T) { |
| + r := rand.New(rand.NewPCG(7, 13)) |
| + runs, holds := 0, 0 |
| + for n := range 20000 { |
| + i := randomInput(r) |
| + before := fmt.Sprintf("%#v", i) |
| + d := Decide(i) |
| + if fmt.Sprintf("%#v", i) != before { |
| + t.Fatalf("#%d: Decide changed its input", n) |
| + } |
| + // (f) the same input, the same output. |
| + if again := Decide(i); !reflect.DeepEqual(again, d) { |
| + t.Fatalf("#%d: not deterministic: %+v then %+v", n, d, again) |
| + } |
| + if d.WakeAt != nil && !d.WakeAt.After(i.Now) { |
| + t.Fatalf("#%d: a wake-up not after now: %v", n, d.WakeAt) |
| + } |
| + if !d.Run { |
| + holds++ |
| + // (e) a hold only when rules 1 to 4 all fail. A kept metered |
| + // session held by its spending bar is rule 1's hold. |
| + if k, ok := connOf(i, i.Step.Kept); ok && k.Billing == Metered && eligible(k, i.Step, i.Now) && leave(i.Step) && spendBar(k) != "" { |
| + continue |
| + } |
| + if someAdmits(i) { |
| + t.Fatalf("#%d: held though a candidate admits: %+v\n%#v", n, d, i) |
| + } |
| + continue |
| + } |
| + runs++ |
| + c, ok := connOf(i, d.Connection) |
| + if !ok { |
| + t.Fatalf("#%d: ran on an unknown connection %q", n, d.Connection) |
| + } |
| + if !eligible(c, i.Step, i.Now) { |
| + t.Fatalf("#%d: a bar passed: %+v on %#v", n, d, c) |
| + } |
| + if c.Billing == Metered { |
| + // (b) no metered admission without urgent or a release. |
| + if i.Step.Priority != Urgent && i.Step.Release == "" { |
| + t.Fatalf("#%d: metered without leave: %+v", n, d) |
| + } |
| + // (c) never two attempts on one slot; (d) never at the budget |
| + // or with the spend unknown. |
| + if c.Busy || c.SpendUnknown || c.SpendDay >= c.BudgetDay || c.SpendMonth >= c.BudgetMonth { |
| + t.Fatalf("#%d: metered admitted past its guard: %+v on %#v", n, d, c) |
| + } |
| + continue |
| + } |
| + // (a) no subscription admission at or above t(effective), except a |
| + // kept session. |
| + if d.Reason != ReasonSticky { |
| + for _, w := range c.Windows { |
| + if (w.ResetsAt == nil || i.Now.Before(*w.ResetsAt)) && w.Used >= Threshold(Effective(i.Step)) { |
| + t.Fatalf("#%d: admitted at %.2f, threshold %.2f: %+v", n, w.Used, Threshold(Effective(i.Step)), d) |
| + } |
| + } |
| + } else if c.ID != i.Step.Kept { |
| + t.Fatalf("#%d: sticky on a connection that is not kept: %+v", n, d) |
| + } |
| + } |
| + if runs < 1000 || holds < 1000 { |
| + t.Fatalf("the generator is lopsided: %d runs, %d holds", runs, holds) |
| + } |
| +} |
| + |
| +// TestDecideOrderFree: the order the connections are listed in never |
| +// changes the decision (the precedence is total). |
| +func TestDecideOrderFree(t *testing.T) { |
| + r := rand.New(rand.NewPCG(3, 5)) |
| + for n := range 5000 { |
| + i := randomInput(r) |
| + d := Decide(i) |
| + j := i |
| + j.Conns = slices.Clone(i.Conns) |
| + r.Shuffle(len(j.Conns), func(a, b int) { j.Conns[a], j.Conns[b] = j.Conns[b], j.Conns[a] }) |
| + if e := Decide(j); !reflect.DeepEqual(e, d) { |
| + t.Fatalf("#%d: order changed the decision: %+v then %+v", n, d, e) |
| + } |
| + } |
| +} |