18fe3baeb2299ddf4d12118ae45d2aca15741d8a
[platform/core/system/dlog.git] / src / tests / sort_vector_neg.c
1 /*
2  * Copyright (c) 2019-2020, Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // C
18 #include <assert.h>
19
20 // DLog
21 #include <log_file.h>
22 #include <sort_vector.h>
23
24 size_t total_flushed;
25
26 static bool fail_calloc;
27 void *__real_calloc(size_t nmemb, size_t size);
28 void *__wrap_calloc(size_t nmemb, size_t size)
29 {
30         return fail_calloc ? NULL : __real_calloc(nmemb, size);
31 }
32
33 void fail_calloc_test()
34 {
35         struct sort_vector sv;
36         sort_vector_init(&sv);
37
38         struct log_config conf = { .begin = NULL, .last = NULL};
39         sort_vector_apply_config(&sv, &conf);
40
41         sv.size = 99;
42
43         fail_calloc = true;
44         assert(sort_vector_finalize(&sv) == 0);
45         fail_calloc = false;
46 }
47
48 void empty_timestamp()
49 {
50         struct sort_vector sv;
51         sort_vector_init(&sv);
52
53         struct log_config conf = { .begin = NULL, .last = NULL};
54         sort_vector_apply_config(&sv, &conf);
55
56         sv.size = 2;
57         sv.sort_by = DLOGUTIL_SORT_SENT_MONO;
58
59         dlogutil_entry_s log;
60         log.sec_sent_mono = 0;
61         log.nsec_sent_mono = -1;
62
63         sort_vector_finalize(&sv);
64         sort_vector_push(&sv, &log);
65         assert(sort_vector_time_span(&sv, (struct timespec) { .tv_nsec = -1, }) == -1);
66 }
67
68 void logs_order_empty()
69 {
70         struct sort_vector sv;
71         sort_vector_init(&sv);
72
73         struct log_config conf = { .begin = NULL, .last = NULL};
74         sort_vector_apply_config(&sv, &conf);
75
76         sv.size = 4;
77         sv.sort_by = DLOGUTIL_SORT_SENT_MONO;
78
79         dlogutil_entry_s log_A = {
80                 .sec_sent_mono = 0,
81                 .nsec_sent_mono = -1,
82                 .pid = 123,
83         }, log_B = {
84                 .sec_sent_mono = 0,
85                 .nsec_sent_mono = -1,
86                 .pid = 456,
87         }, log_C = {
88                 .sec_sent_mono = 0,
89                 .nsec_sent_mono = -1,
90                 .pid = 789,
91         };
92
93         sort_vector_finalize(&sv);
94         sort_vector_push(&sv, &log_A);
95         sort_vector_push(&sv, &log_B);
96         sort_vector_push(&sv, &log_C);
97
98         dlogutil_entry_s *first = sort_vector_pop_ret(&sv);
99         assert(first == &log_A);
100         assert(first->pid == 123);
101
102         dlogutil_entry_s *second = sort_vector_pop_ret(&sv);
103         assert(second == &log_B);
104         assert(second->pid == 456);
105
106         dlogutil_entry_s *third = sort_vector_pop_ret(&sv);
107         assert(third == &log_C);
108         assert(third->pid == 789);
109 }
110
111 void logs_order_mix()
112 {
113         struct sort_vector sv;
114         sort_vector_init(&sv);
115
116         struct log_config conf = { .begin = NULL, .last = NULL};
117         sort_vector_apply_config(&sv, &conf);
118
119         sv.size = 5;
120         sv.sort_by = DLOGUTIL_SORT_SENT_MONO;
121
122         dlogutil_entry_s log_A = {
123                 .sec_sent_mono = 2,
124                 .nsec_sent_mono = 0,
125                 .pid = 123,
126         }, log_B = {
127                 .sec_sent_mono = 0,
128                 .nsec_sent_mono = -1,
129                 .pid = 456,
130         }, log_C = {
131                 .sec_sent_mono = 3,
132                 .nsec_sent_mono = 0,
133                 .pid = 789,
134         }, log_D = {
135                 .sec_sent_mono = 1,
136                 .nsec_sent_mono = 0,
137                 .pid = 848,
138         };
139
140         sort_vector_finalize(&sv);
141         sort_vector_push(&sv, &log_A);
142         sort_vector_push(&sv, &log_B);
143         sort_vector_push(&sv, &log_C);
144         sort_vector_push(&sv, &log_D);
145
146         dlogutil_entry_s *first = sort_vector_pop_ret(&sv);
147         assert(first == &log_A);
148         assert(first->pid == 123);
149
150         dlogutil_entry_s *second = sort_vector_pop_ret(&sv);
151         assert(second == &log_B);
152         assert(second->pid == 456);
153
154         dlogutil_entry_s *third = sort_vector_pop_ret(&sv);
155         assert(third == &log_D);
156         assert(third->pid == 848);
157
158         dlogutil_entry_s *fourth = sort_vector_pop_ret(&sv);
159         assert(fourth == &log_C);
160         assert(fourth->pid == 789);
161 }
162
163 int main()
164 {
165         fail_calloc_test();
166         empty_timestamp();
167         logs_order_empty();
168         logs_order_mix();
169 }