LexLeo 0.0.0-dev+f8e5087-dirty
Technical documentation
Loading...
Searching...
No Matches
stream_fake_provider.c
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-3.0-or-later
2 * Copyright (C) 2026 Sylvain Labopin
3 */
4
21
23
25
27
29
31 fake_stream_t *impl;
33};
34
36 stream_fake_t **out_fake,
37 stream_t **out_stream,
38 const osal_mem_ops_t *mem)
39{
40 if (out_fake) {
41 *out_fake = NULL;
42 }
43 if (out_stream) {
44 *out_stream = NULL;
45 }
46
47 if (!out_fake || !out_stream || !mem) {
49 }
50
51 const stream_env_t env = {
52 .mem = mem
53 };
54
55 fake_stream_t *impl = NULL;
56 stream_t *stream = NULL;
57
58 stream_status_t st = fake_stream_create(&impl, &stream, &env);
59 if (st != STREAM_STATUS_OK) {
60 return st;
61 }
62
63 stream_fake_t *fake =
64 (stream_fake_t *)env.mem->calloc(1u, sizeof(*fake));
65 if (!fake) {
66 fake_stream_destroy(&impl, &stream);
67 return STREAM_STATUS_OOM;
68 }
69
70 fake->impl = impl;
71 fake->mem = mem;
72
73 *out_fake = fake;
74 *out_stream = stream;
75 return STREAM_STATUS_OK;
76}
77
79 stream_fake_t **fake,
80 stream_t **stream)
81{
82 fake_stream_t *impl = NULL;
83
84 if (fake && *fake) {
85 impl = (*fake)->impl;
86
87 LEXLEO_ASSERT((*fake)->mem && (*fake)->mem->free);
88
89 (*fake)->mem->free(*fake);
90 *fake = NULL;
91 }
92
93 fake_stream_destroy(&impl, stream);
94}
95
97{
98 if (!fake || !fake->impl) {
99 return;
100 }
101
102 fake_stream_reset(fake->impl);
103}
104
106 stream_fake_t *fake,
107 size_t n,
108 stream_status_t status)
109{
110 if (!fake || !fake->impl) {
111 return;
112 }
113
114 fake_stream_set_write_result(fake->impl, n, status);
115}
116
118 stream_fake_t *fake,
119 stream_status_t status)
120{
121 if (!fake || !fake->impl) {
122 return;
123 }
124
125 fake_stream_set_flush_result(fake->impl, status);
126}
127
129 stream_fake_t *fake,
130 size_t call_idx,
131 stream_status_t status)
132{
133 if (!fake || !fake->impl) {
134 return;
135 }
136
137 fake_stream_fail_write_since(fake->impl, call_idx, status);
138}
139
141{
142 if (!fake || !fake->impl) {
143 return NULL;
144 }
145
146 return (const stream_fake_counters_t *)fake_stream_counters(fake->impl);
147}
148
150{
151 if (!fake || !fake->impl) {
152 return 0u;
153 }
154
155 return fake_stream_written_len(fake->impl);
156}
157
158const uint8_t *stream_fake_written_data(const stream_fake_t *fake)
159{
160 if (!fake || !fake->impl) {
161 return NULL;
162 }
163
164 return fake_stream_written_data(fake->impl);
165}
#define LEXLEO_ASSERT(expr)
Public environment type for the stream port.
const stream_fake_counters_t * stream_fake_counters(const stream_fake_t *fake)
Return the current call counters recorded by the fake stream.
void stream_fake_set_flush_result(stream_fake_t *fake, stream_status_t status)
Configure the result returned by flush operations.
void stream_fake_destroy(stream_fake_t **fake, stream_t **stream)
Destroy a fake stream backend and its associated public stream.
void stream_fake_fail_write_since(stream_fake_t *fake, size_t call_idx, stream_status_t status)
Make fake writes fail starting from a given call index.
void stream_fake_set_write_result(stream_fake_t *fake, size_t n, stream_status_t status)
Configure the result returned by the next write operations.
void stream_fake_reset(stream_fake_t *fake)
Reset the fake stream runtime state and counters.
size_t stream_fake_written_len(const stream_fake_t *fake)
Return the number of bytes captured by the fake stream.
const uint8_t * stream_fake_written_data(const stream_fake_t *fake)
Return the captured bytes written through the fake stream.
stream_status_t stream_fake_create(stream_fake_t **out_fake, stream_t **out_stream, const osal_mem_ops_t *mem)
Create a fake stream backend and its associated public stream_t.
Fake stream provider used by stream tests.
Internal include shim for the stream fake provider.
stream_status_t
Public status codes used by the stream port.
@ STREAM_STATUS_INVALID
@ STREAM_STATUS_OK
@ STREAM_STATUS_OOM
void *(* calloc)(size_t nmemb, size_t size)
Runtime environment for the stream port.
Definition stream_env.h:35
const osal_mem_ops_t * mem
Memory operations used by the stream port.
Definition stream_env.h:43
Call counters recorded by the fake stream.
fake_stream_t * impl
const osal_mem_ops_t * mem
Private handle structure for a stream_t.