AverageRoller_test.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "memory/Allocator.h"
  16. #include "util/AverageRoller.h"
  17. #include "util/AverageRoller_pvt.h"
  18. #include "util/events/EventBase.h"
  19. #include <stdio.h>
  20. int main()
  21. {
  22. #define ENTRY_COUNT 13
  23. // {seconds,entry}
  24. const uint32_t testData[ENTRY_COUNT][3] =
  25. {
  26. // The entry between the */and/* is the one which was just altered.
  27. // second col1 col2 col3 average
  28. { 0,/* */0/* */, 0},
  29. { 1,/* 0 */1/* */, 0},
  30. { 2,/* 0 1 */2/* */, 1},
  31. { 3,/* */3/* 1 2 */, 2},
  32. { 4,/* 3 */4/* 2 */, 3},
  33. { 5,/* 3 4 */5/* */, 4},
  34. { 6,/* */6/* 4 5 */, 5},
  35. { 7,/* 6 */7/* 5 */, 6},
  36. { 8,/* 6 7 */8/* */, 7},
  37. { 9,/* */9/* 7 8 */, 8},
  38. // 9 */7/* 8 8 skipped a second
  39. {11,/* 9 7 */7/* */, 7},
  40. {12,/* */6/* 7 7 */, 6},
  41. // 6 */7/* 7 6 skipped a second
  42. {14,/* 6 7 */4/* */, 5}
  43. };
  44. const uint32_t windowSeconds = 3;
  45. struct Allocator* allocator = Allocator_new(4096);
  46. struct EventBase* eventBase = EventBase_new(allocator);
  47. struct AverageRoller_pvt* roller =
  48. (struct AverageRoller_pvt*) AverageRoller_new(windowSeconds, eventBase, allocator);
  49. // To make life easy we will pretend it's january first 1970...
  50. roller->lastUpdateTime = 0;
  51. uint32_t ret = 0;
  52. for (uint32_t i = 0; i < ENTRY_COUNT; i++)
  53. {
  54. uint32_t average = AverageRoller_updateAtTime(&roller->pub, testData[i][0], testData[i][1]);
  55. if (average != testData[i][2]) {
  56. printf("For average #%d, expected %d, got %d, float: %f, entryCount: %d\n",
  57. (int) i, (int) testData[i][2], (int) average,
  58. ((float) roller->sum) / roller->entryCount,
  59. (int) roller->entryCount);
  60. ret = 1;
  61. }
  62. }
  63. Allocator_free(allocator);
  64. return ret;
  65. }