mirror of https://gerrit.osmocom.org/libosmocore
I noticed some more issues and it is the easiest to revert and include
the fixed version.
This reverts commit 73377229bb
.
jolly/7bit_ussd
parent
73377229bb
commit
efd2bd691f
@ -1,40 +0,0 @@
|
||||
#ifndef _LOGGINGRB_H
|
||||
#define _LOGGINGRB_H
|
||||
|
||||
/* (C) 2012-2013 by Katerina Barone-Adesi <kat.obsc@gmail.com>
|
||||
* All Rights Reserved
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
/*! \defgroup loggingrb Osmocom ringbuffer-backed logging
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*! \file loggingrb.h
|
||||
*/
|
||||
|
||||
struct log_info;
|
||||
|
||||
size_t log_target_rb_used_size(struct log_target const *target);
|
||||
size_t log_target_rb_avail_size(struct log_target const *target);
|
||||
const char *log_target_rb_get(struct log_target const *target, size_t logindex);
|
||||
struct log_target *log_target_create_rb(size_t size);
|
||||
|
||||
/*! @} */
|
||||
|
||||
#endif /* _LOGGINGRB_H */
|
@ -1,58 +0,0 @@
|
||||
#ifndef _STRRB_H
|
||||
#define _STRRB_H
|
||||
|
||||
/* (C) 2012-2013 by Katerina Barone-Adesi <kat.obsc@gmail.com>
|
||||
* All Rights Reserved
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
*/
|
||||
|
||||
/*! \defgroup osmo_strrb Osmocom ringbuffers for log strings
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*! \file strrb.h
|
||||
* \brief Osmocom string ringbuffer handling routines
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <osmocom/core/talloc.h>
|
||||
|
||||
/*! \brief A structure representing an osmocom string ringbuffer */
|
||||
|
||||
#define RB_MAX_MESSAGE_SIZE 240
|
||||
struct osmo_strrb {
|
||||
uint16_t start; /*!< \brief index of the first slot */
|
||||
uint16_t end; /*!< \brief index of the last slot */
|
||||
uint16_t size; /*!< \brief max number of messages to store */
|
||||
char **buffer; /*!< \brief storage for messages */
|
||||
};
|
||||
|
||||
struct osmo_strrb *osmo_strrb_create(TALLOC_CTX * ctx, size_t rb_size);
|
||||
bool osmo_strrb_is_empty(const struct osmo_strrb *rb);
|
||||
const char *osmo_strrb_get_nth(const struct osmo_strrb *rb,
|
||||
unsigned int string_index);
|
||||
bool _osmo_strrb_is_bufindex_valid(const struct osmo_strrb *rb,
|
||||
unsigned int offset);
|
||||
size_t osmo_strrb_elements(const struct osmo_strrb *rb);
|
||||
int osmo_strrb_add(struct osmo_strrb *rb, const char *data);
|
||||
|
||||
/*! @} */
|
||||
|
||||
#endif /* _STRRB_H */
|
@ -1,98 +0,0 @@
|
||||
/* Ringbuffer-backed logging support code */
|
||||
|
||||
/* (C) 2012-2013 by Katerina Barone-Adesi
|
||||
* All Rights Reserved
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
*/
|
||||
|
||||
/*! \addtogroup logging
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*! \file loggingrb.c */
|
||||
|
||||
#include <osmocom/core/strrb.h>
|
||||
#include <osmocom/core/logging.h>
|
||||
#include <osmocom/core/loggingrb.h>
|
||||
|
||||
static void _rb_output(struct log_target *target,
|
||||
unsigned int level, const char *log)
|
||||
{
|
||||
osmo_strrb_add(target->tgt_rb.rb, log);
|
||||
}
|
||||
|
||||
/*! \brief Return the number of log strings in the osmo_strrb-backed target.
|
||||
* \param[in] target The target to search.
|
||||
*
|
||||
* \return The number of log strings in the osmo_strrb-backed target.
|
||||
*/
|
||||
size_t log_target_rb_used_size(struct log_target const *target)
|
||||
{
|
||||
return osmo_strrb_elements(target->tgt_rb.rb);
|
||||
}
|
||||
|
||||
/*! \brief Return the capacity of the osmo_strrb-backed target.
|
||||
* \param[in] target The target to search.
|
||||
*
|
||||
* Note that this is the capacity (aka max number of messages).
|
||||
* It is not the number of unused message slots.
|
||||
* \return The number of log strings in the osmo_strrb-backed target.
|
||||
*/
|
||||
size_t log_target_rb_avail_size(struct log_target const *target)
|
||||
{
|
||||
struct osmo_strrb *rb = target->tgt_rb.rb;
|
||||
return rb->size - 1;
|
||||
}
|
||||
|
||||
/*! \brief Return the nth log entry in a target.
|
||||
* \param[in] target The target to search.
|
||||
* \param[in] logindex The index of the log entry/error message.
|
||||
*
|
||||
* \return A pointer to the nth message, or NULL if logindex is invalid.
|
||||
*/
|
||||
const char *log_target_rb_get(struct log_target const *target, size_t logindex)
|
||||
{
|
||||
return osmo_strrb_get_nth(target->tgt_rb.rb, logindex);
|
||||
}
|
||||
|
||||
/*! \brief Create a new logging target for ringbuffer-backed logging.
|
||||
* \param[in] size The size of the internal backing osmo_strrb (messages).
|
||||
* \returns A log target in case of success, NULL in case of error.
|
||||
*/
|
||||
struct log_target *log_target_create_rb(size_t size)
|
||||
{
|
||||
struct log_target *target;
|
||||
struct osmo_strrb *rb;
|
||||
|
||||
target = log_target_create();
|
||||
if (!target)
|
||||
return NULL;
|
||||
|
||||
rb = osmo_strrb_create(target, size + 1);
|
||||
if (!rb) {
|
||||
log_target_destroy(target);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
target->tgt_rb.rb = rb;
|
||||
target->type = LOG_TGT_TYPE_STRRB;
|
||||
target->output = _rb_output;
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
/* @} */
|
@ -1,170 +0,0 @@
|
||||
/* Ringbuffer implementation, tailored for logging.
|
||||
* This is a lossy ringbuffer. It keeps up to N of the newest messages,
|
||||
* overwriting the oldest as newer ones come in.
|
||||
*
|
||||
* (C) 2012-2013, Katerina Barone-Adesi <kat.obsc@gmail.com>
|
||||
* All Rights Reserved
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
*/
|
||||
|
||||
/*! \file strrb.c
|
||||
* \brief Lossy string ringbuffer for logging; keeps newest messages.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
|
||||
#include <osmocom/core/strrb.h>
|
||||
|
||||
/* Ringbuffer assumptions, invarients, and notes:
|
||||
* - start is the index of the first used index slot in the ring buffer.
|
||||
* - end is the index of the next index slot in the ring buffer.
|
||||
* - start == end => buffer is empty
|
||||
* - Consequence: the buffer can hold at most size - 1 messages
|
||||
* (if this were not the case, full and empty buffers would be indistinguishable
|
||||
* given the conventions in this implementation).
|
||||
* - Whenever the ringbuffer is full, start is advanced. The second oldest
|
||||
* message becomes unreachable by valid indexes (end is not a valid index)
|
||||
* and the oldest message is overwritten (if there was a message there, which
|
||||
* is the case unless this is the first time the ringbuffer becomes full).
|
||||
*/
|
||||
|
||||
/*! \brief Create an empty, initialized osmo_strrb.
|
||||
* \param[in] ctx The talloc memory context which should own this.
|
||||
* \param[in] rb_size The number of messages the osmo_strrb can hold.
|
||||
* \returns A struct osmo_strrb* on success, NULL in case of error.
|
||||
*
|
||||
* This function creates and initializes a ringbuffer.
|
||||
*/
|
||||
|
||||
struct osmo_strrb *osmo_strrb_create(TALLOC_CTX * ctx, size_t rb_size)
|
||||
{
|
||||
struct osmo_strrb *rb = NULL;
|
||||
unsigned int i;
|
||||
|
||||
rb = talloc_zero(ctx, struct osmo_strrb);
|
||||
if (!rb)
|
||||
goto alloc_error;
|
||||
|
||||
/* start and end are zero already, which is correct */
|
||||
rb->size = rb_size;
|
||||
|
||||
rb->buffer = talloc_array(rb, char *, rb->size);
|
||||
if (!rb->buffer)
|
||||
goto alloc_error;
|
||||
for (i = 0; i < rb->size; i++) {
|
||||
rb->buffer[i] =
|
||||
talloc_zero_size(rb->buffer, RB_MAX_MESSAGE_SIZE);
|
||||
if (!rb->buffer[i])
|
||||
goto alloc_error;
|
||||
}
|
||||
|
||||
return rb;
|
||||
|
||||
alloc_error: /* talloc_free(NULL) is safe */
|
||||
talloc_free(rb);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*! \brief Check if an osmo_strrb is empty.
|
||||
* \param[in] rb The osmo_strrb to check.
|
||||
* \returns True if the osmo_strrb is empty, false otherwise.
|
||||
*/
|
||||
bool osmo_strrb_is_empty(const struct osmo_strrb *rb)
|
||||
{
|
||||
return rb->end == rb->start;
|
||||
}
|
||||
|
||||
/*! \brief Return a pointer to the Nth string in the osmo_strrb.
|
||||
* \param[in] rb The osmo_strrb to search.
|
||||
* \param[in] string_index The index sought (N), zero-indexed.
|
||||
*
|
||||
* Return a pointer to the Nth string in the osmo_strrb.
|
||||
* Return NULL if there is no Nth string.
|
||||
* Note that N is zero-indexed.
|
||||
* \returns A pointer to the target string on success, NULL in case of error.
|
||||
*/
|
||||
const char *osmo_strrb_get_nth(const struct osmo_strrb *rb,
|
||||
unsigned int string_index)
|
||||
{
|
||||
unsigned int offset = string_index + rb->start;
|
||||
|
||||
if ((offset >= rb->size) && (rb->start > rb->end))
|
||||
offset -= rb->size;
|
||||
if (_osmo_strrb_is_bufindex_valid(rb, offset))
|
||||
return rb->buffer[offset];
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool _osmo_strrb_is_bufindex_valid(const struct osmo_strrb *rb,
|
||||
unsigned int bufi)
|
||||
{
|
||||
if (osmo_strrb_is_empty(rb))
|
||||
return 0;
|
||||
if ((bufi >= rb->size) || (bufi < 0))
|
||||
return 0;
|
||||
if (rb->start < rb->end)
|
||||
return (bufi >= rb->start) && (bufi < rb->end);
|
||||
return (bufi < rb->end) || (bufi >= rb->start);
|
||||
}
|
||||
|
||||
/*! \brief Count the number of log messages in an osmo_strrb.
|
||||
* \param[in] rb The osmo_strrb to count the elements of.
|
||||
*
|
||||
* \returns The number of log messages in the osmo_strrb.
|
||||
*/
|
||||
size_t osmo_strrb_elements(const struct osmo_strrb *rb)
|
||||
{
|
||||
if (rb->end < rb->start)
|
||||
return rb->end + (rb->size - rb->start);
|
||||
|
||||
return rb->end - rb->start;
|
||||
}
|
||||
|
||||
/*! \brief Add a string to the osmo_strrb.
|
||||
* \param[in] rb The osmo_strrb to add to.
|
||||
* \param[in] data The string to add.
|
||||
*
|
||||
* Add a message to the osmo_strrb.
|
||||
* Older messages will be overwritten as necessary.
|
||||
* \returns 0 normally, 1 as a warning (ie, if data was truncated).
|
||||
*/
|
||||
int osmo_strrb_add(struct osmo_strrb *rb, const char *data)
|
||||
{
|
||||
size_t len = strlen(data);
|
||||
int ret = 0;
|
||||
|
||||
if (len >= RB_MAX_MESSAGE_SIZE) {
|
||||
len = RB_MAX_MESSAGE_SIZE - 1;
|
||||
ret = 1;
|
||||
}
|
||||
|
||||
memcpy(rb->buffer[rb->end], data, len);
|
||||
rb->buffer[rb->end][len] = '\0';
|
||||
|
||||
rb->end += 1;
|
||||
rb->end %= rb->size;
|
||||
|
||||
/* The buffer is full; oldest message is forgotten - see notes above */
|
||||
if (rb->end == rb->start) {
|
||||
rb->start += 1;
|
||||
rb->start %= rb->size;
|
||||
}
|
||||
return ret;
|
||||
}
|
@ -1,83 +0,0 @@
|
||||
/* simple test for the debug interface */
|
||||
/*
|
||||
* (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
|
||||
* (C) 2012-2013 by Katerina Barone-Adesi <kat.obsc@gmail.com>
|
||||
* All Rights Reserved
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
#include <assert.h>
|
||||
|
||||
#include <osmocom/core/logging.h>
|
||||
#include <osmocom/core/utils.h>
|
||||
#include <osmocom/core/ringb.h>
|
||||
#include <osmocom/vty/logging_rbvty.h>
|
||||
|
||||
enum {
|
||||
DRLL,
|
||||
DCC,
|
||||
DMM,
|
||||
};
|
||||
|
||||
static const struct log_info_cat default_categories[] = {
|
||||
[DRLL] = {
|
||||
.name = "DRLL",
|
||||
.description = "A-bis Radio Link Layer (RLL)",
|
||||
.color = "\033[1;31m",
|
||||
.enabled = 1, .loglevel = LOGL_NOTICE,
|
||||
},
|
||||
[DCC] = {
|
||||
.name = "DCC",
|
||||
.description = "Layer3 Call Control (CC)",
|
||||
.color = "\033[1;32m",
|
||||
.enabled = 1, .loglevel = LOGL_NOTICE,
|
||||
},
|
||||
[DMM] = {
|
||||
.name = NULL,
|
||||
.description = "Layer3 Mobility Management (MM)",
|
||||
.color = "\033[1;33m",
|
||||
.enabled = 1, .loglevel = LOGL_NOTICE,
|
||||
},
|
||||
};
|
||||
|
||||
const struct log_info log_info = {
|
||||
.cat = default_categories,
|
||||
.num_cat = ARRAY_SIZE(default_categories),
|
||||
};
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct log_target *ringbuf_target;
|
||||
|
||||
log_init(&log_info, NULL);
|
||||
ringbuf_target = log_target_create_rbvty(NULL, 0x1000);
|
||||
log_add_target(ringbuf_target);
|
||||
log_set_all_filter(ringbuf_target, 1);
|
||||
log_set_print_filename(ringbuf_target, 0);
|
||||
|
||||
log_parse_category_mask(ringbuf_target, "DRLL:DCC");
|
||||
log_parse_category_mask(ringbuf_target, "DRLL");
|
||||
DEBUGP(DCC, "You should not see this\n");
|
||||
|
||||
log_parse_category_mask(ringbuf_target, "DRLL:DCC");
|
||||
DEBUGP(DRLL, "You should see this\n");
|
||||
DEBUGP(DCC, "You should see this\n");
|
||||
DEBUGP(DMM, "You should not see this\n");
|
||||
fprintf(stderr, ringbuffer_get_nth(ringbuf_target->tgt_rbvty.rb, 0));
|
||||
fprintf(stderr, ringbuffer_get_nth(ringbuf_target->tgt_rbvty.rb, 1));
|
||||
assert(!ringbuffer_get_nth(ringbuf_target->tgt_rbvty.rb, 2));
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,225 +0,0 @@
|
||||
/* (C) 2012-2013 by Katerina Barone-Adesi <kat.obsc@gmail.com>
|
||||
* All Rights Reserved
|
||||
*
|
||||
* This program is iree 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.
|
||||
*
|
||||
* This program 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, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <osmocom/core/strrb.h>
|
||||
#include <osmocom/core/talloc.h>
|
||||
#include <osmocom/core/logging.h>
|
||||
|
||||
struct osmo_strrb *rb0, *rb1, *rb2, *rb3, *rb4, *rb5;
|
||||
|
||||
#define STR0 "hello"
|
||||
#define STR1 "a"
|
||||
#define STR2 "world"
|
||||
#define STR3 "sky"
|
||||
#define STR4 "moon"
|
||||
|
||||
#define TESTSIZE 2
|
||||
|
||||
void init_rbs(void)
|
||||
{
|
||||
rb0 = osmo_strrb_create(NULL, TESTSIZE);
|
||||
|
||||
rb1 = osmo_strrb_create(NULL, TESTSIZE);
|
||||
osmo_strrb_add(rb1, STR0);
|
||||
|
||||
rb2 = osmo_strrb_create(NULL, TESTSIZE);
|
||||
osmo_strrb_add(rb2, STR0);
|
||||
osmo_strrb_add(rb2, STR1);
|
||||
|
||||
rb3 = osmo_strrb_create(NULL, TESTSIZE);
|
||||
osmo_strrb_add(rb3, STR0);
|
||||
osmo_strrb_add(rb3, STR1);
|
||||
osmo_strrb_add(rb3, STR2);
|
||||
|
||||
rb4 = osmo_strrb_create(NULL, TESTSIZE);
|
||||
osmo_strrb_add(rb4, STR0);
|
||||
osmo_strrb_add(rb4, STR1);
|
||||
osmo_strrb_add(rb4, STR2);
|
||||
osmo_strrb_add(rb4, STR3);
|
||||
|
||||
rb5 = osmo_strrb_create(NULL, TESTSIZE);
|
||||
osmo_strrb_add(rb5, STR0);
|
||||
osmo_strrb_add(rb5, STR1);
|
||||
osmo_strrb_add(rb5, STR2);
|
||||
osmo_strrb_add(rb5, STR3);
|
||||
osmo_strrb_add(rb5, STR4);
|
||||
}
|
||||
|
||||
void free_rbs(void)
|
||||
{
|
||||
talloc_free(rb0);
|
||||
talloc_free(rb1);
|
||||
talloc_free(rb2);
|
||||
talloc_free(rb3);
|
||||
talloc_free(rb4);
|
||||
talloc_free(rb5);
|
||||
}
|
||||
|
||||
void test_offset_valid(void)
|
||||
{
|
||||
assert(_osmo_strrb_is_bufindex_valid(rb1, 0));
|
||||
assert(!_osmo_strrb_is_bufindex_valid(rb1, 1));
|
||||
assert(!_osmo_strrb_is_bufindex_valid(rb1, 2));
|
||||
|
||||
assert(!_osmo_strrb_is_bufindex_valid(rb3, 0));
|
||||
assert(_osmo_strrb_is_bufindex_valid(rb3, 1));
|
||||
assert(_osmo_strrb_is_bufindex_valid(rb3, 2));
|
||||
|
||||
assert(_osmo_strrb_is_bufindex_valid(rb4, 0));
|
||||
assert(!_osmo_strrb_is_bufindex_valid(rb4, 1));
|
||||
assert(_osmo_strrb_is_bufindex_valid(rb4, 2));
|
||||
|
||||
assert(_osmo_strrb_is_bufindex_valid(rb5, 0));
|
||||
assert(_osmo_strrb_is_bufindex_valid(rb5, 1));
|
||||
assert(!_osmo_strrb_is_bufindex_valid(rb5, 2));
|
||||
}
|
||||
|
||||
void test_elems(void)
|
||||
{
|
||||
assert(osmo_strrb_elements(rb0) == 0);
|
||||
assert(osmo_strrb_elements(rb1) == 1);
|
||||
assert(osmo_strrb_elements(rb2) == 2);
|
||||
assert(osmo_strrb_elements(rb3) == 2);
|
||||
}
|
||||
|
||||
void test_getn(void)
|
||||
{
|
||||
assert(!osmo_strrb_get_nth(rb0, 0));
|
||||
assert(!strcmp(STR0, osmo_strrb_get_nth(rb2, 0)));
|
||||
assert(!strcmp(STR1, osmo_strrb_get_nth(rb2, 1)));
|
||||
assert(!strcmp(STR1, osmo_strrb_get_nth(rb3, 0)));
|
||||
assert(!strcmp(STR2, osmo_strrb_get_nth(rb3, 1)));
|
||||
assert(!osmo_strrb_get_nth(rb3, 2));
|
||||
}
|
||||
|
||||
void test_getn_wrap(void)
|
||||
{
|
||||
assert(!strcmp(STR2, osmo_strrb_get_nth(rb4, 0)));
|
||||
assert(!strcmp(STR3, osmo_strrb_get_nth(rb4, 1)));
|
||||
|
||||
assert(!strcmp(STR3, osmo_strrb_get_nth(rb5, 0)));
|
||||
assert(!strcmp(STR4, osmo_strrb_get_nth(rb5, 1)));
|
||||
}
|
||||
|
||||
void test_add(void)
|
||||
{
|
||||
struct osmo_strrb *rb = osmo_strrb_create(NULL, 3);
|
||||
assert(rb->start == 0);
|
||||
assert(rb->end == 0);
|
||||
|
||||
osmo_strrb_add(rb, "a");
|
||||
osmo_strrb_add(rb, "b");
|
||||
osmo_strrb_add(rb, "c");
|
||||
assert(rb->start == 0);
|
||||
assert(rb->end == 3);
|
||||
assert(osmo_strrb_elements(rb) == 3);
|
||||
|
||||
osmo_strrb_add(rb, "d");
|
||||
assert(rb->start == 1);
|
||||
assert(rb->end == 0);
|
||||
assert(osmo_strrb_elements(rb) == 3);
|
||||
assert(!strcmp("b", osmo_strrb_get_nth(rb, 0)));
|
||||
assert(!strcmp("c", osmo_strrb_get_nth(rb, 1)));
|
||||
assert(!strcmp("d", osmo_strrb_get_nth(rb, 2)));
|
||||
|
||||
osmo_strrb_add(rb, "e");
|
||||
assert(rb->start == 2);
|
||||
assert(rb->end == 1);
|
||||
assert(!strcmp("c", osmo_strrb_get_nth(rb, 0)));
|
||||
assert(!strcmp("d", osmo_strrb_get_nth(rb, 1)));
|
||||
assert(!strcmp("e", osmo_strrb_get_nth(rb, 2)));
|
||||
|
||||
osmo_strrb_add(rb, "f");
|
||||
assert(rb->start == 3);
|
||||
assert(rb->end == 2);
|
||||
assert(!strcmp("d", osmo_strrb_get_nth(rb, 0)));
|
||||
assert(!strcmp("e", osmo_strrb_get_nth(rb, 1)));
|
||||
assert(!strcmp("f", osmo_strrb_get_nth(rb, 2)));
|
||||
|
||||
osmo_strrb_add(rb, "g");
|
||||
assert(rb->start == 0);
|
||||
assert(rb->end == 3);
|
||||
assert(!strcmp("e", osmo_strrb_get_nth(rb, 0)));
|
||||
assert(!strcmp("f", osmo_strrb_get_nth(rb, 1)));
|
||||
assert(!strcmp("g", osmo_strrb_get_nth(rb, 2)));
|
||||
|
||||
osmo_strrb_add(rb, "h");
|
||||
assert(rb->start == 1);
|
||||
assert(rb->end == 0);
|
||||
assert(!strcmp("f", osmo_strrb_get_nth(rb, 0)));
|
||||
assert(!strcmp("g", osmo_strrb_get_nth(rb, 1)));
|
||||
assert(!strcmp("h", osmo_strrb_get_nth(rb, 2)));
|
||||
|
||||
talloc_free(rb);
|
||||
}
|
||||
|
||||
void test_long_msg(void)
|
||||
{
|
||||
struct osmo_strrb *rb = osmo_strrb_create(NULL, 2);
|
||||
int test_size = RB_MAX_MESSAGE_SIZE + 7;
|
||||
char *tests1, *tests2;
|
||||
const char *rb_content;
|
||||
int i;
|
||||
|
||||
tests1 = malloc(test_size);
|
||||
tests2 = malloc(test_size);
|
||||
/* Be certain allocating memory worked before continuing */
|
||||
assert(tests1);
|
||||
assert(tests2);
|
||||
|
||||
for (i = 0; i < RB_MAX_MESSAGE_SIZE; i += 2) {
|
||||
tests1[i] = 'a';
|
||||
tests1[i + 1] = 'b';
|
||||
}
|
||||
tests1[i] = '\0';
|
||||
|
||||
osmo_strrb_add(rb, tests1);
|
||||
strcpy(tests2, tests1);
|
||||
|
||||
/* Verify that no stale data from test1 is lingering... */
|
||||
bzero(tests1, test_size);
|
||||
free(tests1);
|
||||
|
||||
rb_content = osmo_strrb_get_nth(rb, 0);
|
||||
assert(!strncmp(tests2, rb_content, RB_MAX_MESSAGE_SIZE - 1));
|
||||
assert(!rb_content[RB_MAX_MESSAGE_SIZE - 1]);
|
||||
assert(strlen(rb_content) == RB_MAX_MESSAGE_SIZE - 1);
|
||||
|
||||
free(tests2);
|
||||
talloc_free(rb);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
init_rbs();
|
||||
test_offset_valid();
|
||||
test_elems();
|
||||
test_getn();
|
||||
test_getn_wrap();
|
||||
test_add();
|
||||
test_long_msg();
|
||||
printf("All tests passed\n");
|
||||
|
||||
free_rbs();
|
||||
return 0;
|
||||
}
|
@ -1 +0,0 @@
|
||||
All tests passed
|
Loading…
Reference in new issue