Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: 91b5b2e38648d0e230910a166039a667da46a755 (plain)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?xml version="1.0"?>
<!--
  Copyright (C) 2015 R-T Specialty, LLC.

  This file is part of tame-core.

  tame-core is free software: you can redistribute it and/or modify it
  under the terms of the GNU General Public License as
  published by the Free Software Foundation, either version 3 of the
  License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-->
<package xmlns="http://www.lovullo.com/rater"
  xmlns:c="http://www.lovullo.com/calc"
  xmlns:t="http://www.lovullo.com/rater/apply-template"
  desc="Interpolation specs">

  <import package="../../../base" />
  <import package="../../../numeric/common" />
  <import package="../../../numeric/minmax" />
  <import package="../../../numeric/round" />
  <import package="../../../vector/arithmetic" />

  <import package="../../spec" />
  <import package="../../../vector/table" />
  <import package="../../../vector/interpolate" />


  <const name="INTERP_TABLE_STEP"
         value="100" type="integer"
         desc="Test table step" />

  <!-- table containing test data -->
  <t:create-table name="interp-query-field-test"
                  desc="Test data for query interpolation">
    <t:table-column name="key"
                    index="0"
                    desc="Lookup value" />
    <t:table-column name="pred"
                    index="1"
                    desc="Random value for predicate testing" />
    <t:table-column name="value"
                    index="2"
                    desc="Value to interpolate" />

    <t:table-rows>
      <t:table-row>
        <t:table-value const="100" />
        <t:table-value const="10" />
        <t:table-value const="1" />
      </t:table-row>

      <t:table-row>
        <t:table-value const="200" />
        <t:table-value const="20" />
        <t:table-value const="2" />
      </t:table-row>

      <!-- two values for predicate testing -->
      <t:table-row>
        <t:table-value const="300" />
        <t:table-value const="30" />
        <t:table-value const="3" />
      </t:table-row>
      <t:table-row>
        <t:table-value const="300" />
        <t:table-value const="31" />
        <t:table-value const="3.5" />
      </t:table-row>

      <t:table-row>
        <t:table-value const="400" />
        <t:table-value const="40" />
        <t:table-value const="4" />
      </t:table-row>
      <t:table-row>
        <t:table-value const="400" />
        <t:table-value const="31" />
        <t:table-value const="4" />
      </t:table-row>
    </t:table-rows>
  </t:create-table>


  <const name="INTERP_TABLE_SM_STEP"
         value="15" type="integer"
         desc="Small-step test table step" />

  <!-- test another step (we didn't hardcode 100, did we)? -->
  <t:create-table name="interp-query-field-test-sm"
                  desc="Test data for query interpolation; sm step">
    <t:table-column name="key"
                    index="0"
                    desc="Lookup value" />
    <t:table-column name="value"
                    index="1"
                    desc="Value to interpolate" />

    <t:table-rows>
      <t:table-row>
        <t:table-value const="15" />
        <t:table-value const="0" />
      </t:table-row>

      <t:table-row>
        <t:table-value const="30" />
        <t:table-value const="10" />
      </t:table-row>
    </t:table-rows>
  </t:create-table>


  <!-- by avoiding hardcoding these, we make these tests less fragile
       (if the test data were to change) -->
  <section title="Common inferable table values">
    <rate yields="interpTableMaxKeyValue">
      <t:maxreduce isvector="true">
        <t:query-field table="interp-query-field-test"
                       field="key" />
      </t:maxreduce>
    </rate>

    <rate yields="interpTableMaxFieldValue">
      <t:query-first-field table="interp-query-field-test"
                           field="value">
        <t:when field="key">
          <c:value-of name="interpTableMaxKeyValue" />
        </t:when>
      </t:query-first-field>
    </rate>
  </section>



  <t:describe name="_interpolate-query-field_">
    <t:describe name="exact value query">
      <!-- a simple expression with no body performs basic interpolation
           based on a query result...just like that! -->
      <t:it desc="succeeds without predicates">
        <t:given>
          <t:interpolate-query-field table="interp-query-field-test"
                                     field="value"
                                     key="key"
                                     step="INTERP_TABLE_STEP"
                                     actual="#100" />
        </t:given>

        <t:expect>
          <t:match-result>
            <c:eq>
              <c:value-of name="#1" />
            </c:eq>
          </t:match-result>
        </t:expect>
      </t:it>


      <!-- sanity check -->
      <t:it desc="succeeds with predicates">
        <t:given>
          <t:interpolate-query-field table="interp-query-field-test"
                                     field="value"
                                     key="key"
                                     step="INTERP_TABLE_STEP"
                                     actual="#300">
            <t:when field="pred">
              <c:const value="31" type="float"
                       desc="Test predicate value" />
            </t:when>
          </t:interpolate-query-field>
        </t:given>

        <t:expect>
          <t:match-result>
            <c:eq>
              <c:value-of name="#3.5" />
            </c:eq>
          </t:match-result>
        </t:expect>
      </t:it>
    </t:describe>


    <t:describe name="inexact value query">
      <t:it desc="performs interpolation on query field">
        <!-- take that you f@%*#^! PITA algorithms -->
        <t:given>
          <t:interpolate-query-field table="interp-query-field-test"
                                     field="value"
                                     key="key"
                                     step="INTERP_TABLE_STEP"
                                     actual="#150" />
        </t:given>

        <t:expect>
          <t:match-result>
            <c:eq>
              <!-- this value does not exist in the table; it is
                   interpolated, just like real magic -->
              <c:value-of name="#1.5" />
            </c:eq>
          </t:match-result>
        </t:expect>
      </t:it>


      <!-- take note that the predicate applies to *both* rows
           returned by the query -->
      <t:it desc="performs interpolation considering predicates">
        <t:given>
          <t:interpolate-query-field table="interp-query-field-test"
                                     field="value"
                                     key="key"
                                     step="INTERP_TABLE_STEP"
                                     actual="#350">
            <t:when field="pred">
              <c:const value="31" type="float"
                       desc="Test predicate value" />
            </t:when>
          </t:interpolate-query-field>
        </t:given>

        <t:expect>
          <t:match-result>
            <c:eq>
              <!-- note that this interpolates between 3.5 and 4 -->
              <c:value-of name="#3.75" />
            </c:eq>
          </t:match-result>
        </t:expect>
      </t:it>
    </t:describe>


    <!-- ensure that the step is indeed configurable -->
    <t:it desc="permits arbitrary step">
      <t:given>
        <t:interpolate-query-field table="interp-query-field-test-sm"
                                   field="value"
                                   key="key"
                                   step="INTERP_TABLE_SM_STEP"
                                   actual="#22.5" />
      </t:given>

      <t:expect>
        <t:match-result>
          <c:eq>
            <c:value-of name="#5" />
          </c:eq>
        </t:match-result>
      </t:expect>
    </t:it>


    <t:describe name="with actual exceeding maximum">
      <t:describe name="with no step factor">
        <t:it desc="yields value of maximum key">
          <t:given>
            <t:interpolate-query-field table="interp-query-field-test"
                                       field="value"
                                       key="key"
                                       step="INTERP_TABLE_STEP"
                                       table_max="interpTableMaxKeyValue"
                                       actual="#10000" />
          </t:given>

          <t:expect>
            <t:match-result>
              <c:eq>
                <c:value-of name="interpTableMaxFieldValue" />
              </c:eq>
            </t:match-result>
          </t:expect>
        </t:it>
      </t:describe>


      <!-- we can infer values past the maximum -->
      <!-- TODO: these are fragile, because we'd otherwise be
           duplicating a lot of logic; let's factor some common logic
           out of _interpolate-query-field_ -->
      <t:describe name="with step factor">
        <t:it desc="applies arbitrary factor linearly to difference">
          <t:given>
            <!-- note that our step factor differs from the previous
                 field value step in the table -->
            <t:interpolate-query-field table="interp-query-field-test"
                                       field="value"
                                       key="key"
                                       step="INTERP_TABLE_STEP"
                                       step_factor="#2"
                                       table_max="interpTableMaxKeyValue"
                                       actual="#600" />
          </t:given>

          <t:expect>
            <t:match-result>
              <c:eq>
                <c:value-of name="#8" />
              </c:eq>
            </t:match-result>
          </t:expect>
        </t:it>


        <!-- half-step relative to above expectation -->
        <t:it desc="interpolates linear factor">
          <t:given>
            <t:interpolate-query-field table="interp-query-field-test"
                                       field="value"
                                       key="key"
                                       step="INTERP_TABLE_STEP"
                                       step_factor="#2"
                                       table_max="interpTableMaxKeyValue"
                                       actual="#650" />
          </t:given>

          <t:expect>
            <t:match-result>
              <c:eq>
                <c:value-of name="#9" />
              </c:eq>
            </t:match-result>
          </t:expect>
        </t:it>
      </t:describe>
    </t:describe>
  </t:describe>
</package>