|
| 1 | +// Copyright 2026 The go-python Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package py |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestStringSearchBounds(t *testing.T) { |
| 13 | + // Expected results were checked against CPython. |
| 14 | + for _, tc := range []struct { |
| 15 | + s, sub string |
| 16 | + start, end, find, count int |
| 17 | + }{ |
| 18 | + {"abcabc", "a", -3, 6, 3, 1}, |
| 19 | + {"abcabc", "a", 0, -1, 0, 2}, |
| 20 | + {"abcabc", "c", 0, -1, 2, 1}, |
| 21 | + {"abcabc", "a", -100, 100, 0, 2}, |
| 22 | + {"abcabc", "a", 0, -100, -1, 0}, |
| 23 | + {"abcabc", "", 7, 100, -1, 0}, |
| 24 | + {"abcabc", "", 4, 2, -1, 0}, |
| 25 | + {"abcabc", "", 6, 6, 6, 1}, |
| 26 | + {"abcabc", "", 0, -1, 0, 6}, |
| 27 | + {"é世界é", "é", -1, 4, 3, 1}, |
| 28 | + {"é世界é", "界", 0, -1, 2, 1}, |
| 29 | + {"é世界é", "", -3, -1, 1, 3}, |
| 30 | + {"", "", 0, 0, 0, 1}, |
| 31 | + {"", "", 1, 1, -1, 0}, |
| 32 | + } { |
| 33 | + for _, method := range []string{"find", "count"} { |
| 34 | + t.Run(fmt.Sprintf("%s/%q/%q/%d:%d", method, tc.s, tc.sub, tc.start, tc.end), func(t *testing.T) { |
| 35 | + defer func() { |
| 36 | + if p := recover(); p != nil { |
| 37 | + t.Errorf("unexpected panic: %v", p) |
| 38 | + } |
| 39 | + }() |
| 40 | + args := Tuple{String(tc.sub), Int(tc.start), Int(tc.end)} |
| 41 | + var got Object |
| 42 | + var err error |
| 43 | + want := tc.find |
| 44 | + if method == "find" { |
| 45 | + got, err = String(tc.s).find(args) |
| 46 | + } else { |
| 47 | + got, err = String(tc.s).Count(args) |
| 48 | + want = tc.count |
| 49 | + } |
| 50 | + if err != nil || got != Int(want) { |
| 51 | + t.Errorf("got %v, %v; want %d", got, err, want) |
| 52 | + } |
| 53 | + }) |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments