c_tlm_test.h
Go to the documentation of this file.
1 /*
2  * SocialLedge.com - Copyright (C) 2013
3  *
4  * This file is part of free software framework for embedded processors.
5  * You can use it and/or distribute it as long as this copyright header
6  * remains unmodified. The code is free for personal use and requires
7  * permission to use in a commercial product.
8  *
9  * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
10  * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
12  * I SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
13  * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
14  *
15  * You can reach the author of this software at :
16  * p r e e t . w i k i @ g m a i l . c o m
17  */
18 
19 #ifndef C_TLM_TEST_H_
20 #define C_TLM_TEST_H_
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 #if TESTING
25 
26 #include "c_tlm_stream.h"
27 #include "c_tlm_comp.h"
28 #include "c_tlm_var.h"
29 #include <assert.h>
30 #include <string.h>
31 
32 static void string_stream(const char* s, void *arg) {
33  strcat((char*)arg, s);
34 }
35 
36 static bool test_tlm(void)
37 {
38  long long int a = 0xAA00DEADBEEF0055;
39  int b = 1;
40  short c = 2;
41  char d = 3;
42  float f = 0;
43 
44  puts("Test: Add 1 component");
45  tlm_component *first_comp = NULL;
46  assert(0 != (first_comp = tlm_component_add("first")));
47 
48  char str_stream[128] = { 0 };
49  tlm_stream_one(first_comp, string_stream, str_stream);
50  assert(0 == strcmp(str_stream, "START:first:0\nEND:first\n"));
51 
52  puts("Test: Add 2 components");
53  tlm_component* second_comp = tlm_component_add("second");
54  assert(0 != second_comp);
55  assert(0 == tlm_component_add("second"));
56 
57  assert(0 == tlm_component_get_by_name("null"));
58  assert(second_comp == tlm_component_get_by_name("second"));
59 
60 
61  puts("Test: Add 3rd component with large data");
62  tlm_component* third_comp = tlm_component_add("third");
63  assert(0 != third_comp);
64  assert(0 == tlm_component_add("third"));
65  char large_array1[512] = { 0 };
66  char large_array2[512] = { 0 };
67  char large_array_org[512] = { 0 };
68  for (int i = 0; i < sizeof(large_array1); i++) {
69  large_array1[i] = i;
70  large_array2[i] = i;
71  large_array_org[i] = i;
72  }
73  assert(tlm_variable_register(third_comp, "large_array1", large_array1, sizeof(large_array1), 1, tlm_char));
74  assert(tlm_variable_register(third_comp, "large_array2", large_array2, 1, sizeof(large_array2), tlm_char));
75 
76 
77  puts("Test: Add variable to component");
78  assert(tlm_variable_register(second_comp, "a", &a, sizeof(a), 1, tlm_int));
79  assert(!tlm_variable_register(second_comp, "a", &a, sizeof(a), 1, tlm_int));
80  assert(!tlm_variable_register(second_comp, "a", (void*)1 , sizeof(a), 1, tlm_int));
81  assert(!tlm_variable_register(second_comp, "b", &a, sizeof(a), 1, tlm_int));
82 
83  puts("Test: Register variables to component");
84  assert(TLM_REG_VAR(second_comp, b, tlm_int));
85  assert(TLM_REG_VAR(second_comp, c, tlm_int));
86  assert(TLM_REG_VAR(second_comp, d, tlm_int));
87  assert(TLM_REG_VAR(second_comp, f, tlm_float));
88 
89  puts("Test: Get pointer by name");
90  assert(&b == tlm_variable_get_by_name(second_comp, "b")->data_ptr);
91  assert(NULL == tlm_variable_get_by_name(second_comp, "#"));
92 
93  puts("Test: TLM Stream decode");
94  b = 0;
95  d = 0;
96  char stream_start[100] = "START:second:2\n";
97  char stream_var_a[100] = "b:4:1:0:01,01,00,00";
98  char stream_var_d[100] = "d:1:1:0:11";
99  assert(tlm_stream_decode(stream_start));
100  assert(!tlm_stream_decode(stream_var_a));
101  assert(!tlm_stream_decode(stream_var_d));
102  assert(257 == b);
103  assert(17 == d);
104 
105  puts("Test: TLM Stream to stdio");
106  tlm_stream_all_file(stdout);
107 
108  /* Stream to file, then change values of a and b, and decode
109  * back to the stream and verify old values of a and b.
110  */
111  b = 0x12345;
112  d = 0x15;
113  puts("Test: TLM Stream to file");
114  FILE *file = fopen("test.txt", "w");
115  assert(file);
116  tlm_stream_all_file(file);
117  fclose(file);
118 
119  b = 0;
120  d = 0;
121  memset(large_array1, 0, sizeof(large_array1));
122  memset(large_array2, 0, sizeof(large_array2));
123  file = fopen("test.txt", "r");
124  assert(file);
125  assert(tlm_stream_decode_file(file));
126  assert(0x12345 == b);
127  assert(0x15 == d);
128  assert(0 == memcmp(large_array_org, large_array1, sizeof(large_array_org)));
129  assert(0 == memcmp(large_array_org, large_array2, sizeof(large_array_org)));
130  fclose(file);
131 
132 
133  char s1[42] = "0123456789012345678901234567890123456789";
134  char s2[42] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN";
135  tlm_component* disk = tlm_component_add("disk");
136  TLM_REG_VAR(disk, s1, tlm_string);
137  TLM_REG_VAR(disk, s2, tlm_string);
138 
139  file = fopen("test.txt", "w");
140  assert(file);
141  tlm_stream_one_file(disk, file);
142  fclose(file);
143 
144  memset(s1, 0, sizeof(s1));
145  memset(s2, 0, sizeof(s2));
146 
147  file = fopen("test.txt", "r");
148  assert(file);
149  assert(tlm_stream_decode_file(file));
150  fclose(file);
151 
152  printf("s1 = |%s|\n", s1);
153  printf("s2 = |%s|\n", s2);
154 
155  assert(0 == strcmp(s1, "0123456789012345678901234567890123456789"));
156  assert(0 == strcmp(s2, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN"));
157 
158  return true;
159 }
160 
161 
162 #ifdef __cplusplus
163 }
164 #endif
165 #endif /* TESTING */
166 #endif /* C_TLM_TEST_H_ */
printf("Recorder started: %s\n", uiTraceStart()?"OK":"ERROR")
tlm_component * tlm_component_get_by_name(const char *name)
Definition: c_tlm_comp.c:84
Each real time kernel port consists of three files that contain the core kernel components and are common to every and one or more files that are specific to a particular microcontroller and or compiler The FreeRTOS Source directory contains the three files that are common to every port list c
Definition: readme.txt:2
tlm_component * tlm_component_add(const char *name)
Definition: c_tlm_comp.c:43
void tlm_stream_one(tlm_component *comp, stream_callback_type stream, void *print_ascii, void *arg)
Definition: c_tlm_stream.c:175
void tlm_stream_one_file(tlm_component *comp_ptr, FILE *file)
Definition: c_tlm_stream.c:235
Telemetry stream and decode.
bool tlm_stream_decode_file(FILE *file)
Definition: c_tlm_stream.c:266
bool tlm_variable_register(tlm_component *comp_ptr, const char *name, const void *data_ptr, const uint16_t data_size, const uint16_t arr_size, tlm_type type)
Definition: c_tlm_var.c:55
Definition: c_tlm_var.h:40
#define TLM_REG_VAR(comp, var, type)
Definition: c_tlm_var.h:88
Definition: c_tlm_var.h:45
void tlm_stream_all_file(FILE *file)
Definition: c_tlm_stream.c:256
Definition: c_tlm_var.h:42
const tlm_reg_var_type * tlm_variable_get_by_name(tlm_component *comp_ptr, const char *name)
Definition: c_tlm_var.c:94
Definition: c_tlm_comp.h:51
Definition: c_tlm_var.h:43