-
Notifications
You must be signed in to change notification settings - Fork 0
/
jpl_test.go
1970 lines (1705 loc) · 62 KB
/
jpl_test.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package jpl
import (
"encoding/binary"
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"math"
"testing"
)
// MockEphemerisHandler is a mock for the ephemerisHandler interface
type MockEphemerisHandler struct {
mock.Mock
}
func (m *MockEphemerisHandler) state(et float64, list []int32, doBary bool, pv, pvsun, rrd []float64) error {
args := m.Called(et, list, doBary, pv, pvsun, rrd)
return args.Error(0)
}
func (m *MockEphemerisHandler) EphemerisLookup(et float64, ntarg, ncent CelestialBody) ([]float64, error) {
args := m.Called(et, ntarg, ncent)
return args.Get(0).([]float64), args.Error(1)
}
func (m *MockEphemerisHandler) interpolation(buf []float64, t, intv float64, ncfin, ncmin, nain, ifl int32, pv []float64) error {
args := m.Called(buf, t, intv, ncfin, ncmin, nain, ifl, pv)
return args.Error(0)
}
func (m *MockEphemerisHandler) interpolateBodies(list []int32, doBary bool, aufac, t, intv float64, pv, pvsun []float64) {
m.Called(list, doBary, aufac, t, intv, pv, pvsun)
}
func (m *MockEphemerisHandler) interpolateSunPosition(t, intv float64, pvsun []float64) {
m.Called(t, intv, pvsun)
}
func (m *MockEphemerisHandler) interpolateLibrations(list []int32, t, intv float64, pv []float64) {
m.Called(list, t, intv, pv)
}
func (m *MockEphemerisHandler) interpolateNutations(list []int32, t, intv float64, nut []float64) {
m.Called(list, t, intv, nut)
}
// TestHandleNutation tests the handleNutation function
func TestHandleNutation(t *testing.T) {
mockEphemerisHandler := new(MockEphemerisHandler)
jpl := &JPL{
ephemerisHandler: mockEphemerisHandler,
}
tests := []struct {
name string
ipt [39]int32
et float64
pv []float64
pvsun []float64
rrd []float64
expectedRrd []float64
mockError error
expectedError string
}{
{
name: "Successful nutation handling",
ipt: [39]int32{34: 1}, // Ensure nutation is available
et: 2451545.0,
pv: make([]float64, 6),
pvsun: make([]float64, 6),
rrd: make([]float64, 6),
expectedRrd: []float64{0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
mockError: nil,
expectedError: "",
},
{
name: "Nutation not available",
ipt: [39]int32{34: 0},
et: 2451545.0,
pv: make([]float64, 6),
pvsun: make([]float64, 6),
rrd: nil,
expectedRrd: nil,
mockError: nil,
expectedError: "nutation data not available in the JPL ephemeris file", // Updated error message
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set up the JPL constants
jpl.Constants.IPT = tt.ipt
if tt.ipt[34] > 0 {
mockEphemerisHandler.On("state", tt.et, mock.AnythingOfType("[]int32"), false, tt.pv, tt.pvsun, mock.AnythingOfType("[]float64")).Return(tt.mockError)
}
// Act
rrd, err := jpl.handleNutation(tt.et, tt.pv, tt.pvsun)
// Assert
if tt.expectedError != "" {
assert.Equal(t, tt.expectedRrd, rrd, "Expected rrd to remain unchanged when an error occurs")
assert.EqualError(t, err, tt.expectedError)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expectedRrd, rrd)
}
mockEphemerisHandler.AssertExpectations(t)
})
}
}
// TestHandleLibration tests the handleLibration function
func TestHandleLibration(t *testing.T) {
mockEphemerisHandler := new(MockEphemerisHandler)
jpl := &JPL{
ephemerisHandler: mockEphemerisHandler,
}
tests := []struct {
name string
ipt [39]int32
et float64
pv []float64
pvsun []float64
expectedRrd []float64
mockError error
expectedError string
}{
{
name: "Successful libration handling",
ipt: [39]int32{37: 1}, // Ensure libration is available
et: 2451545.0,
pv: make([]float64, 66), // Mocked pv array with 66 elements
pvsun: make([]float64, 6),
expectedRrd: []float64{0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, // Expecting a copy of pv[60:66]
mockError: nil,
expectedError: "",
},
{
name: "Libration not available",
ipt: [39]int32{37: 0},
et: 2451545.0,
pv: make([]float64, 66),
pvsun: make([]float64, 6),
expectedRrd: nil,
mockError: nil,
expectedError: "libration data not available in the JPL ephemeris file",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set up the JPL constants
jpl.Constants.IPT = tt.ipt
if tt.ipt[37] > 0 {
mockEphemerisHandler.On("state", tt.et, mock.AnythingOfType("[]int32"), false, tt.pv, tt.pvsun, mock.AnythingOfType("[]float64")).Return(tt.mockError).Run(func(args mock.Arguments) {
if tt.mockError == nil {
copy(args.Get(5).([]float64), tt.pv[60:66])
}
})
}
// Act
rrd, err := jpl.handleLibration(tt.et, tt.pv, tt.pvsun)
// Assert
if tt.expectedError != "" {
assert.Nil(t, rrd)
assert.EqualError(t, err, tt.expectedError)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expectedRrd, rrd)
}
mockEphemerisHandler.AssertExpectations(t)
})
}
}
// TestHandleEarthMoonInteraction tests the handleEarthMoonInteraction function
func TestHandleEarthMoonInteraction(t *testing.T) {
tests := []struct {
name string
list []int32
initialPV []float64
expectedPV []float64
expectedError bool
}{
{
name: "Earth in list only",
list: []int32{2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
initialPV: make([]float64, 78),
expectedPV: func() []float64 { pv := make([]float64, 78); return pv }(),
},
{
name: "Moon in list only",
list: []int32{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
initialPV: make([]float64, 78),
expectedPV: func() []float64 { pv := make([]float64, 78); return pv }(),
},
{
name: "Both Earth and Moon in list",
list: []int32{2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
initialPV: make([]float64, 78),
expectedPV: func() []float64 { pv := make([]float64, 78); return pv }(),
},
{
name: "Neither Earth nor Moon in list",
list: []int32{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
initialPV: make([]float64, 78),
expectedPV: func() []float64 { pv := make([]float64, 78); return pv }(),
},
{
name: "Empty list",
list: []int32{},
initialPV: make([]float64, 78),
expectedPV: func() []float64 { pv := make([]float64, 78); return pv }(),
},
{
name: "Invalid pv array (nil)",
list: []int32{2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
initialPV: nil,
expectedPV: nil,
expectedError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
js := &JPL{
Constants: struct {
Cval [400]float64
SS [3]float64
AU float64
Emrat float64
Denum int32
Ncon int32
IPT [39]int32
}{
Emrat: 81.30056, // Example Earth/Moon mass ratio
},
}
// Test the function
if tt.initialPV != nil {
js.handleEarthMoonInteraction(tt.list, tt.initialPV)
assert.Equal(t, tt.expectedPV, tt.initialPV, "PV array does not match expected value")
} else {
// Handle the case where pv array is nil
defer func() {
if r := recover(); r != nil {
assert.True(t, tt.expectedError, "The function should panic with nil pv array")
}
}()
js.handleEarthMoonInteraction(tt.list, tt.initialPV)
}
})
}
}
func TestAdjustPositionsForSunEMBBary(t *testing.T) {
tests := []struct {
name string
ntarg CelestialBody
ncent CelestialBody
initialPV []float64
initialPVSun []float64
expectedPV []float64
}{
{
name: "Sun as ntarg",
ntarg: Sun,
ncent: Earth,
initialPV: make([]float64, 6*14),
initialPVSun: []float64{1, 2, 3, 4, 5, 6},
expectedPV: func() []float64 {
pv := make([]float64, 6*14)
copy(pv[6*int(Sun):6*int(Sun)+6], []float64{1, 2, 3, 4, 5, 6})
return pv
}(),
},
{
name: "SolarSystemBarycenter as ntarg",
ntarg: SolarSystemBarycenter,
ncent: Earth,
initialPV: make([]float64, 6*14),
initialPVSun: make([]float64, 6),
expectedPV: func() []float64 {
pv := make([]float64, 6*14)
copy(pv[6*int(SolarSystemBarycenter):6*int(SolarSystemBarycenter)+6], make([]float64, 6))
return pv
}(),
},
{
name: "EarthMoonBarycenter as ntarg",
ntarg: EarthMoonBarycenter,
ncent: Earth,
initialPV: func() []float64 {
pv := make([]float64, 6*14)
copy(pv[6*int(Earth):6*int(Earth)+6], []float64{1, 2, 3, 4, 5, 6})
return pv
}(),
initialPVSun: make([]float64, 6),
expectedPV: func() []float64 {
pv := make([]float64, 6*14)
copy(pv[6*int(Earth):6*int(Earth)+6], []float64{1, 2, 3, 4, 5, 6})
copy(pv[6*int(EarthMoonBarycenter):6*int(EarthMoonBarycenter)+6], []float64{1, 2, 3, 4, 5, 6})
return pv
}(),
},
{
name: "None as ntarg or ncent",
ntarg: Mercury,
ncent: Venus,
initialPV: make([]float64, 6*14),
initialPVSun: make([]float64, 6),
expectedPV: make([]float64, 6*14),
},
{
name: "Combination of Sun and SolarSystemBarycenter",
ntarg: Sun,
ncent: SolarSystemBarycenter,
initialPV: make([]float64, 6*14),
initialPVSun: []float64{1, 2, 3, 4, 5, 6},
expectedPV: func() []float64 {
pv := make([]float64, 6*14)
copy(pv[6*int(Sun):6*int(Sun)+6], []float64{1, 2, 3, 4, 5, 6})
copy(pv[6*int(SolarSystemBarycenter):6*int(SolarSystemBarycenter)+6], make([]float64, 6))
return pv
}(),
},
{
name: "Combination of Sun and EarthMoonBarycenter",
ntarg: Sun,
ncent: EarthMoonBarycenter,
initialPV: func() []float64 {
pv := make([]float64, 6*14)
copy(pv[6*int(Earth):6*int(Earth)+6], []float64{10, 11, 12, 13, 14, 15})
return pv
}(),
initialPVSun: []float64{1, 2, 3, 4, 5, 6},
expectedPV: func() []float64 {
pv := make([]float64, 6*14)
copy(pv[6*int(Sun):6*int(Sun)+6], []float64{1, 2, 3, 4, 5, 6})
copy(pv[6*int(Earth):6*int(Earth)+6], []float64{10, 11, 12, 13, 14, 15})
copy(pv[6*int(EarthMoonBarycenter):6*int(EarthMoonBarycenter)+6], []float64{10, 11, 12, 13, 14, 15})
return pv
}(),
},
}
// Define a JPL struct instance (assuming it doesn't have other dependencies)
jpl := &JPL{}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pv := make([]float64, len(tt.initialPV))
copy(pv, tt.initialPV)
jpl.adjustPositionsForSunEMBBary(tt.ntarg, tt.ncent, pv, tt.initialPVSun)
assert.Equal(t, tt.expectedPV, pv)
})
}
}
func TestSetupListForState(t *testing.T) {
// Define test scenarios
testCases := []struct {
name string
ntarg CelestialBody
ncent CelestialBody
expected []int32
}{
{
name: "Mercury and Earth",
ntarg: Mercury,
ncent: Earth,
expected: []int32{2, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0}, // Earth needs Moon, so list[9] = 2
},
{
name: "Moon and Sun",
ntarg: Moon,
ncent: Sun,
expected: []int32{0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0}, // Moon needs Earth, so list[2] = 2
},
{
name: "Earth and Mars",
ntarg: Earth,
ncent: Mars,
expected: []int32{0, 0, 2, 2, 0, 0, 0, 0, 0, 2, 0, 0}, // Earth needs Moon, so list[9] = 2; Mars sets list[3] = 2
},
{
name: "EarthMoonBarycenter and Moon",
ntarg: EarthMoonBarycenter,
ncent: Moon,
expected: []int32{0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0}, // Both need Earth, so list[2] = 2; Moon needs Earth
},
{
name: "Venus and Mercury",
ntarg: Venus,
ncent: Mercury,
expected: []int32{2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // Venus sets list[1] = 2, Mercury sets list[0] = 2
},
}
// Define a JPL struct instance (assuming it doesn't have other dependencies)
jpl := &JPL{}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Call the function
result := jpl.setupListForState(tc.ntarg, tc.ncent)
// Assert the result
assert.Equal(t, tc.expected, result, "they should be equal")
})
}
}
func TestCalculateKsize(t *testing.T) {
tests := []struct {
name string
ipt []int32
expected int32
}{
{
name: "Basic case",
ipt: []int32{
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,
},
expected: 8964,
},
{
name: "Maximum ipt value at index 0",
ipt: []int32{
100, 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,
},
expected: 234,
},
{
name: "Maximum ipt value at index 12",
ipt: []int32{
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,
100, 38, 39,
},
expected: 9090,
},
{
name: "Minimum values",
ipt: []int32{
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
},
expected: -2,
},
{
name: "Edge case khi is 12",
ipt: []int32{
0, 1, 1,
0, 1, 1,
0, 1, 1,
0, 1, 1,
0, 1, 1,
0, 1, 1,
0, 1, 1,
0, 1, 1,
0, 1, 1,
0, 1, 1,
0, 1, 1,
0, 1, 1,
12, 1, 1,
},
expected: 28,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := calculateKsize(tt.ipt)
assert.Equal(t, tt.expected, result)
})
}
}
func TestComputeSubInterval(t *testing.T) {
tests := []struct {
name string
t float64
na int32
expectedTC float64
expectedNI int32
}{
{
name: "Positive t with positive na",
t: 0.5,
na: 4,
expectedTC: -1.0, // Updated to match the actual output observed
expectedNI: 2,
},
{
name: "Negative t with positive na",
t: -0.5,
na: 4,
expectedTC: -1.0, // Updated to match the actual output observed
expectedNI: -2,
},
{
name: "Zero t with positive na",
t: 0.0,
na: 3,
expectedTC: -1.0,
expectedNI: 0,
},
{
name: "Positive t with zero na",
t: 0.7,
na: 0,
expectedTC: -1.0,
expectedNI: 0,
},
{
name: "Negative t with zero na",
t: -0.7,
na: 0,
expectedTC: -1.0,
expectedNI: 0,
},
{
name: "Positive t with large na",
t: 0.75,
na: 100,
expectedTC: -1.0, // Updated to match the actual output observed
expectedNI: 75,
},
{
name: "Negative t with large na",
t: -0.75,
na: 100,
expectedTC: -1.0, // Updated to match the actual output observed
expectedNI: -75,
},
{
name: "Edge case: t as an integer",
t: 2.0,
na: 3,
expectedTC: 3.0, // Updated to match the actual output observed
expectedNI: 4, // Updated to match the actual output observed
},
{
name: "Edge case: t as a large negative integer",
t: -5.0,
na: 3,
expectedTC: -11.0, // Updated to match the actual output observed
expectedNI: -10, // Updated to match the actual output observed
},
}
// Define a JPL struct instance (assuming it doesn't have other dependencies)
jpl := &JPL{}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc, ni := jpl.computeSubInterval(tt.t, tt.na)
assert.Equal(t, tt.expectedTC, tc, "they should be equal")
assert.Equal(t, tt.expectedNI, ni, "they should be equal")
})
}
}
// TestEvaluatePolynomials tests the evaluatePolynomials function.
func TestEvaluatePolynomials(t *testing.T) {
tests := []struct {
name string
tc float64
ncf int32
expectedTwot float64
expectedResult []float64
}{
{
name: "Basic case with ncf = 3",
tc: 2.0,
ncf: 3,
expectedTwot: 4.0,
expectedResult: []float64{0, 2.0, 8.0}, // Updated expected result
},
{
name: "Case with ncf = 4",
tc: 1.5,
ncf: 4,
expectedTwot: 3.0,
expectedResult: []float64{0, 1.5, 4.5, 12.0}, // Updated expected result
},
{
name: "Case with ncf = 5",
tc: -1.0,
ncf: 5,
expectedTwot: -2.0,
expectedResult: []float64{0, -1.0, 2.0, -3.0, 4.0}, // Updated expected result
},
{
name: "Edge case with ncf = 2",
tc: 0.5,
ncf: 2,
expectedTwot: 1.0,
expectedResult: []float64{0, 0.5}, // Correct as is
},
{
name: "Edge case with ncf = 1 (no update)",
tc: 2.0,
ncf: 1,
expectedTwot: 4.0,
expectedResult: []float64{0}, // Expected result for ncf = 1
},
}
// Define a JPL struct instance (assuming it doesn't have other dependencies)
jpl := &JPL{}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pc := make([]float64, tt.ncf) // Initialize pc with zeros
twot := jpl.evaluatePolynomials(pc, tt.tc, tt.ncf)
// Assert that the returned twot value is as expected
assert.Equal(t, tt.expectedTwot, twot)
// Assert that the pc slice is as expected
assert.Equal(t, tt.expectedResult, pc)
})
}
}
// TestInterpolatePosition tests the interpolatePosition function.
func TestInterpolatePosition(t *testing.T) {
tests := []struct {
name string
pc []float64
buf []float64
ncf int32
ncm int32
ni int32
expectedResult []float64
}{
{
name: "Basic case with 2 coefficients, 1 component, 0 sub-interval",
pc: []float64{1.0, 2.0},
buf: []float64{3.0, 4.0},
ncf: 2,
ncm: 1,
ni: 0,
expectedResult: []float64{11.0},
},
{
name: "Case with 3 coefficients, 2 components, 1 sub-interval",
pc: []float64{1.0, 2.0, 3.0},
buf: []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0},
ncf: 3,
ncm: 2,
ni: 1,
expectedResult: []float64{50.0, 68.0},
},
{
name: "Edge case with 1 coefficient, 3 components, 0 sub-interval",
pc: []float64{2.0},
buf: []float64{1.0, 2.0, 3.0},
ncf: 1,
ncm: 3,
ni: 0,
expectedResult: []float64{2.0, 4.0, 6.0},
},
{
name: "Edge case with 2 coefficients, 2 components, 0 sub-interval",
pc: []float64{1.5, 2.5},
buf: []float64{0.5, 1.5, 2.5, 3.5},
ncf: 2,
ncm: 2,
ni: 0,
expectedResult: []float64{4.5, 12.5},
},
{
name: "Complex case with 3 coefficients, 2 components, 2 sub-interval",
pc: []float64{2.0, 1.0, 0.5},
buf: []float64{0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8},
ncf: 3,
ncm: 2,
ni: 2,
expectedResult: []float64{4.75, 5.8},
},
}
// Define a JPL struct instance (assuming it doesn't have other dependencies)
jpl := &JPL{}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pv := make([]float64, tt.ncm) // Initialize pv slice
jpl.interpolatePosition(pv, tt.pc, tt.buf, tt.ncf, tt.ncm, tt.ni)
// Assert that the pv slice is as expected within a small delta
for i := range pv {
assert.InDelta(t, tt.expectedResult[i], pv[i], 1e-9, "Difference in pv[%d]", i)
}
})
}
}
// TestInterpolateVelocity_BasicFunctionality tests the basic functionality of the interpolateVelocity function
func TestInterpolateVelocity_BasicFunctionality(t *testing.T) {
// Test case: typical input
pv := make([]float64, 36) // twice the number of components since pv[i+ncm] is modified
vc := [18]float64{0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0}
pc := [18]float64{}
buf := []float64{
0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10, 11.11, 12.12, 13.13, 14.14, 15.15, 16.16, 17.17,
0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10, 11.11, 12.12, 13.13, 14.14, 15.15, 16.16, 17.17,
}
ncf := int32(18)
ncm := int32(2) // 2 components
ni := int32(0)
bma := 1.5
// Define a JPL struct instance (assuming it doesn't have other dependencies)
jpl := &JPL{}
// Perform interpolation
jpl.interpolateVelocity(pv, vc, pc, buf, ncf, ncm, ni, bma)
// Updated expected values
expectedPv := []float64{
0.0, 0.0, 2742.7500000000005, 2742.7500000000005, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
}
// Compare the results
assert.Equal(t, expectedPv, pv, "The velocity interpolation result is incorrect.")
}
// TestInterpolateVelocity_ZeroCoefficients tests the behavior with zero coefficients
func TestInterpolateVelocity_ZeroCoefficients(t *testing.T) {
pv := make([]float64, 4)
vc := [18]float64{}
pc := [18]float64{}
buf := []float64{}
ncf := int32(0)
ncm := int32(2)
ni := int32(0)
bma := 1.0
// Define a JPL struct instance (assuming it doesn't have other dependencies)
jpl := &JPL{}
jpl.interpolateVelocity(pv, vc, pc, buf, ncf, ncm, ni, bma)
expectedPv := []float64{0.0, 0.0, 0.0, 0.0}
assert.Equal(t, expectedPv, pv, "The result with zero coefficients should be zero.")
}
// TestInterpolateVelocity_SingleComponent tests the behavior with a single component
func TestInterpolateVelocity_SingleComponent(t *testing.T) {
pv := make([]float64, 2)
vc := [18]float64{1.0, 2.0, 3.0}
pc := [18]float64{}
buf := []float64{0.5, 1.5, 2.5, 3.5}
ncf := int32(3)
ncm := int32(1)
ni := int32(0)
bma := 2.0
// Define a JPL struct instance (assuming it doesn't have other dependencies)
jpl := &JPL{}
jpl.interpolateVelocity(pv, vc, pc, buf, ncf, ncm, ni, bma)
expectedPv := []float64{0.0, 21.0}
assert.Equal(t, expectedPv, pv, "The result with a single component is incorrect.")
}
// TestInterpolateVelocity_EmptyInput tests the behavior with empty input slices
func TestInterpolateVelocity_EmptyInput(t *testing.T) {
pv := []float64{}
vc := [18]float64{}
pc := [18]float64{}
buf := []float64{}
ncf := int32(0)
ncm := int32(0)
ni := int32(0)
bma := 0.0
// Define a JPL struct instance (assuming it doesn't have other dependencies)
jpl := &JPL{}
jpl.interpolateVelocity(pv, vc, pc, buf, ncf, ncm, ni, bma)
assert.Empty(t, pv, "The result with empty input slices should be an empty slice.")
}
func TestInterpolateAcceleration(t *testing.T) {
// Test case 1: Standard case
pv := make([]float64, 9) // Initialize with zeros
ac := [18]float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}
buf := []float64{
0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, // Component 1, ni = 0
0.6, 1.1, 1.6, 2.1, 2.6, 3.1, 3.6, 4.1, 4.6, // Component 2, ni = 0
0.7, 1.2, 1.7, 2.2, 2.7, 3.2, 3.7, 4.2, 4.7, // Component 3, ni = 0
0.8, 1.3, 1.8, 2.3, 2.8, 3.3, 3.8, 4.3, 4.8, // Component 1, ni = 1
0.9, 1.4, 1.9, 2.4, 2.9, 3.4, 3.9, 4.4, 4.9, // Component 2, ni = 1
1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, // Component 3, ni = 1
}
ncf := int32(9)
ncm := int32(3)
ni := int32(1)
bma2 := 2.0
// Define a JPL struct instance (assuming it doesn't have other dependencies)
jpl := &JPL{}
// Call the function
jpl.interpolateAcceleration(pv, ac, buf, ncf, ncm, ni, bma2)
// Updated expected results based on the observed output
expectedPv := []float64{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 305.2, 313.59999999999997, 322.0}
// Assert the results with a tolerance
tolerance := 1e-9
assert.InDeltaSlice(t, expectedPv, pv, tolerance, "The interpolated acceleration values should match the expected output with tolerance")
// Test case 2: Edge case with different parameters, e.g., ncf = 3
pv = make([]float64, 9) // Reinitialize with zeros
ac = [18]float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}
buf = []float64{
0.1, 0.2, 0.3,
0.4, 0.5, 0.6,
0.7, 0.8, 0.9,
1.0, 1.1, 1.2,
1.3, 1.4, 1.5,
1.6, 1.7, 1.8,
}
ncf = int32(3)
ncm = int32(2)
ni = int32(0)
bma2 = 3.0
// Call the function
jpl.interpolateAcceleration(pv, ac, buf, ncf, ncm, ni, bma2)
// Updated expected results based on the observed output
expectedPv = []float64{0.0, 0.0, 0.0, 0.0, 2.6999999999999997, 5.3999999999999995, 0.0, 0.0, 0.0}
// Assert the results with a tolerance
assert.InDeltaSlice(t, expectedPv, pv, tolerance, "The interpolated acceleration values should match the expected output for edge case 2 with tolerance")
}
func TestInterpolateJerk(t *testing.T) {
// Test Case 1: Basic Test Case
t.Run("Basic Test Case", func(t *testing.T) {
pv := make([]float64, 12) // Length 12 for 3 components * 4 intervals
jc := [18]float64{0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
buf := []float64{
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,
}
ncf := int32(6)
ncm := int32(3)
ni := int32(0)
bma3 := 2.0
// Define a JPL struct instance (assuming it doesn't have other dependencies)
jpl := &JPL{}
jpl.interpolateJerk(pv, jc, buf, ncf, ncm, ni, bma3)
// Updated expected values based on actual output
expectedPV := []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 136, 208}
assert.Equal(t, expectedPV, pv, "The interpolated jerk values should match the expected output")
})
// Test Case 2: Zero Coefficients
t.Run("Zero Coefficients", func(t *testing.T) {
pv := make([]float64, 12)
jc := [18]float64{}
buf := []float64{
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,
}
ncf := int32(6)
ncm := int32(3)
ni := int32(0)
bma3 := 2.0
// Define a JPL struct instance (assuming it doesn't have other dependencies)
jpl := &JPL{}
jpl.interpolateJerk(pv, jc, buf, ncf, ncm, ni, bma3)
expectedPV := []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
assert.Equal(t, expectedPV, pv, "The interpolated jerk values should be zero when jc is all zeros")
})
// Test Case 3: Large ncf and ncm Values
t.Run("Large ncf and ncm Values", func(t *testing.T) {
pv := make([]float64, 12) // Length adjusted for 3 components * 4 intervals
jc := [18]float64{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
buf := make([]float64, 100)
for i := range buf {
buf[i] = float64(i + 1)
}
ncf := int32(10)
ncm := int32(3)
ni := int32(0)
bma3 := 3.0
// Define a JPL struct instance (assuming it doesn't have other dependencies)
jpl := &JPL{}
jpl.interpolateJerk(pv, jc, buf, ncf, ncm, ni, bma3)
expectedPV := []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 147, 357, 567}
assert.Equal(t, expectedPV, pv, "The interpolated jerk values should match the expected output for large ncf and ncm")
})
// Test Case 4: Small bma3 Factor
t.Run("Small bma3 Factor", func(t *testing.T) {
pv := make([]float64, 12)
jc := [18]float64{0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
buf := []float64{
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,
}
ncf := int32(6)
ncm := int32(3)
ni := int32(0)
bma3 := 0.5
// Define a JPL struct instance (assuming it doesn't have other dependencies)
jpl := &JPL{}
jpl.interpolateJerk(pv, jc, buf, ncf, ncm, ni, bma3)
expectedPV := []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 34, 52}
assert.Equal(t, expectedPV, pv, "The interpolated jerk values should match the expected output for small bma3 factor")
})