using Test
using VSOP87


# Testing

@testset "VSOP87 Main Series" begin

    # Mercury
    pv,ierr = vsop87(2451545.0,0,1,0.0)
    @test pv ≈ [0.3870982122, 4.4026057779, 0.0446647518, 0.2007208958, 0.0406161541, 0.0456355031] atol=1E-10

    pv,ierr = vsop87(2122820.0,0,1,0.0)
    @test pv ≈ [0.3870983087, 5.5532501234, 0.0496101939, 0.1993701567, 0.0400177912, 0.0467766328] atol=1E-10

    # Venus
    pv,ierr = vsop87(2451545.0,0,2,0.0)
    @test pv ≈ [0.7233269304, 3.176135091, -0.0045086077, 0.0050312182, 0.0068248058, 0.0288221481] atol=1E-10

    pv,ierr = vsop87(2122820.0,0,2,0.0)
    @test pv ≈ [0.7233247251, 3.5192700749, -0.0047739162, 0.0053755162, 0.0055732704, 0.0291355398] atol=1E-10

    # Mars
    pv,ierr = vsop87(2451545.0,0,4,0.0)
    @test pv ≈ [1.5236789887, 6.2038757099, 0.0853133078, -0.0378067117, 0.0104705229, 0.0122862593] atol=1E-10

    pv,ierr = vsop87(2122820.0,0,4,0.0)
    @test pv ≈ [1.5236045525, 3.0145860938, 0.0818247408, -0.0434649474, 0.0102833001, 0.0132404725] atol=1E-10

    # Jupiter
    pv,ierr = vsop87(2451545.0,0,5,0.0)
    @test pv ≈ [5.2042662908, 0.5999772955, 0.0469877116, 0.0130817658, -0.0020729462, 0.0111943279] atol=1E-10

    pv,ierr = vsop87(2122820.0,0,5,0.0)
    @test pv ≈ [5.2026637693, 1.3989872239, 0.0462126899, 0.010229216, -0.0018026304, 0.0114181037] atol=1E-10

    # Saturn
    pv,ierr = vsop87(2451545.0,0,6,0.0)
    @test pv ≈ [9.5820161867, 0.872743095, 0.0003336009, 0.0557224686, -0.0086968779, 0.0198660071] atol=1E-10

    pv,ierr = vsop87(2122820.0,0,6,0.0)
    @test pv ≈ [9.53164267, 3.6837162325, 0.0034527067, 0.0581970245, -0.0093872672, 0.0193059636] atol=1E-10

    # Uranus
    pv,ierr = vsop87(2451545.0,0,7,0.0)
    @test pv ≈ [19.2294229491, 5.4713756706, -0.0438022683, 0.0073046995, 0.0018594791, 0.0064803904] atol=1E-10

    pv,ierr = vsop87(2122820.0,0,7,0.0)
    @test pv ≈ [19.2685452353, 1.0026901573, -0.0428389228, 0.0113976086, 0.0019824675, 0.0066094091] atol=1E-10

    # Neptune
    pv,ierr = vsop87(2451545.0,0,8,0.0)
    @test pv ≈ [30.1036169706, 5.3268987909, 0.0089053321, 0.0068181684, -0.0102818994, 0.0115021344] atol=1E-10

    pv,ierr = vsop87(2122820.0,0,8,0.0)
    @test pv ≈ [30.2002490334, 2.4115747749, 0.003226111, 0.0104510184, -0.010289848, 0.0114907692] atol=1E-10

    # Earth-Moon (EMB)
    pv,ierr = vsop87(2451545.0,0,9,0.0)
    @test pv ≈ [0.9999964222, 1.7534128816, -0.0037339066, 0.0162796346, -6.036e-7, 6.284e-7] atol=1E-10

    pv,ierr = vsop87(2122820.0,0,9,0.0)
    @test pv ≈ [1.0000134926, 1.8519621673, -0.0029638175, 0.0168402194, 0.0010301901, -5.30795e-5] atol=1E-10

end


@testset "VSOP87 A-Series   " begin

    # Mercury
    pv,ierr = vsop87(2451545.0,1,1,0.0)
    @test pv ≈ [-.1300934115 , -.4472876716 , -.0245983802 , .0213663982 , -.0064479797 , -.0024878668] atol=1E-10

    pv,ierr = vsop87(2122820.0,1,1,0.0)
    @test pv ≈ [.2146329139 , -.3752296250 , -.0503982597 , .0188319750 , .0155086039 , -.0005235332] atol=1E-10

    # Venus
    pv,ierr = vsop87(2451545.0,1,2,0.0)
    @test pv ≈ [-.7183022797, -.0326546017, .0410142975, .0007981261, -.0202952188, -.0003234551] atol=1E-10

    pv,ierr = vsop87(2122820.0,1,2,0.0)
    @test pv ≈ [-.6660158465, -.2753592311, .0357874175, .0075852982, -.0187913517, -.0006523229 ] atol=1E-10

    # Earth
    pv,ierr = vsop87(2451545.0,1,3,0.0)
    @test pv ≈ [-.1771354586, .9672416237, -.0000039000, -.0172076240, -.0031587881, .0000001069] atol=1E-10

    pv,ierr = vsop87(2122820.0,1,3,0.0)
    @test pv ≈ [-.2763146784, .9433985307, .0019115387, -.0168030101, -.0048929320, -.0000120286] atol=1E-10

    # Mars
    pv,ierr = vsop87(2451545.0,1,4,0.0)
    @test pv ≈ [1.3907159264, -.0134157043, -.0344677967, .0006714930, .0151872479, .0003016546] atol=1E-10

    pv,ierr = vsop87(2122820.0,1,4,0.0)
    @test pv ≈ [-1.6278485157, .3060194814, .0494215740, -.0019834234, -.0126075128, -.0002068580] atol=1E-10

    # Jupiter
    pv,ierr = vsop87(2451545.0,1,5,0.0)
    @test pv ≈ [4.0011740268, 2.9385810077, -.1017837501, -.0045683226, .0064432013, .0000755806] atol=1E-10

    pv,ierr = vsop87(2122820.0,1,5,0.0)
    @test pv ≈ [.4207861894, 5.1019591310, -.0280087663, -.0076034843, .0009707381, .0001701694] atol=1E-10

    # Saturn
    pv,ierr = vsop87(2451545.0,1,6,0.0)
    @test pv ≈ [6.4064068573, 6.5699929449, -.3690768029,  -.0042923542, .0038903162, .0001029504] atol=1E-10

    pv,ierr = vsop87(2122820.0,1,6,0.0)
    @test pv ≈ [-7.9395559173, -5.8435867016, .4165601597, .0029826848, -.0044772655, -.0000311309] atol=1E-10

    # Uranus
    pv,ierr = vsop87(2451545.0,1,7,0.0)
    @test pv ≈ [14.4318934159, -13.7343162527, -.2381421963, .0026781013, .0026726895, -.0000247716] atol=1E-10

    pv,ierr = vsop87(2122820.0,1,7,0.0)
    @test pv ≈ [11.8546461039, 15.5595370553, -.0950189343, -.0031646232, .0022092867, .0000505971] atol=1E-10

    # Neptune
    pv,ierr = vsop87(2451545.0,1,8,0.0)
    @test pv ≈ [16.8121116576, -24.9916630908, .1272190171, .0025792564, .0017769299, -.0000959082] atol=1E-10

    pv,ierr = vsop87(2122820.0,1,8,0.0)
    @test pv ≈ [-22.7985170870, 19.5994768858, .1206374034,  -.0020722808, -.0023628830, .0000962862] atol=1E-10

    # Earth-Moon (EMB)
    pv,ierr = vsop87(2451545.0,1,9,0.0)
    @test pv ≈ [-.1771591440, .9672192891, -.0000009536, -.0172031075, -.0031639188, .0000000258] atol=1E-10

    pv,ierr = vsop87(2122820.0,1,9,0.0)
    @test pv ≈ [-.2762837551, .9433889919, .0019144183, -.0168007538, -.0048864356, -.0000118518] atol=1E-10

end


@testset "VSOP87 E-Series   " begin

    # Mercury
    pv,ierr = vsop87(2451545.0,5,1,0.0)
    @test pv ≈ [-.1372349394, -.4500758422, -.0243922379, .0213717757, -.0064553869, -.0024879611] atol=1E-10

    pv,ierr = vsop87(2122820.0,5,1,0.0)
    @test pv ≈ [.2171621774, -.3801214149, -.0504926506, .0188386487, .0155090442, -.0005236921] atol=1E-10

    # Venus
    pv,ierr = vsop87(2451545.0,5,2,0.0)
    @test pv ≈ [-.7254438061, -.0354427729, .0412204390, .0008035035, -.0203026261, -.0003235494] atol=1E-10

    pv,ierr = vsop87(2122820.0,5,2,0.0)
    @test pv ≈ [-.6634865823, -.2802510190, .0356930259, .0075919721, -.0187909113, -.0006524817] atol=1E-10

    # Earth
    pv,ierr = vsop87(2451545.0,5,3,0.0)
    @test pv ≈ [-.1842769826, .9644534530, .0002022407, -.0172022466, -.0031661954, .0000000125] atol=1E-10

    pv,ierr = vsop87(2122820.0,5,3,0.0)
    @test pv ≈ [-.2737854147, .9385067424, .0018171487, -.0167963362, -.0048924916, -.0000121874] atol=1E-10

    # Mars
    pv,ierr = vsop87(2451545.0,5,4,0.0)
    @test pv ≈ [1.3835744053, -.0162038666, -.0342616574, .0006768704, .0151798404, .0003015603] atol=1E-10

    pv,ierr = vsop87(2122820.0,5,4,0.0)
    @test pv ≈ [-1.6253192611, .3011276886, .0493271851, -.0019767501, -.0126070725, -.0002070169] atol=1E-10

    # Jupiter
    pv,ierr = vsop87(2451545.0,5,5,0.0)
    @test pv ≈ [3.9940325025, 2.9357928287, -.1015776146, -.0045629454, .0064357940, .0000754864] atol=1E-10

    pv,ierr = vsop87(2122820.0,5,5,0.0)
    @test pv ≈ [.4233154540, 5.0970673238, -.0281031462, -.0075968103, .0009711785, .0001700105] atol=1E-10

    # Saturn
    pv,ierr = vsop87(2451545.0,5,6,0.0)
    @test pv ≈ [6.3992653459, 6.5672047374, -.3688706482, -.0042869773, .0038829091, .0001028562] atol=1E-10

    pv,ierr = vsop87(2122820.0,5,6,0.0)
    @test pv ≈ [-7.9370266647, -5.8484785039, .4164657927, .0029893590, -.0044768249, -.0000312897] atol=1E-10

    # Uranus
    pv,ierr = vsop87(2451545.0,5,7,0.0)
    @test pv ≈ [14.4247519568, -13.7371045087, -.2379360887, .0026834778, .0026652832, -.0000248664] atol=1E-10

    pv,ierr = vsop87(2122820.0,5,7,0.0)
    @test pv ≈ [11.8571754273, 15.5546452202, -.0951132908, -.0031579496, .0022097279, .0000504382] atol=1E-10

    # Neptune
    pv,ierr = vsop87(2451545.0,5,8,0.0)
    @test pv ≈ [16.8049701269, -24.9944513569, .1274251215, .0025846351, .0017695229, -.0000960030] atol=1E-10

    pv,ierr = vsop87(2122820.0,5,8,0.0)
    @test pv ≈ [-22.7959876638, 19.5945850298, .1205430330, -.0020656054, -.0023624426, .0000961274] atol=1E-10

    # Sun
    pv,ierr = vsop87(2451545.0,5,0,0.0)
    @test pv ≈ [-.0071415279, -.0027881715, .0002061418, .0000053774, -.0000074073, -.0000000943] atol=1E-10

    pv,ierr = vsop87(2122820.0,5,0,0.0)
    @test pv ≈ [.0025292639, -.0048917910, -.0000943913, .0000066739, .0000004404, -.0000001588] atol=1E-10

end
