-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidate.go
61 lines (53 loc) · 2.18 KB
/
validate.go
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
package conval
import (
"fmt"
"log"
"github.com/ftl/hamradio/callsign"
)
func ValidateExamples(definition *Definition, prefixes PrefixDatabase) error {
for i, example := range definition.Examples {
err := validateExample(definition, example, prefixes, false)
if err != nil {
return fmt.Errorf("example #%d is invalid: %w", i+1, err)
}
}
return nil
}
func ValidateExamplesTrace(definition *Definition, prefixes PrefixDatabase) error {
for i, example := range definition.Examples {
log.Printf("example #%d", i+1)
err := validateExample(definition, example, prefixes, true)
if err != nil {
return fmt.Errorf("example #%d is invalid: %w", i+1, err)
}
}
return nil
}
func validateExample(definition *Definition, example Example, prefixes PrefixDatabase, trace bool) error {
counter := NewCounter(*definition, example.Setup.ToSetup(), prefixes)
counter.SetTrace(trace)
for i, qso := range example.QSOs {
exchangeFields := counter.EffectiveExchangeFields(callsign.MustParse(qso.TheirCall))
qsoScore := counter.Add(qso.ToQSO(exchangeFields, example.Setup.MyExchange, prefixes, definition))
if trace {
log.Printf("QSO #%d with exchange fields %v: %+v", i+1, exchangeFields, qsoScore)
}
if !(qso.Score.Equal(qsoScore)) {
return fmt.Errorf("the score of QSO #%d is wrong, expected %d points * %d multis, duplicate should be %t, but got %d points * %d multis, duplicate is %t", i+1, qso.Score.Points, qso.Score.Multis, qso.Score.Duplicate, qsoScore.Points, qsoScore.Multis, qsoScore.Duplicate)
}
}
totalScore := counter.TotalScore()
if trace {
log.Printf("%+v = %d", totalScore, counter.Total(totalScore))
}
if !equalScore(counter, example.Score, totalScore) {
return fmt.Errorf("the total score is wrong, expected %d qsos with %d points * %d multis, but got %d qsos with %d points * %d multis", example.Score.QSOs, example.Score.Points, example.Score.Multis, totalScore.QSOs, totalScore.Points, totalScore.Multis)
}
return nil
}
func equalScore(counter *Counter, expected ScoreExample, actual BandScore) bool {
return expected.QSOs == actual.QSOs &&
expected.Points == actual.Points &&
expected.Multis == actual.Multis &&
expected.Total == counter.Total(actual)
}