using Test
using PlotAdaptiveGrid1D

@testset "adapted_grid" begin
  @testset "sin(1/x) on [0,1]" begin
    f(x) = sin(1/x)
    xs = adapted_grid(f, (0.0, 1.0))
    @test xs == [7.629546109209478e-5, 0.00015259092218418955, 0.00022888638327628433, 0.0003051818443683791, 0.0003814773054604739, 0.00045777276655256866, 0.0005340682276446635, 0.0006103636887367582, 0.0006866591498288529, 0.0007629546109209478, 0.0008392500720130426, 0.0009155455331051373, 0.0009918409941972323, 0.001068136455289327, 0.0011444319163814217, 0.0012207273774735164, 0.0012970228385656112, 0.0013733182996577059, 0.0014496137607498006, 0.0015259092218418955, 0.0016022046829339905, 0.0016785001440260852, 0.00175479560511818, 0.0018310910662102746, 0.0019073865273023696, 0.0019836819883944645, 0.0020599774494865592, 0.002136272910578654, 0.0022125683716707487, 0.0022888638327628434, 0.002365159293854938, 0.002441454754947033, 0.0025177502160391276, 0.0025940456771312223, 0.002670341138223317, 0.0027466365993154118, 0.0028229320604075065, 0.002899227521499601, 0.002975522982591696, 0.003051818443683791, 0.0031281139047758862, 0.003204409365867981, 0.0032807048269600757, 0.0033570002880521704, 0.003433295749144265, 0.00350959121023636, 0.0035858866713284546, 0.0036621821324205493, 0.003738477593512644, 0.003814773054604739, 0.0038910685156968343, 0.003967363976788929, 0.004043659437881024, 0.0041199548989731185, 0.004196250360065213, 0.004272545821157308, 0.004348841282249403, 0.004425136743341497, 0.004501432204433592, 0.004577727665525687, 0.0046540231266177816, 0.004730318587709876, 0.004806614048801971, 0.004882909509894066, 0.0049592049709861604, 0.005035500432078255, 0.00511179589317035, 0.005188091354262445, 0.005264386815354539, 0.005340682276446634, 0.005416977737538729, 0.0054932731986308235, 0.005569568659722918, 0.005645864120815013, 0.005722159581907108, 0.005798455042999202, 0.005874750504091297, 0.005951045965183392, 0.006027341426275487, 0.006103636887367582, 0.006179932348459677, 0.0062562278095517725, 0.006332523270643867, 0.006408818731735962, 0.006485114192828057, 0.006561409653920151, 0.006637705115012246, 0.006714000576104341, 0.0067902960371964355, 0.00686659149828853, 0.006942886959380625, 0.00701918242047272, 0.007095477881564814, 0.007171773342656909, 0.007248068803749004, 0.007324364264841099, 0.007400659725933193, 0.007476955187025288, 0.007553250648117384, 0.007629546109209478, 0.007705841570301573, 0.007782137031393669, 0.007858432492485763, 0.007934727953577858, 0.008011023414669954, 0.008087318875762048, 0.008163614336854141, 0.008239909797946237, 0.008316205259038333, 0.008392500720130426, 0.00846879618122252, 0.008545091642314616, 0.008621387103406711, 0.008697682564498805, 0.0087739780255909, 0.008850273486682995, 0.00892656894777509, 0.009002864408867184, 0.009079159869959278, 0.009155455331051374, 0.00923175079214347, 0.009308046253235563, 0.009384341714327657, 0.009460637175419753, 0.009536932636511848, 0.009613228097603942, 0.009689523558696036, 0.009765819019788131, 0.01047128072407395, 0.011176742428359768, 0.011882204132645586, 0.012587665836931404, 0.013293127541217222, 0.01399858924550304, 0.014704050949788858, 0.015409512654074675, 0.01611497435836049, 0.01682043606264631, 0.017525897766932128, 0.018231359471217946, 0.018936821175503764, 0.019642282879789582, 0.0203477445840754, 0.021053206288361218, 0.021758667992647036, 0.02246412969693285, 0.02316959140121867, 0.023875053105504487, 0.024580514809790305, 0.025285976514076124, 0.025991438218361942, 0.02669689992264776, 0.027402361626933578, 0.028107823331219396, 0.028813285035505214, 0.029518746739791032, 0.03022420844407685, 0.03092967014836267, 0.03163513185264849, 0.032340593556934305, 0.03304605526122012, 0.03375151696550594, 0.03445697866979176, 0.03516244037407758, 0.03586790207836339, 0.036573363782649214, 0.037278825486935036, 0.03798428719122085, 0.038689748895506665, 0.039395210599792486, 0.04010067230407831, 0.04080613400836412, 0.04151159571264994, 0.04221705741693576, 0.04292251912122158, 0.043627980825507395, 0.04433344252979321, 0.04503890423407903, 0.04574436593836485, 0.04644982764265067, 0.04715528934693648, 0.047860751051222304, 0.048566212755508126, 0.04927167445979394, 0.049977136164079755, 0.05068259786836558, 0.0513880595726514, 0.05209352127693721, 0.05279898298122303, 0.05350444468550885, 0.05420990638979467, 0.054915368094080486, 0.0556208297983663, 0.05632629150265212, 0.057031753206937943, 0.05773721491122376, 0.05844267661550957, 0.059148138319795394, 0.059853600024081216, 0.06055906172836703, 0.061264523432652845, 0.06196998513693867, 0.06267544684122449, 0.0633809085455103, 0.06408637024979613, 0.06479183195408195, 0.06549729365836776, 0.06620275536265358, 0.06690821706693939, 0.0676136787712252, 0.06831914047551102, 0.06902460217979685, 0.06973006388408268, 0.07043552558836849, 0.0711409872926543, 0.07184644899694012, 0.07255191070122594, 0.07325737240551175, 0.07396283410979757, 0.0746682958140834, 0.07537375751836922, 0.07607921922265504, 0.07678468092694085, 0.07749014263122667, 0.0789010660397983, 0.08031198944836994, 0.08172291285694158, 0.08313383626551321, 0.08383929796979903, 0.08454475967408484, 0.08525022137837066, 0.08595568308265648, 0.08666114478694231, 0.08736660649122813, 0.08807206819551394, 0.08877752989979976, 0.08948299160408557, 0.09018845330837139, 0.0908939150126572, 0.09159937671694303, 0.09301030012551467, 0.0944212235340863, 0.09583214694265793, 0.09724307035122956, 0.09865399375980119, 0.10006491716837283, 0.10176072948516193, 0.10345654180195102, 0.10515235411874012, 0.10684816643552922, 0.1085439787523183, 0.1102397910691074, 0.1119356033858965, 0.11363141570268559, 0.11532722801947469, 0.11702304033626379, 0.11871885265305288, 0.12041466496984198, 0.12211047728663108, 0.12380628960342016, 0.12550210192020927, 0.12719791423699836, 0.12889372655378745, 0.13058953887057656, 0.13228535118736565, 0.13398116350415473, 0.13737278813773293, 0.14076441277131113, 0.1441560374048893, 0.1475476620384675, 0.1509392866720457, 0.15433091130562387, 0.16006606223189163, 0.16580121315815935, 0.1686687886212932, 0.17153636408442707, 0.17440393954756095, 0.17727151501069482, 0.1801390904738287, 0.18300666593696255, 0.1858742414000964, 0.18874181686323027, 0.19447696778949802, 0.20021211871576575, 0.20644210013042646, 0.21267208154508715, 0.21890206295974787, 0.2251320443744086, 0.23136202578906928, 0.23759200720373, 0.24382198861839072, 0.2500519700330514, 0.2730259372838398, 0.29599990453462816, 0.3083151650025936, 0.320630425470559, 0.33294568593852436, 0.3452609464064898, 0.35952938338950047, 0.3737978203725111, 0.3880662573555217, 0.4023346943385323, 0.42769490022477685, 0.45305510611102146, 0.47529372132562075, 0.49753233654022, 0.5217783965977698, 0.5460244566553195, 0.5704146177040659, 0.5948047787528121, 0.6487376626244042, 0.7000534407448434, 0.7534099435113505, 0.7983247536668452, 0.8531606459863641, 0.9047080021287085, 0.9872908640160599]
  end

  @testset "1/x on [0, 1]" begin
    f(x) = 1/x
    xs = adapted_grid(f, (0.0, 1.0))
    @test xs == [7.629546109209478e-5, 0.00015259092218418955, 0.00022888638327628433, 0.0003051818443683791, 0.0003814773054604739, 0.00045777276655256866, 0.0005340682276446635, 0.0006103636887367582, 0.0006866591498288529, 0.0007629546109209478, 0.0008392500720130426, 0.0009155455331051373, 0.001068136455289327, 0.0012207273774735164, 0.0013733182996577059, 0.0015259092218418955, 0.0016785001440260852, 0.0018310910662102746, 0.002136272910578654, 0.002441454754947033, 0.0027466365993154118, 0.003051818443683791, 0.0033570002880521704, 0.0036621821324205493, 0.004272545821157308, 0.004882909509894066, 0.0054932731986308235, 0.006103636887367582, 0.006714000576104341, 0.007324364264841099, 0.007934727953577858, 0.008545091642314616, 0.009155455331051374, 0.009765819019788131, 0.015409512654074675, 0.021053206288361218, 0.02669689992264776, 0.032340593556934305, 0.043627980825507395, 0.054915368094080486, 0.07749014263122667, 0.10006491716837283, 0.12719791423699836, 0.15433091130562387, 0.17727151501069482, 0.20021211871576575, 0.2500519700330514, 0.29599990453462816, 0.3452609464064898, 0.4023346943385323, 0.45305510611102146, 0.49753233654022, 0.5460244566553195, 0.5948047787528121, 0.6487376626244042, 0.7000534407448434, 0.7534099435113505, 0.7983247536668452, 0.8531606459863641, 0.9047080021287085, 0.9872908640160599]
  end

  @testset "abs(x) on [-1, 1]" begin
    xs = adapted_grid(abs, (-1.0, 1.0))
    @test xs == [-0.9804683619604238, -0.7998701656632544, -0.6913381773887523, -0.5995757625684686, -0.49989605993389713, -0.4080001909307437, -0.3094781071870203, -0.19533061132293544, -0.1446101995504463, -0.09388978777795712, -0.07165117256335783, -0.049412557348758546, -0.027173942134159263, -0.004935326919559979, 0.007187703109214885, 0.019310733137989748, 0.03143376316676461, 0.04355679319553948, 0.0678028532530892, 0.09204891331063893, 0.14082923540813155, 0.1896095575056242, 0.29747532524880854, 0.4001068814896869, 0.5068198870227011, 0.5966495073336903, 0.7063212919727282, 0.809416004257417, 0.9745817280321198]
  end
end

