utils: Export linear regression calculation as public function
[platform/upstream/gstreamer.git] / tests / check / gst / gstutils.c
index 3a5de64..18ba199 100644 (file)
@@ -1747,6 +1747,212 @@ GST_START_TEST (test_element_link_with_ghost_pads)
 
 GST_END_TEST;
 
+static const GstClockTime times1[] = {
+  257116899087539, 120632754291904,
+  257117935914250, 120633825367344,
+  257119448289434, 120635306141271,
+  257120493671524, 120636384357825,
+  257121550784861, 120637417438878,
+  257123042669403, 120638895344150,
+  257124089184865, 120639971729651,
+  257125545836474, 120641406788243,
+  257127030618490, 120642885914220,
+  257128033712770, 120643888843907,
+  257129081768074, 120644981892002,
+  257130145383845, 120646016376867,
+  257131532530200, 120647389850987,
+  257132578136034, 120648472767247,
+  257134102475722, 120649953785315,
+  257135142994788, 120651028858556,
+  257136585079868, 120652441303624,
+  257137618260656, 120653491627112,
+  257139108694546, 120654963978184,
+  257140644022048, 120656500233068,
+  257141685671825, 120657578510655,
+  257142741238288, 120658610889805,
+  257144243633074, 120660093098060,
+  257145287962271, 120661172901525,
+  257146740596716, 120662591572179,
+  257147757607150, 120663622822179,
+  257149263992401, 120665135578527,
+  257150303719290, 120666176166905,
+  257151355569906, 120667217304601,
+  257152430578406, 120668326099768,
+  257153490501095, 120669360554111,
+  257154512360784, 120670365497960,
+  257155530610577, 120671399006259,
+  257156562091659, 120672432728185,
+  257157945388742, 120673800312414,
+  257159287547073, 120675142444983,
+  257160324912880, 120676215076817,
+  257345408328042, 120861261738196,
+  257346412270919, 120862265613926,
+  257347420532284, 120863278644933,
+  257348431187638, 120864284412754,
+  257349439018028, 120865293110265,
+  257351796217938, 120867651111973,
+  257352803038092, 120868659107578,
+  257354152688899, 120870008594883,
+  257355157088906, 120871011097327,
+  257356162439182, 120872016346348,
+  257357167872040, 120873021656407,
+  257358182440058, 120874048633945,
+  257359198881356, 120875052265538,
+  257100756525466, 120616619282139,
+  257101789337770, 120617655475988,
+  257102816323472, 120618674000157,
+  257103822485250, 120619679005039,
+  257104840760423, 120620710743321,
+  257105859459496, 120621715351476,
+  257106886662470, 120622764942539,
+  257108387497864, 120624244221106,
+  257109428859191, 120625321461096,
+  257110485892785, 120626356892003,
+  257111869872141, 120627726459874,
+  257112915903774, 120628813190830,
+  257114329982208, 120630187061682,
+  257115376666026, 120631271992101
+};
+
+
+static const GstClockTime times2[] = {
+  291678579009762, 162107345029507,
+  291679770464405, 162108597684538,
+  291680972924370, 162109745816863,
+  291682278949629, 162111000577605,
+  291683590706117, 162112357724822,
+  291684792322541, 162113613156950,
+  291685931362506, 162114760556854,
+  291687132156589, 162115909238493,
+  291688265012060, 162117120603240,
+  291689372183047, 162118126279508,
+  291705506022294, 162134329373992,
+  291667914301004, 162096795553658,
+  291668119537668, 162096949051905,
+  291668274671455, 162097049238371,
+  291668429435600, 162097256356719,
+  291668586128535, 162097355689763,
+  291668741306233, 162097565678460,
+  291668893789203, 162097661044916,
+  291669100256555, 162097865694145,
+  291669216417563, 162098069214693,
+  291669836394620, 162098677275530,
+  291669990447821, 162098792601263,
+  291670149426086, 162098916899184,
+  291670300232152, 162099114225621,
+  291670411261917, 162099236784112,
+  291670598483507, 162099402158751,
+  291671716582687, 162100558744122,
+  291672600759788, 162101499326359,
+  291673919988307, 162102751981384,
+  291675174441643, 162104005551939,
+  291676271562197, 162105105252898,
+  291677376345374, 162106195737516
+};
+
+static const GstClockTime times3[] = {
+  291881924291688, 162223997578228,
+  291883318122262, 162224167198360,
+  291884786394838, 162224335172501,
+  291886004374386, 162224503695531,
+  291887224353285, 162224673560021,
+  291888472403367, 162224843760361,
+  291889727977561, 162225014479362,
+  291890989982306, 162225174554558,
+  291892247875763, 162225339753039,
+  291893502163547, 162225673230987,
+  291894711382216, 162225829494101,
+  291895961021506, 162225964530832,
+  291897251690854, 162226127287981,
+  291898508630785, 162226303710406,
+  291899740172868, 162226472478047,
+  291900998878873, 162226637402085,
+  291902334919875, 162226797873245,
+  291903572196610, 162226964352963,
+  291904727342699, 162227125312525,
+  291906071189108, 162228361337153,
+  291907308146005, 162229560625638,
+  291908351925126, 162230604986650,
+  291909396411423, 162231653690543,
+  291910453965348, 162232698550995,
+  291912096870744, 162233475264947,
+  291913234148395, 162233606516855,
+  291915448096576, 162233921145559,
+  291916707748827, 162234047154298,
+  291918737451070, 162234370837425,
+  291919896016205, 162234705504337,
+  291921098663980, 162234872320397,
+  291922315691409, 162235031023366
+};
+
+static const GstClockTime times4[] = {
+  10, 0,
+  20, 20,
+  30, 40,
+  40, 60,
+  50, 80,
+  60, 100
+};
+
+struct test_entry
+{
+  gint n;
+  const GstClockTime *v;
+  GstClockTime expect_internal;
+  GstClockTime expect_external;
+  guint64 expect_num;
+  guint64 expect_denom;
+} times[] = {
+  {
+  32, times1, 257154512360784, 120670380469753, 4052622913376634109,
+        4052799313904261962}, {
+  64, times1, 257359198881356, 120875054227405, 2011895759027682422,
+        2012014931360215503}, {
+  32, times2, 291705506022294, 162134297192792, 2319535707505209857,
+        2321009753483354451}, {
+  32, times3, 291922315691409, 162234934150296, 1370930728180888261,
+        4392719527011673456}, {
+  6, times4, 60, 100, 2, 1}
+};
+
+GST_START_TEST (test_regression)
+{
+  GstClockTime m_num, m_den, internal, external;
+  gdouble r_squared, rate, expect_rate;
+  gint i;
+
+  for (i = 0; i < G_N_ELEMENTS (times); i++) {
+    fail_unless (gst_calculate_linear_regression (times[i].v, NULL, times[i].n,
+            &m_num, &m_den, &external, &internal, &r_squared));
+
+    GST_LOG ("xbase %" G_GUINT64_FORMAT " ybase %" G_GUINT64_FORMAT " rate = %"
+        G_GUINT64_FORMAT " / %" G_GUINT64_FORMAT " = %.10f r_squared %f\n",
+        internal, external, m_num, m_den, (gdouble) (m_num) / (m_den),
+        r_squared);
+
+    /* Require high correlation */
+    fail_unless (r_squared >= 0.9);
+
+    fail_unless (internal == times[i].expect_internal,
+        "Regression params %d fail. internal %" G_GUINT64_FORMAT
+        " != expected %" G_GUINT64_FORMAT, i, internal,
+        times[i].expect_internal);
+    /* Rate must be within 1% tolerance */
+    expect_rate = ((gdouble) (times[i].expect_num) / times[i].expect_denom);
+    rate = ((gdouble) (m_num) / m_den);
+    fail_unless ((expect_rate - rate) >= -0.1 && (expect_rate - rate) <= 0.1,
+        "Regression params %d fail. Rate out of range. Expected %f, got %f",
+        i, expect_rate, rate);
+    fail_unless (external >= times[i].expect_external * 0.99 &&
+        external <= times[i].expect_external * 1.01,
+        "Regression params %d fail. external %" G_GUINT64_FORMAT
+        " != expected %" G_GUINT64_FORMAT, i, external,
+        times[i].expect_external);
+  }
+}
+
+GST_END_TEST;
+
 static Suite *
 gst_utils_suite (void)
 {
@@ -1786,6 +1992,7 @@ gst_utils_suite (void)
 
   tcase_add_test (tc_chain, test_read_macros);
   tcase_add_test (tc_chain, test_write_macros);
+  tcase_add_test (tc_chain, test_regression);
 
   return s;
 }