55#include <stdlib.h>
66#include <string.h>
77#include <sys/resource.h>
8+ #include <math.h>
89
910#include "common.h"
1011#include "storage_number.h"
@@ -246,7 +247,7 @@ int unit_test_storage()
246247
247248struct feed_values {
248249 unsigned long long microseconds ;
249- calculated_number value ;
250+ collected_number value ;
250251};
251252
252253struct test {
@@ -262,6 +263,9 @@ struct test {
262263 unsigned long result_entries ;
263264 struct feed_values * feed ;
264265 calculated_number * results ;
266+
267+ collected_number * feed2 ;
268+ calculated_number * results2 ;
265269};
266270
267271// --------------------------------------------------------------------------------------------------------------------
@@ -295,7 +299,9 @@ struct test test1 = {
295299 10 , // feed entries
296300 9 , // result entries
297301 test1_feed , // feed
298- test1_results // results
302+ test1_results , // results
303+ NULL , // feed2
304+ NULL // results2
299305};
300306
301307// --------------------------------------------------------------------------------------------------------------------
@@ -329,7 +335,9 @@ struct test test2 = {
329335 10 , // feed entries
330336 9 , // result entries
331337 test2_feed , // feed
332- test2_results // results
338+ test2_results , // results
339+ NULL , // feed2
340+ NULL // results2
333341};
334342
335343// --------------------------------------------------------------------------------------------------------------------
@@ -362,7 +370,9 @@ struct test test3 = {
362370 10 , // feed entries
363371 9 , // result entries
364372 test3_feed , // feed
365- test3_results // results
373+ test3_results , // results
374+ NULL , // feed2
375+ NULL // results2
366376};
367377
368378// --------------------------------------------------------------------------------------------------------------------
@@ -395,7 +405,9 @@ struct test test4 = {
395405 10 , // feed entries
396406 9 , // result entries
397407 test4_feed , // feed
398- test4_results // results
408+ test4_results , // results
409+ NULL , // feed2
410+ NULL // results2
399411};
400412
401413// --------------------------------------------------------------------------------------------------------------------
@@ -428,7 +440,9 @@ struct test test5 = {
428440 10 , // feed entries
429441 9 , // result entries
430442 test5_feed , // feed
431- test5_results // results
443+ test5_results , // results
444+ NULL , // feed2
445+ NULL // results2
432446};
433447
434448// --------------------------------------------------------------------------------------------------------------------
@@ -467,7 +481,9 @@ struct test test6 = {
467481 16 , // feed entries
468482 4 , // result entries
469483 test6_feed , // feed
470- test6_results // results
484+ test6_results , // results
485+ NULL , // feed2
486+ NULL // results2
471487};
472488
473489// --------------------------------------------------------------------------------------------------------------------
@@ -500,7 +516,9 @@ struct test test7 = {
500516 10 , // feed entries
501517 18 , // result entries
502518 test7_feed , // feed
503- test7_results // results
519+ test7_results , // results
520+ NULL , // feed2
521+ NULL // results2
504522};
505523
506524// --------------------------------------------------------------------------------------------------------------------
@@ -529,7 +547,9 @@ struct test test8 = {
529547 6 , // feed entries
530548 10 , // result entries
531549 test8_feed , // feed
532- test8_results // results
550+ test8_results , // results
551+ NULL , // feed2
552+ NULL // results2
533553};
534554
535555// --------------------------------------------------------------------------------------------------------------------
@@ -568,7 +588,9 @@ struct test test9 = {
568588 16 , // feed entries
569589 4 , // result entries
570590 test9_feed , // feed
571- test9_results // results
591+ test9_results , // results
592+ NULL , // feed2
593+ NULL // results2
572594};
573595
574596// --------------------------------------------------------------------------------------------------------------------
@@ -600,8 +622,131 @@ struct test test10 = {
600622 RRDDIM_INCREMENTAL , // algorithm
601623 10 , // feed entries
602624 7 , // result entries
603- test10_feed , // feed
604- test10_results // results
625+ test10_feed , // feed
626+ test10_results , // results
627+ NULL , // feed2
628+ NULL // results2
629+ };
630+
631+ // --------------------------------------------------------------------------------------------------------------------
632+ // test11
633+
634+ struct feed_values test11_feed [] = {
635+ { 0 , 10 },
636+ { 1000000 , 20 },
637+ { 1000000 , 30 },
638+ { 1000000 , 40 },
639+ { 1000000 , 50 },
640+ { 1000000 , 60 },
641+ { 1000000 , 70 },
642+ { 1000000 , 80 },
643+ { 1000000 , 90 },
644+ { 1000000 , 100 },
645+ };
646+
647+ collected_number test11_feed2 [] = {
648+ 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100
649+ };
650+
651+ calculated_number test11_results [] = {
652+ 50 , 50 , 50 , 50 , 50 , 50 , 50 , 50 , 50
653+ };
654+
655+ calculated_number test11_results2 [] = {
656+ 50 , 50 , 50 , 50 , 50 , 50 , 50 , 50 , 50
657+ };
658+
659+ struct test test11 = {
660+ "test11" , // name
661+ "test percentage-of-incremental-row with equal values" ,
662+ 1 , // update_every
663+ 1 , // multiplier
664+ 1 , // divisor
665+ RRDDIM_PCENT_OVER_DIFF_TOTAL , // algorithm
666+ 10 , // feed entries
667+ 9 , // result entries
668+ test11_feed , // feed
669+ test11_results , // results
670+ test11_feed2 , // feed2
671+ test11_results2 // results2
672+ };
673+
674+ // --------------------------------------------------------------------------------------------------------------------
675+ // test12
676+
677+ struct feed_values test12_feed [] = {
678+ { 0 , 10 },
679+ { 1000000 , 20 },
680+ { 1000000 , 30 },
681+ { 1000000 , 40 },
682+ { 1000000 , 50 },
683+ { 1000000 , 60 },
684+ { 1000000 , 70 },
685+ { 1000000 , 80 },
686+ { 1000000 , 90 },
687+ { 1000000 , 100 },
688+ };
689+
690+ collected_number test12_feed2 [] = {
691+ 10 * 3 , 20 * 3 , 30 * 3 , 40 * 3 , 50 * 3 , 60 * 3 , 70 * 3 , 80 * 3 , 90 * 3 , 100 * 3
692+ };
693+
694+ calculated_number test12_results [] = {
695+ 25 , 25 , 25 , 25 , 25 , 25 , 25 , 25 , 25
696+ };
697+
698+ calculated_number test12_results2 [] = {
699+ 75 , 75 , 75 , 75 , 75 , 75 , 75 , 75 , 75
700+ };
701+
702+ struct test test12 = {
703+ "test12" , // name
704+ "test percentage-of-incremental-row with equal values" ,
705+ 1 , // update_every
706+ 1 , // multiplier
707+ 1 , // divisor
708+ RRDDIM_PCENT_OVER_DIFF_TOTAL , // algorithm
709+ 10 , // feed entries
710+ 9 , // result entries
711+ test12_feed , // feed
712+ test12_results , // results
713+ test12_feed2 , // feed2
714+ test12_results2 // results2
715+ };
716+
717+ // --------------------------------------------------------------------------------------------------------------------
718+ // test13
719+
720+ struct feed_values test13_feed [] = {
721+ { 500000 , 1000 },
722+ { 600000 , 1000 + 600 },
723+ { 200000 , 1600 + 200 },
724+ { 1000000 , 1800 + 1000 },
725+ { 200000 , 2800 + 200 },
726+ { 2000000 , 3000 + 2000 },
727+ { 600000 , 5000 + 600 },
728+ { 400000 , 5600 + 400 },
729+ { 900000 , 6000 + 900 },
730+ { 1000000 , 6900 + 1000 },
731+ };
732+
733+ calculated_number test13_results [] = {
734+ 83.3333300 , 100 , 100 , 100 , 100 , 100 , 100
735+ };
736+
737+ struct test test13 = {
738+ "test13" , // name
739+ "test incremental values updated in short and long durations" ,
740+ 1 , // update_every
741+ 1 , // multiplier
742+ 1 , // divisor
743+ RRDDIM_PCENT_OVER_DIFF_TOTAL , // algorithm
744+ 10 , // feed entries
745+ 7 , // result entries
746+ test13_feed , // feed
747+ test13_results , // results
748+ NULL , // feed2
749+ NULL // results2
605750};
606751
607752// --------------------------------------------------------------------------------------------------------------------
@@ -618,7 +763,12 @@ int run_test(struct test *test)
618763
619764 // create the chart
620765 RRDSET * st = rrdset_create ("netdata" , name , name , "netdata" , NULL , "Unit Testing" , "a value" , 1 , 1 , RRDSET_TYPE_LINE );
621- RRDDIM * rd = rrddim_add (st , "dimension" , NULL , test -> multiplier , test -> divisor , test -> algorithm );
766+ RRDDIM * rd = rrddim_add (st , "dim1" , NULL , test -> multiplier , test -> divisor , test -> algorithm );
767+
768+ RRDDIM * rd2 = NULL ;
769+ if (test -> feed2 )
770+ rd2 = rrddim_add (st , "dim2" , NULL , test -> multiplier , test -> divisor , test -> algorithm );
771+
622772 st -> debug = 1 ;
623773
624774 // feed it with the test data
@@ -627,14 +777,21 @@ int run_test(struct test *test)
627777 if (debug_flags ) fprintf (stderr , "\n\n" );
628778
629779 if (c ) {
630- fprintf (stderr , " > %s: feeding position %lu, after %llu microseconds, with value " CALCULATED_NUMBER_FORMAT " \n" , test -> name , c + 1 , test -> feed [c ].microseconds , test -> feed [ c ]. value );
780+ fprintf (stderr , " > %s: feeding position %lu, after %llu microseconds\n" , test -> name , c + 1 , test -> feed [c ].microseconds );
631781 rrdset_next_usec (st , test -> feed [c ].microseconds );
632782 }
633783 else {
634- fprintf (stderr , " > %s: feeding position %lu with value " CALCULATED_NUMBER_FORMAT "\n" , test -> name , c + 1 , test -> feed [c ].value );
784+ fprintf (stderr , " > %s: feeding position %lu\n" , test -> name , c + 1 );
785+ }
786+
787+ fprintf (stderr , " >> %s with value " COLLECTED_NUMBER_FORMAT "\n" , rd -> name , test -> feed [c ].value );
788+ rrddim_set (st , "dim1" , test -> feed [c ].value );
789+
790+ if (rd2 ) {
791+ fprintf (stderr , " >> %s with value " COLLECTED_NUMBER_FORMAT "\n" , rd2 -> name , test -> feed2 [c ]);
792+ rrddim_set (st , "dim2" , test -> feed2 [c ]);
635793 }
636794
637- rrddim_set (st , "dimension" , test -> feed [c ].value );
638795 rrdset_done (st );
639796
640797 // align the first entry to second boundary
@@ -654,9 +811,19 @@ int run_test(struct test *test)
654811
655812 unsigned long max = (st -> counter < test -> result_entries )?st -> counter :test -> result_entries ;
656813 for (c = 0 ; c < max ; c ++ ) {
657- calculated_number v = unpack_storage_number (rd -> values [c ]), n = test -> results [c ];
658- fprintf (stderr , " %s: checking position %lu, expecting value " CALCULATED_NUMBER_FORMAT ", found " CALCULATED_NUMBER_FORMAT ", %s\n" , test -> name , c + 1 , n , v , (v == n )?"OK" :"### E R R O R ###" );
659- if (v != n ) errors ++ ;
814+ calculated_number v = unpack_storage_number (rd -> values [c ]);
815+ calculated_number n = test -> results [c ];
816+ int same = (roundl (v * 10000000.0 ) == roundl (n * 10000000.0 ))?1 :0 ;
817+ fprintf (stderr , " %s/%s: checking position %lu, expecting value " CALCULATED_NUMBER_FORMAT ", found " CALCULATED_NUMBER_FORMAT ", %s\n" , test -> name , rd -> name , c + 1 , n , v , (same )?"OK" :"### E R R O R ###" );
818+ if (!same ) errors ++ ;
819+
820+ if (rd2 ) {
821+ v = unpack_storage_number (rd2 -> values [c ]);
822+ n = test -> results2 [c ];
823+ same = (roundl (v * 10000000.0 ) == roundl (n * 10000000.0 ))?1 :0 ;
824+ fprintf (stderr , " %s/%s: checking position %lu, expecting value " CALCULATED_NUMBER_FORMAT ", found " CALCULATED_NUMBER_FORMAT ", %s\n" , test -> name , rd2 -> name , c + 1 , n , v , (same )?"OK" :"### E R R O R ###" );
825+ if (!same ) errors ++ ;
826+ }
660827 }
661828
662829 return errors ;
@@ -694,6 +861,15 @@ int run_all_mockup_tests(void)
694861 if (run_test (& test10 ))
695862 return 1 ;
696863
864+ if (run_test (& test11 ))
865+ return 1 ;
866+
867+ if (run_test (& test12 ))
868+ return 1 ;
869+
870+ if (run_test (& test13 ))
871+ return 1 ;
872+
697873 return 0 ;
698874}
699875
0 commit comments