~jaro/balkon

ref: segfault-debug balkon/test/Data/Text/ParagraphLayout/Internal/LinePaginationSpec.hs -rw-r--r-- 9.6 KiB
f72b5805Jaro Add stress test for Heisenbug hunting. 1 year, 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
module Data.Text.ParagraphLayout.Internal.LinePaginationSpec (spec) where

import Control.Monad (forM_)
import Data.Int (Int32)

import Test.Hspec
import Data.Text.ParagraphLayout.Internal.LinePagination

emptyLines :: [Int32]
emptyLines = []

-- TODO: For rich text, add tests with unequal line heights.
tenLines :: [Int32]
tenLines = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]

spec :: Spec
spec = do

    -- Lower level function.
    -- Must prevent overflow and meet orphan/widow constraints at all times.
    describe "bestSplit" $ do

        describe "emptyLines, orphans = 1, widows = 1" $ do
            let ls = emptyLines
            let page = bestSplit 1 1

            ([-30, -5, 0, 5, 30, 90, 100, 110] :: [Int32]) `forM_` \ h ->
                it ("maxHeight = " ++ show h ++ " returns empty lists") $
                    page h ls `shouldBe`
                        ([], [])

        describe "tenLines, orphans = 1, widows = 1" $ do
            let ls = tenLines
            let page = bestSplit 1 1

            ([-30, -5, 0, 5] :: [Int32]) `forM_` \ h ->
                it ("maxHeight = " ++ show h ++ " puts all in second list") $
                    page h ls `shouldBe`
                        ([], [10, 10, 10, 10, 10, 10, 10, 10, 10, 10])

            ([30, 35] :: [Int32]) `forM_` \ h ->
                it ("maxHeight = " ++ show h ++ " puts 3 in first list") $
                    page h ls `shouldBe`
                        ([10, 10, 10], [10, 10, 10, 10, 10, 10, 10])

            ([100, 110] :: [Int32]) `forM_` \ h ->
                it ("maxHeight = " ++ show h ++ " puts all in first list") $
                    page h ls `shouldBe`
                        ([10, 10, 10, 10, 10, 10, 10, 10, 10, 10], [])

        describe "tenLines, orphans = 3, widows = 4" $ do
            let ls = tenLines
            let page = bestSplit 3 4
            -- Acceptable page breaks:
            --  -  0 + 10
            --  -  3 +  7
            --  -  4 +  6
            --  -  5 +  5
            --  -  6 +  4
            --  - 10 +  0

            ([0, 10, 15, 25] :: [Int32]) `forM_` \ h ->
                it ("maxHeight = " ++ show h ++ " ensures 3 orphans") $
                    page h ls `shouldBe`
                        ([], [10, 10, 10, 10, 10, 10, 10, 10, 10, 10])

            ([30, 35] :: [Int32]) `forM_` \ h ->
                it ("maxHeight = " ++ show h ++ " puts 3 in first list") $
                    page h ls `shouldBe`
                        ([10, 10, 10], [10, 10, 10, 10, 10, 10, 10])

            ([40, 45] :: [Int32]) `forM_` \ h ->
                it ("maxHeight = " ++ show h ++ " puts 4 in first list") $
                    page h ls `shouldBe`
                        ([10, 10, 10, 10], [10, 10, 10, 10, 10, 10])

            ([60, 75, 90] :: [Int32]) `forM_` \ h ->
                it ("maxHeight = " ++ show h ++ " ensures 4 widows") $
                    page h ls `shouldBe`
                        ([10, 10, 10, 10, 10, 10], [10, 10, 10, 10])

            ([100, 110] :: [Int32]) `forM_` \ h ->
                it ("maxHeight = " ++ show h ++ " puts all in first list") $
                    page h ls `shouldBe`
                        ([10, 10, 10, 10, 10, 10, 10, 10, 10, 10], [])

        describe "tenLines, orphans = 6, widows = 4" $ do
            let ls = tenLines
            let page = bestSplit 6 4
            -- Acceptable page breaks:
            --  -  0 + 10
            --  -  6 +  4
            --  - 10 +  0

            ([0, 10, 15, 35, 50, 55] :: [Int32]) `forM_` \ h ->
                it ("maxHeight = " ++ show h ++ " ensures 6 orphans") $
                    page h ls `shouldBe`
                        ([], [10, 10, 10, 10, 10, 10, 10, 10, 10, 10])

            ([60, 65, 85, 95] :: [Int32]) `forM_` \ h ->
                it ("maxHeight = " ++ show h ++ " puts 6 in first list") $
                    page h ls `shouldBe`
                        ([10, 10, 10, 10, 10, 10], [10, 10, 10, 10])

            ([100, 110] :: [Int32]) `forM_` \ h ->
                it ("maxHeight = " ++ show h ++ " puts all in first list") $
                    page h ls `shouldBe`
                        ([10, 10, 10, 10, 10, 10, 10, 10, 10, 10], [])

        describe "tenLines, orphans = 6, widows = 5" $ do
            let ls = tenLines
            let page = bestSplit 6 5
            -- Acceptable page breaks:
            --  -  0 + 10
            --  - 10 +  0

            ([0, 10, 60, 65, 85, 95] :: [Int32]) `forM_` \ h ->
                it ("maxHeight = " ++ show h ++ " puts all in second list") $
                    page h ls `shouldBe`
                        ([], [10, 10, 10, 10, 10, 10, 10, 10, 10, 10])

            ([100, 110] :: [Int32]) `forM_` \ h ->
                it ("maxHeight = " ++ show h ++ " puts all in first list") $
                    page h ls `shouldBe`
                        ([10, 10, 10, 10, 10, 10, 10, 10, 10, 10], [])

    -- Higher level function.
    -- Must return a non-empty prefix if input was non-empty.
    -- May only break orphan/widow constraints or overflow if unavoidable.
    describe "paginateLines" $ do

        describe "emptyLines, orphans = 1, widows = 1" $ do
            let ls = emptyLines
            let page = paginateLines 1 1

            it "continues page with no lines when space is zero" $
                page 0 0 ls `shouldBe`
                        (Continue, [], [])

            it "tolerates negative current page height" $
                page (-30) 0 ls `shouldBe`
                        (Continue, [], [])

            it "tolerates negative next page height" $
                page 0 (-30) ls `shouldBe`
                        (Continue, [], [])

            it "continues page with no lines when space is equal" $
                page 50 50 ls `shouldBe`
                        (Continue, [], [])

            it "continues page with no lines when next page has more space" $
                page 50 100 ls `shouldBe`
                        (Continue, [], [])

        describe "tenLines, orphans = 1, widows = 1" $ do
            let ls = tenLines
            let page = paginateLines 1 1

            it "puts all lines on current page if possible" $
                page 200 200 ls `shouldBe`
                    (Continue, [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], [])

            it "puts as many lines on current page as possible" $
                page 60 200 ls `shouldBe`
                    (Continue, [10, 10, 10, 10, 10, 10], [10, 10, 10, 10])

            it "starts at next page if not enough room" $
                page 5 200 ls `shouldBe`
                    (Break, [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], [])

            it "starts at next page and handles breaking there" $
                page 5 70 ls `shouldBe`
                    (Break, [10, 10, 10, 10, 10, 10, 10], [10, 10, 10])

            it "overflows on current page" $
                page 5 5 ls `shouldBe`
                    (Continue, [10], [10, 10, 10, 10, 10, 10, 10, 10, 10])

            it "overflows on next page if it has more room" $
                page 5 6 ls `shouldBe`
                    (Break, [10], [10, 10, 10, 10, 10, 10, 10, 10, 10])

            it "tolerates negative current page height" $
                page (-30) 0 ls `shouldBe`
                    (Break, [10], [10, 10, 10, 10, 10, 10, 10, 10, 10])

            it "tolerates negative next page height" $
                page 0 (-30) ls `shouldBe`
                    (Continue, [10], [10, 10, 10, 10, 10, 10, 10, 10, 10])

        describe "tenLines, orphans = 5, widows = 3" $ do
            let ls = tenLines
            let page = paginateLines 5 3
            -- Acceptable page breaks:
            --  -  0 + 10
            --  -  5 +  5
            --  -  6 +  4
            --  -  7 +  3
            --  - 10 +  0

            it "puts all lines on current page if possible" $
                page 200 200 ls `shouldBe`
                    (Continue, [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], [])

            it "puts as many lines on current page as possible" $
                page 60 200 ls `shouldBe`
                    (Continue, [10, 10, 10, 10, 10, 10], [10, 10, 10, 10])

            it "starts at next page if not enough room" $
                page 5 200 ls `shouldBe`
                    (Break, [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], [])

            it "starts at next page and handles breaking there" $
                page 5 70 ls `shouldBe`
                    (Break, [10, 10, 10, 10, 10, 10, 10], [10, 10, 10])

            it "overflows on current page" $
                page 5 5 ls `shouldBe`
                    (Continue, [10], [10, 10, 10, 10, 10, 10, 10, 10, 10])

            it "overflows on next page if it has more room" $
                page 5 6 ls `shouldBe`
                    (Break, [10], [10, 10, 10, 10, 10, 10, 10, 10, 10])

            -- Behaviour affected by orphans/widows:

            it "breaks early to meet widows constraint" $
                page 80 200 ls `shouldBe`
                    (Continue, [10, 10, 10, 10, 10, 10, 10], [10, 10, 10])

            it "breaks at start to meet orphans constraint" $
                page 45 200 ls `shouldBe`
                    (Break, [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], [])

            it "starts at next page and meets widows constraint there" $
                page 5 80 ls `shouldBe`
                    (Break, [10, 10, 10, 10, 10, 10, 10], [10, 10, 10])

            it "continues page and violates impossible constraints" $
                page 45 46 ls `shouldBe`
                    (Continue, [10, 10, 10, 10], [10, 10, 10, 10, 10, 10])

            it "breaks page and violates impossible constraints" $
                page 5 36 ls `shouldBe`
                    (Break, [10, 10, 10], [10, 10, 10, 10, 10, 10, 10])