@@ -28,6 +28,11 @@ type testsData struct {
2828
2929const dir_path string = "../testdata"
3030const max_allowed_error = 0.2 // %
31+ // SV fields are just inregers in FORTRAN, so there might be situations where:
32+ // calculated value 16.47, reference 17
33+ // calculated value 2.52, reference 3
34+ // ...
35+ const max_sv_error = 50 // %
3136
3237func TestIGRFDataCases (t * testing.T ) {
3338 tests := getTestData ()
@@ -38,28 +43,91 @@ func TestIGRFDataCases(t *testing.T) {
3843 t .Errorf ("IGRF() error = %v, wantErr %v" , err , tt .wantErr )
3944 return
4045 }
41- compare_x := compareFloats (float64 (got .NorthComponent ), float64 (tt .want .NorthComponent ), max_allowed_error )
42- if ! compare_x {
46+ // Declination
47+ // there are just two digits after the dot in FORTRAN results, so truncating it
48+ dn := convertToPrecision (float64 (got .Declination ), 2 )
49+ compare := compareFloats (dn , float64 (tt .want .Declination ), max_allowed_error )
50+ if ! compare {
51+ t .Errorf ("IGRF() Declination = %v, want %v" , got .Declination , tt .want .Declination )
52+ }
53+ // Declination SV
54+ compare = compareFloats (math .Round (float64 (got .DeclinationSV )), float64 (tt .want .DeclinationSV ), max_sv_error )
55+ if ! compare {
56+ t .Errorf ("IGRF() DeclinationSV = %v, want %v" , got .DeclinationSV , tt .want .DeclinationSV )
57+ }
58+ // Inclination
59+ in := convertToPrecision (float64 (got .Inclination ), 2 )
60+ compare = compareFloats (in , float64 (tt .want .Inclination ), max_allowed_error )
61+ if ! compare {
62+ t .Errorf ("IGRF() Inclination = %v, want %v" , got .Inclination , tt .want .Inclination )
63+ }
64+ // Inclination SV
65+ compare = compareFloats (math .Round (float64 (got .InclinationSV )), float64 (tt .want .InclinationSV ), max_sv_error )
66+ if ! compare {
67+ t .Errorf ("IGRF() InclinationSV = %v, want %v" , got .InclinationSV , tt .want .InclinationSV )
68+ }
69+ // Horizontal intensity
70+ compare = compareFloats (float64 (got .HorizontalIntensity ), float64 (tt .want .HorizontalIntensity ), max_allowed_error )
71+ if ! compare {
72+ t .Errorf ("IGRF() Horizontal intensity = %v, want %v" , got .HorizontalIntensity , tt .want .HorizontalIntensity )
73+ }
74+ // Horizontal SV
75+ compare = compareFloats (math .Round (float64 (got .HorizontalSV )), float64 (tt .want .HorizontalSV ), max_sv_error )
76+ if ! compare {
77+ t .Errorf ("IGRF() HorizontalSV = %v, want %v" , got .HorizontalSV , tt .want .HorizontalSV )
78+ }
79+ // North component
80+ compare = compareFloats (float64 (got .NorthComponent ), float64 (tt .want .NorthComponent ), max_allowed_error )
81+ if ! compare {
4382 t .Errorf ("IGRF() NorthComponent = %v, want %v" , got .NorthComponent , tt .want .NorthComponent )
4483 }
45- // Fortran results are rounded!!!
84+ // North SV
85+ compare = compareFloats (math .Round (float64 (got .NorthSV )), float64 (tt .want .NorthSV ), max_sv_error )
86+ if ! compare {
87+ t .Errorf ("IGRF() NorthSV = %v, want %v" , got .NorthSV , tt .want .NorthSV )
88+ }
89+ // East Component - FORTRAN results are rounded!!!
4690 new_east_got := math .Round (float64 (got .EastComponent ))
4791 new_east_want := math .Round (float64 (tt .want .EastComponent ))
48- compare_y : = compareFloats (new_east_got , new_east_want , max_allowed_error )
49- if ! compare_y {
92+ compare = compareFloats (new_east_got , new_east_want , max_allowed_error )
93+ if ! compare {
5094 t .Errorf ("IGRF() EastComponent = %v, want %v" , got .EastComponent , tt .want .EastComponent )
5195 }
52- compare_z := compareFloats (float64 (got .VerticalComponent ), float64 (tt .want .VerticalComponent ), max_allowed_error )
53- if ! compare_z {
96+ // EastSV
97+ compare = compareFloats (math .Round (float64 (got .EastSV )), float64 (tt .want .EastSV ), max_sv_error )
98+ if ! compare {
99+ t .Errorf ("IGRF() EastSV = %v, want %v" , got .EastSV , tt .want .EastSV )
100+ }
101+ // Vertical component
102+ compare = compareFloats (float64 (got .VerticalComponent ), float64 (tt .want .VerticalComponent ), max_allowed_error )
103+ if ! compare {
54104 t .Errorf ("IGRF() VerticalComponent = %v, want %v" , got .VerticalComponent , tt .want .VerticalComponent )
55105 }
56- // if !reflect.DeepEqual(got, tt.want) {
57- // t.Errorf("IGRF() = %v, want %v", got, tt.want)
58- // }
106+ // VerticalSV
107+ compare = compareFloats (math .Round (float64 (got .VerticalSV )), float64 (tt .want .VerticalSV ), max_sv_error )
108+ if ! compare {
109+ t .Errorf ("IGRF() VerticalSV = %v, want %v" , got .VerticalSV , tt .want .VerticalSV )
110+ }
111+ // Total intensity
112+ compare = compareFloats (float64 (got .TotalIntensity ), float64 (tt .want .TotalIntensity ), max_allowed_error )
113+ if ! compare {
114+ t .Errorf ("IGRF() Total = %v, want %v" , got .TotalIntensity , tt .want .TotalIntensity )
115+ }
116+ // TotalSV
117+ compare = compareFloats (math .Round (float64 (got .TotalSV )), float64 (tt .want .TotalSV ), max_sv_error )
118+ if ! compare {
119+ t .Errorf ("IGRF() TotalSV = %v, want %v" , got .TotalSV , tt .want .TotalSV )
120+ }
59121 })
60122 }
61123}
62124
125+ func convertToPrecision (value float64 , precision int ) float64 {
126+ format_verbs := fmt .Sprintf ("%%.%vf" , precision )
127+ vs := fmt .Sprintf (format_verbs , value )
128+ return toFloat64 (vs )
129+ }
130+
63131func compareFloats (check , base , allowable_error float64 ) bool {
64132 value1 := math .Abs (check )
65133 value2 := math .Abs (base )
0 commit comments