segmentTestCheck returns an error if s is incorrectly sorted, does not contain exactly expectedSegments segments, or contains a segment which fails the passed check. This should be used only for testing, and has been added to this package for templating convenience.
(expectedSegments int, segFunc func(int, Range, Value) error)
| 2117 | // This should be used only for testing, and has been added to this package for |
| 2118 | // templating convenience. |
| 2119 | func (s *Set) segmentTestCheck(expectedSegments int, segFunc func(int, Range, Value) error) error { |
| 2120 | havePrev := false |
| 2121 | prev := Key(0) |
| 2122 | nrSegments := 0 |
| 2123 | for seg := s.FirstSegment(); seg.Ok(); seg = seg.NextSegment() { |
| 2124 | next := seg.Start() |
| 2125 | if havePrev && prev >= next { |
| 2126 | return fmt.Errorf("incorrect order: key %d (segment %d) >= key %d (segment %d)", prev, nrSegments-1, next, nrSegments) |
| 2127 | } |
| 2128 | if segFunc != nil { |
| 2129 | if err := segFunc(nrSegments, seg.Range(), seg.Value()); err != nil { |
| 2130 | return err |
| 2131 | } |
| 2132 | } |
| 2133 | prev = next |
| 2134 | havePrev = true |
| 2135 | nrSegments++ |
| 2136 | } |
| 2137 | if nrSegments != expectedSegments { |
| 2138 | return fmt.Errorf("incorrect number of segments: got %d, wanted %d", nrSegments, expectedSegments) |
| 2139 | } |
| 2140 | return nil |
| 2141 | } |
| 2142 | |
| 2143 | // countSegments counts the number of segments in the set. |
| 2144 | // |