Blame view

RIOT/tests/unittests/tests-core/tests-core-ringbuffer.c 3.39 KB
fb11e647   vrobic   reseau statique a...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  /*
   * Copyright (C) 2014  René Kijewski  <rene.kijewski@fu-berlin.de>
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this library; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   */
  
  #include "thread.h"
  #include "ringbuffer.h"
  #include "mutex.h"
  
  #include "tests-core.h"
  
  /* (ITERATIONS * (BUF_SIZE + 1)) needs to be <= 127! Otherwise `char` overflows. */
  #define ITERATIONS 15
  #define BUF_SIZE 7
  
  static char stack_get[THREAD_STACKSIZE_DEFAULT];
  
  static char rb_buf[BUF_SIZE];
  static ringbuffer_t rb = RINGBUFFER_INIT(rb_buf);
  static mutex_t mutex;
  static kernel_pid_t pid_add, pid_get;
  
  static void assert_avail(unsigned assumed)
  {
      TEST_ASSERT_EQUAL_INT(assumed, rb.avail);
  }
  
  static void assert_add_one(char to_add, int assumed_result)
  {
      int actual_result = ringbuffer_add_one(&rb, to_add);
      TEST_ASSERT_EQUAL_INT(assumed_result, actual_result);
  }
  
  static void assert_get_one(int assumed_result)
  {
      int actual_result = ringbuffer_get_one(&rb);
      TEST_ASSERT_EQUAL_INT(assumed_result, actual_result);
  }
  
  static void run_add(void)
  {
      char next = 0;
      for (unsigned iteration = 0; iteration < ITERATIONS; ++iteration) {
          mutex_lock(&mutex);
  
          for (unsigned i = 0; i < BUF_SIZE; ++i) {
              assert_avail(i);
              assert_add_one(next, -1);
              assert_avail(i + 1);
              ++next;
          }
  
          /* Overwrite oldest element. It should be returned to us. */
          assert_avail(BUF_SIZE);
          assert_add_one(next, next - BUF_SIZE);
          assert_avail(BUF_SIZE);
          ++next;
  
          thread_wakeup(pid_get);
          mutex_unlock_and_sleep(&mutex);
      }
  
      thread_wakeup(pid_get);
  }
  
  static void *run_get(void *arg)
  {
      (void) arg;
  
      char next = 0;
      for (unsigned iteration = 0; iteration < ITERATIONS; ++iteration) {
          ++next; /* the first element of a stride is always overwritten */
  
          mutex_lock(&mutex);
  
          for (unsigned i = BUF_SIZE; i > 0; --i) {
              assert_avail(i);
              assert_get_one(next);
              assert_avail(i - 1);
              ++next;
          }
  
          assert_avail(0);
          assert_get_one(-1);
          assert_avail(0);
  
          thread_wakeup(pid_add);
          mutex_unlock_and_sleep(&mutex);
      }
  
      return NULL;
  }
  
  static void tests_core_ringbuffer(void)
  {
      pid_add = sched_active_pid;
      pid_get = thread_create(stack_get, sizeof (stack_get),
                              THREAD_PRIORITY_MAIN,
                              THREAD_CREATE_SLEEPING | THREAD_CREATE_STACKTEST,
                              run_get, NULL, "get");
      run_add();
  }
  
  Test *tests_core_ringbuffer_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(tests_core_ringbuffer),
      };
  
      EMB_UNIT_TESTCALLER(ringbuffer_tests, NULL, NULL, fixtures);
  
      return (Test *)&ringbuffer_tests;
  }