• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MeltyPlayer / MeltyTool / 17574188774

09 Sep 2025 06:45AM UTC coverage: 39.98% (-2.3%) from 42.309%
17574188774

push

github

MeltyPlayer
Okay, hopefully fixed the models now???

5774 of 16361 branches covered (35.29%)

Branch coverage included in aggregate %.

2 of 2 new or added lines in 1 file covered. (100.0%)

935 existing lines in 47 files now uncovered.

24391 of 59090 relevant lines covered (41.28%)

74782.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.31
/FinModelUtility/Fin/Fin/src/language/equations/fixedFunction/impl/FixedFunctionOps.cs
1
using fin.language.equations.fixedFunction.util;
2

3
namespace fin.language.equations.fixedFunction.impl;
4

5
// TODO: Merge this back into the main value type logic
6
public interface IFixedFunctionOps<TValue, out TConstant>
7
    where TValue : IValue<TValue>
8
    where TConstant : IConstant<TValue>, TValue {
9
  TConstant Zero { get; }
10
  TConstant Half { get; }
11
  TConstant One { get; }
12

13
  TValue Add(TValue lhs, TValue rhs);
14
  TValue AddWithScalar(TValue lhs, IScalarValue rhs);
15
  TValue Subtract(TValue lhs, TValue rhs);
16
  TValue Multiply(TValue lhs, TValue rhs);
17
  TValue MultiplyWithScalar(TValue lhs, IScalarValue rhs);
18

19
  TValue AddWithConstant(TValue lhs, float constant);
20
  TValue MultiplyWithConstant(TValue lhs, float constant);
21

22
  TValue MixWithConstant(TValue lhs, TValue rhs, float mixAmount);
23
  TValue MixWithScalar(TValue lhs, TValue rhs, IScalarValue mixAmount);
24
}
25

26
public interface IColorOps : IFixedFunctionOps<IColorValue, IColorConstant>;
27

28
public interface IScalarOps : IFixedFunctionOps<IScalarValue, IScalarConstant>;
29

30
public abstract class BFixedFunctionOps<TValue, TConstant>
31
    : IFixedFunctionOps<TValue, TConstant>
32
    where TValue : IValue<TValue>
33
    where TConstant : IConstant<TValue>, TValue {
34
  public abstract TConstant Zero { get; }
35
  public abstract TConstant Half { get; }
36
  public abstract TConstant One { get; }
37

38
  public abstract TValue Add(TValue lhs, TValue rhs);
39
  public abstract TValue AddWithScalar(TValue lhs, IScalarValue rhs);
40

41
  public abstract TValue Subtract(TValue lhs, TValue rhs);
42

43
  public abstract TValue Multiply(TValue lhs, TValue rhs);
44
  public abstract TValue MultiplyWithScalar(TValue lhs, IScalarValue rhs);
45

46
  public TValue AddWithConstant(TValue lhs, float constant)
47
    => this.AddWithScalar(lhs, new ScalarConstant(constant));
×
48

49
  public TValue MultiplyWithConstant(TValue lhs, float constant)
50
    => this.MultiplyWithScalar(lhs, new ScalarConstant(constant));
1,504✔
51

52
  public TValue MixWithConstant(TValue lhs, TValue rhs, float mixAmount) {
248✔
53
    lhs = this.MultiplyWithConstant(lhs, 1 - mixAmount);
248✔
54
    rhs = this.MultiplyWithConstant(rhs, mixAmount);
248✔
55

56
    return this.Add(lhs, rhs);
248✔
57
  }
248✔
58

59
  public abstract TValue MixWithScalar(TValue lhs,
60
                                       TValue rhs,
61
                                       IScalarValue mixAmount);
62
}
63

64
public static class ColorWrapperExtensions {
65
  public static ColorWrapper Wrap(this IScalarValue scalarValue)
66
    => new(scalarValue);
9✔
67
}
68

69
public class ColorFixedFunctionOps<TIdentifier>(
1,258✔
70
    IFixedFunctionEquations<TIdentifier> equations)
1,258✔
71
    : BFixedFunctionOps<IColorValue, IColorConstant>, IColorOps
72
    where TIdentifier : notnull {
73
  private IScalarOps ScalarOps_ => equations.ScalarOps;
27✔
74

75
  private readonly IScalarConstant scMinusOne_
1,258✔
76
      = equations.CreateScalarConstant(-1);
1,258✔
77

78
  public override IColorConstant Zero { get; }
402✔
79
    = equations.CreateColorConstant(0);
1,258✔
80

UNCOV
81
  public override IColorConstant Half { get; }
×
82
    = equations.CreateColorConstant(.5f);
1,258✔
83

84
  public override IColorConstant One { get; }
606✔
85
    = equations.CreateColorConstant(1);
1,258✔
86

87
  public bool IsSingleScalarValue(
88
      IColorValue value,
89
      out IScalarValue scalarValue) {
9,333✔
90
    if (value is IColorConstant {
9,333✔
91
            Intensity: IScalarConstant scalarConstant
9,333✔
92
        }) {
11,484✔
93
      scalarValue = scalarConstant;
2,151✔
94
      return true;
2,151✔
95
    }
96

97
    if (value is ColorWrapper { Intensity: IScalarConstant wrappedScalar }) {
7,229✔
98
      scalarValue = wrappedScalar;
47✔
99
      return true;
47✔
100
    }
101

102
    scalarValue = default;
7,135✔
103
    return false;
7,135✔
104
  }
9,333✔
105

106
  public override IColorValue Add(IColorValue lhs, IColorValue rhs) {
3,905✔
107
    if (!FixedFunctionConstants.SIMPLIFY) {
3,905✔
108
      lhs ??= this.Zero;
109
      rhs ??= this.Zero;
110
    } else {
3,905✔
111
      if (this.IsSingleScalarValue(rhs, out var rhsScalar)) {
4,913✔
112
        return this.AddWithScalar(lhs, rhsScalar);
1,008✔
113
      }
114

115
      var lhsIsZero = lhs.IsZero();
2,897✔
116
      var rhsIsZero = rhs.IsZero();
2,897✔
117

118
      if (lhsIsZero && rhsIsZero) {
2,897!
UNCOV
119
        return ColorConstant.ZERO;
×
120
      }
121

122
      if (lhsIsZero) {
4,086✔
123
        return rhs;
1,189✔
124
      }
125

126
      if (rhsIsZero) {
1,708!
UNCOV
127
        return lhs;
×
128
      }
129
    }
1,708✔
130

131
    return lhs.Add(rhs);
1,708✔
132
  }
3,905✔
133

134
  public override IColorValue AddWithScalar(IColorValue lhs,
135
                                            IScalarValue rhs) {
1,403✔
136
    if (!FixedFunctionConstants.SIMPLIFY) {
1,403✔
137
      lhs ??= this.Zero;
138
      rhs ??= this.ScalarOps_.Zero;
139
    } else {
1,403✔
140
      var lhsIsZero = lhs.IsZero();
1,403✔
141
      var rhsIsZero = rhs.IsZero();
1,403✔
142

143
      if (lhsIsZero && rhsIsZero) {
1,427✔
144
        return ColorConstant.ZERO;
24✔
145
      }
146

147
      if (lhsIsZero) {
1,430✔
148
        return equations.CreateColor(rhs);
51✔
149
      }
150

151
      if (rhsIsZero) {
2,588✔
152
        return lhs;
1,260✔
153
      }
154

155
      if (this.IsSingleScalarValue(lhs, out var lhsScalar)) {
74✔
156
        return this.ScalarOps_.Add(lhsScalar, rhs).Wrap();
6✔
157
      }
158
    }
62✔
159

160
    return lhs.Add(rhs);
62✔
161
  }
1,403✔
162

163
  public override IColorValue Subtract(IColorValue lhs, IColorValue rhs) {
405✔
164
    if (!FixedFunctionConstants.SIMPLIFY) {
405✔
165
      lhs ??= this.Zero;
166
      rhs ??= this.Zero;
167
    } else {
405✔
168
      var lhsIsZero = lhs.IsZero();
405✔
169
      var rhsIsZero = rhs.IsZero();
405✔
170

171
      if ((lhsIsZero && rhsIsZero) || lhs.Equals(rhs)) {
414!
172
        return ColorConstant.ZERO;
9✔
173
      }
174

175
      if (lhsIsZero) {
396!
UNCOV
176
        return rhs.Multiply(this.scMinusOne_);
×
177
      }
178

179
      if (rhsIsZero) {
419✔
180
        return lhs;
23✔
181
      }
182
    }
373✔
183

184
    return lhs.Subtract(rhs);
373✔
185
  }
405✔
186

187
  public override IColorValue Multiply(IColorValue lhs, IColorValue rhs) {
4,252✔
188
    if (!FixedFunctionConstants.SIMPLIFY) {
4,252✔
189
      lhs ??= this.Zero;
190
      rhs ??= this.Zero;
191
    } else {
4,252✔
192
      if (this.IsSingleScalarValue(rhs, out var rhsScalar)) {
5,433✔
193
        return this.MultiplyWithScalar(lhs, rhsScalar);
1,181✔
194
      }
195

196
      if (lhs.IsZero() || rhs.IsZero()) {
3,426✔
197
        return ColorConstant.ZERO;
355✔
198
      }
199

200
      var lhsIsOne = lhs.IsOne();
2,716✔
201
      var rhsIsOne = rhs.IsOne();
2,716✔
202

203
      if (lhsIsOne && rhsIsOne) {
2,716!
UNCOV
204
        return ColorConstant.ONE;
×
205
      }
206

207
      if (lhsIsOne) {
2,924✔
208
        return rhs;
208✔
209
      }
210

211
      if (rhsIsOne) {
2,508!
UNCOV
212
        return lhs;
×
213
      }
214
    }
2,508✔
215

216
    return lhs.Multiply(rhs);
2,508✔
217
  }
4,252✔
218

219
  public override IColorValue MultiplyWithScalar(
220
      IColorValue lhs,
221
      IScalarValue rhs) {
3,023✔
222
    if (!FixedFunctionConstants.SIMPLIFY) {
3,023✔
223
      lhs ??= this.Zero;
224
      rhs ??= this.ScalarOps_.Zero;
225
    } else {
3,023✔
226
      if (lhs.IsZero() || rhs.IsZero()) {
3,915✔
227
        return ColorConstant.ZERO;
892✔
228
      }
229

230
      var lhsIsOne = lhs.IsOne();
2,131✔
231
      var rhsIsOne = rhs.IsOne();
2,131✔
232

233
      if (lhsIsOne && rhsIsOne) {
2,286✔
234
        return ColorConstant.ONE;
155✔
235
      }
236

237
      if (lhsIsOne) {
2,017✔
238
        return equations.CreateColor(rhs);
41✔
239
      }
240

241
      if (rhsIsOne) {
2,762✔
242
        return lhs;
827✔
243
      }
244

245
      if (this.IsSingleScalarValue(lhs, out var lhsScalar)) {
1,111✔
246
        return this.ScalarOps_.Multiply(lhsScalar, rhs).Wrap();
3✔
247
      }
248
    }
1,105✔
249

250
    return lhs.Multiply(rhs);
1,105✔
251
  }
3,023✔
252

253
  public override IColorValue MixWithScalar(IColorValue lhs,
254
                                            IColorValue rhs,
255
                                            IScalarValue mixAmount) {
9✔
256
    if (FixedFunctionConstants.SIMPLIFY) {
18✔
257
      if (lhs.IsZero()) {
9!
UNCOV
258
        lhs = ColorConstant.ZERO;
×
UNCOV
259
      }
×
260

261
      if (rhs.IsZero()) {
9!
262
        rhs = ColorConstant.ZERO;
×
UNCOV
263
      }
×
264

265
      if (lhs.IsZero() && rhs.IsZero()) {
9!
266
        return ColorConstant.ZERO;
×
267
      }
268

269
      // No progress, so return starting value
270
      if (mixAmount.IsZero()) {
9!
UNCOV
271
        return lhs;
×
272
      }
273

274
      // Fully progressed, return final value
275
      if (mixAmount.IsOne()) {
9!
UNCOV
276
        return rhs;
×
277
      }
278
    }
9✔
279

280
    // Some combination
281
    lhs = this.MultiplyWithScalar(
9✔
282
        lhs,
9✔
283
        this.ScalarOps_.Subtract(this.ScalarOps_.One, mixAmount));
9✔
284
    rhs = this.MultiplyWithScalar(rhs, mixAmount);
9✔
285

286
    return this.Add(lhs, rhs);
9✔
287
  }
9✔
288
}
289

290
public class ScalarFixedFunctionOps<TIdentifier>(
1,258✔
291
    IFixedFunctionEquations<TIdentifier> equations)
1,258✔
292
    : BFixedFunctionOps<IScalarValue, IScalarConstant>,
293
      IScalarOps
294
    where TIdentifier : notnull {
295
  private readonly IScalarValue
1,258✔
296
      scMinusOne_ = equations.CreateScalarConstant(-1);
1,258✔
297

UNCOV
298
  public override IScalarConstant Zero { get; }
×
299
    = equations.CreateScalarConstant(0);
1,258✔
300

301
  public override IScalarConstant Half { get; }
×
302
    = equations.CreateScalarConstant(.5f);
1,258✔
303

304
  public override IScalarConstant One { get; }
465✔
305
    = equations.CreateScalarConstant(1);
1,258✔
306

307
  public override IScalarValue AddWithScalar(
308
      IScalarValue lhs,
309
      IScalarValue rhs)
310
    => this.Add(lhs, rhs);
399✔
311

312
  public override IScalarValue Add(IScalarValue lhs, IScalarValue rhs) {
1,338✔
313
    if (!FixedFunctionConstants.SIMPLIFY) {
1,338✔
314
      lhs ??= this.Zero;
315
      rhs ??= this.Zero;
316
    } else {
1,338✔
317
      var lhsIsZero = lhs.IsZero();
1,338✔
318
      var rhsIsZero = rhs.IsZero();
1,338✔
319

320
      if (lhsIsZero && rhsIsZero) {
1,532✔
321
        return ScalarConstant.ZERO;
194✔
322
      }
323

324
      if (lhsIsZero) {
1,750✔
325
        return rhs;
606✔
326
      }
327

328
      if (rhsIsZero) {
1,045✔
329
        return lhs;
507✔
330
      }
331

332
      if (lhs.IsConstant(out var lhsConstant) &&
31✔
333
          rhs.IsConstant(out var rhsConstant)) {
41✔
334
        return new ScalarConstant(lhsConstant + rhsConstant);
10✔
335
      }
336
    }
21✔
337

338
    return lhs.Add(rhs);
21✔
339
  }
1,338✔
340

341
  public override IScalarValue Subtract(IScalarValue lhs,
342
                                        IScalarValue rhs) {
455✔
343
    if (!FixedFunctionConstants.SIMPLIFY) {
455✔
344
      lhs ??= this.Zero;
345
      rhs ??= this.Zero;
346
    } else {
455✔
347
      var lhsIsZero = lhs.IsZero();
455✔
348
      var rhsIsZero = rhs.IsZero();
455✔
349

350
      if ((lhsIsZero && rhsIsZero) || lhs.Equals(rhs)) {
456!
351
        return ScalarConstant.ZERO;
1✔
352
      }
353

354
      if (lhsIsZero) {
454!
UNCOV
355
        return rhs.Multiply(this.scMinusOne_);
×
356
      }
357

358
      if (rhsIsZero) {
605✔
359
        return lhs;
151✔
360
      }
361

362
      if (lhs.IsConstant(out var lhsConstant) &&
303✔
363
          rhs.IsConstant(out var rhsConstant)) {
321✔
364
        return new ScalarConstant(lhsConstant - rhsConstant);
18✔
365
      }
366
    }
285✔
367

368
    return lhs.Subtract(rhs);
285✔
369
  }
455✔
370

371
  public override IScalarValue MultiplyWithScalar(
372
      IScalarValue lhs,
373
      IScalarValue rhs)
374
    => this.Multiply(lhs, rhs);
474✔
375

376
  public override IScalarValue Multiply(IScalarValue lhs,
377
                                        IScalarValue rhs) {
1,831✔
378
    if (!FixedFunctionConstants.SIMPLIFY) {
1,831✔
379
      lhs ??= this.Zero;
380
      rhs ??= this.Zero;
381
    } else {
1,831✔
382
      if (lhs.IsZero() || rhs.IsZero()) {
2,472✔
383
        return ScalarConstant.ZERO;
641✔
384
      }
385

386
      var lhsIsOne = lhs.IsOne();
1,190✔
387
      var rhsIsOne = rhs.IsOne();
1,190✔
388

389
      if (lhsIsOne && rhsIsOne) {
1,193✔
390
        return ScalarConstant.ONE;
3✔
391
      }
392

393
      if (lhsIsOne) {
1,403✔
394
        return rhs;
216✔
395
      }
396

397
      if (rhsIsOne) {
1,471✔
398
        return lhs;
500✔
399
      }
400

401
      if (lhs.IsConstant(out var lhsConstant) &&
471✔
402
          rhs.IsConstant(out var rhsConstant)) {
482✔
403
        return new ScalarConstant(lhsConstant * rhsConstant);
11✔
404
      }
405
    }
460✔
406

407
    return lhs.Multiply(rhs);
460✔
408
  }
1,831✔
409

410
  public override IScalarValue MixWithScalar(IScalarValue lhs,
411
                                             IScalarValue rhs,
UNCOV
412
                                             IScalarValue mixAmount) {
×
UNCOV
413
    lhs = this.Multiply(lhs, this.Subtract(ScalarConstant.ONE, mixAmount));
×
UNCOV
414
    rhs = this.Multiply(rhs, mixAmount);
×
415

416
    return this.Add(lhs, rhs);
×
417
  }
×
418
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc