aboutsummaryrefslogtreecommitdiffstats
path: root/test/testdyn.c
blob: bd32064e1317daeb0cc168060efb5fe563e6f690 (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
    ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <ch.h>

#include "test.h"

#if CH_USE_DYNAMIC

static msg_t thread(void *p) {

  test_emit_token(*(char *)p);
  return 0;
}

#if CH_USE_HEAP
static char *dyn1_gettest(void) {

  return "Dynamic APIs, threads creation from heap";
}

static void dyn1_execute(void) {
  size_t n, sz;
  void *p1;
  tprio_t prio = chThdGetPriority();

  /* Test skipped if the heap is already fragmented. */
  if ((n = chHeapStatus(&sz)) == 1) {
    /* Starting threads from the heap. */
    threads[0] = chThdCreateFromHeap(THD_WA_SIZE(THREADS_STACK_SIZE),
                                     prio-1, thread, "A");
    threads[1] = chThdCreateFromHeap(THD_WA_SIZE(THREADS_STACK_SIZE),
                                     prio-2, thread, "B");
    /* Allocating the whole heap in order to make the thread creation fail.*/
    (void)chHeapStatus(&n);
    p1 = chHeapAlloc(n);
    threads[2] = chThdCreateFromHeap(THD_WA_SIZE(THREADS_STACK_SIZE),
                                     prio-3, thread, "C");
    chHeapFree(p1);

    test_assert(1, (threads[0] != NULL) &&
                   (threads[1] != NULL) &&
                   (threads[2] == NULL) &&
                   (threads[3] == NULL) &&
                   (threads[4] == NULL),
                   "thread creation failed");

    /* Claiming the memory from terminated threads. */
    test_wait_threads();
    test_assert_sequence(2, "AB");

    /* Heap status checked again.*/
    test_assert(3, chHeapStatus(&n) == 1, "heap fragmented");
    test_assert(4, n == sz, "heap size changed");
  }
}

const struct testcase testdyn1 = {
  dyn1_gettest,
  NULL,
  NULL,
  dyn1_execute
};
#endif /* CH_USE_HEAP */

#if CH_USE_MEMPOOLS
static MemoryPool mp1;

static char *dyn2_gettest(void) {

  return "Dynamic APIs, threads creation from memory pool";
}

static void dyn2_setup(void) {

  chPoolInit(&mp1, THD_WA_SIZE(THREADS_STACK_SIZE));
}

static void dyn2_execute(void) {
  int i;
  tprio_t prio = chThdGetPriority();

  /* Adding the WAs to the pool. */
  for (i = 0; i < 4; i++)
    chPoolFree(&mp1, wa[i]);

  /* Starting threads from the memory pool. */
  threads[0] = chThdCreateFromMemoryPool(&mp1, prio-1, thread, "A");
  threads[1] = chThdCreateFromMemoryPool(&mp1, prio-2, thread, "B");
  threads[2] = chThdCreateFromMemoryPool(&mp1, prio-3, thread, "C");
  threads[3] = chThdCreateFromMemoryPool(&mp1, prio-4, thread, "D");
  threads[4] = chThdCreateFromMemoryPool(&mp1, prio-5, thread, "E");

  test_assert(1, (threads[0] != NULL) &&
                 (threads[1] != NULL) &&
                 (threads[2] != NULL) &&
                 (threads[3] != NULL) &&
                 (threads[4] == NULL),
                 "thread creation failed");

  /* Claiming the memory from terminated threads. */
  test_wait_threads();
  test_assert_sequence(2, "ABCD");

  /* Now the pool must be full again. */
  for (i = 0; i < 4; i++)
    test_assert(3, chPoolAlloc(&mp1) != NULL, "pool list empty");
  test_assert(4, chPoolAlloc(&mp1) == NULL, "pool list not empty");
}

const struct testcase testdyn2 = {
  dyn2_gettest,
  dyn2_setup,
  NULL,
  dyn2_execute
};
#endif /* CH_USE_MEMPOOLS */

#endif /* CH_USE_DYNAMIC */

/*
 * Test sequence for dynamic APIs pattern.
 */
const struct testcase * const patterndyn[] = {
#if CH_USE_DYNAMIC
#if CH_USE_HEAP
  &testdyn1,
#endif
#if CH_USE_MEMPOOLS
  &testdyn2,
#endif
#endif
  NULL
};