sml_time implemented
This commit is contained in:
parent
3b34cd85dc
commit
352d6997f5
5 changed files with 117 additions and 9 deletions
|
@ -21,18 +21,25 @@
|
|||
#define _SML_TIME_H
|
||||
|
||||
#include "sml_shared.h"
|
||||
#include "sml_number.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define SML_TIME_SEC_INDEX 0x01
|
||||
#define SML_TIME_TIMESTAMP 0x02
|
||||
|
||||
typedef struct {
|
||||
u8 *tag;
|
||||
u32 *data;
|
||||
union {
|
||||
u32 *sec_index;
|
||||
u32 *timestamp;
|
||||
}
|
||||
data;
|
||||
} sml_time;
|
||||
|
||||
// Parses a time, updates the buffer accordingly, memory must be free'd elsewhere.
|
||||
sml_time *sml_time_init();
|
||||
sml_time *sml_time_parse(sml_buffer *buf);
|
||||
void sml_time_write(sml_time *time, sml_buffer *buf);
|
||||
void sml_time_free(sml_time *time);
|
||||
|
|
|
@ -22,8 +22,18 @@
|
|||
#include <sml/sml_number.h>
|
||||
#include <stdio.h>
|
||||
|
||||
sml_time *sml_time_init() {
|
||||
sml_time *t = (sml_time *) malloc(sizeof(sml_time));
|
||||
memset(t, 0, sizeof(sml_time));
|
||||
return t;
|
||||
}
|
||||
|
||||
sml_time *sml_time_parse(sml_buffer *buf) {
|
||||
sml_time *tme = (sml_time *) malloc(sizeof(sml_time));
|
||||
if (sml_buf_optional_is_skipped(buf)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
sml_time *tme = sml_time_init();
|
||||
|
||||
if (sml_buf_get_next_type(buf) != SML_TYPE_LIST) {
|
||||
buf->error = 1;
|
||||
|
@ -38,7 +48,7 @@ sml_time *sml_time_parse(sml_buffer *buf) {
|
|||
tme->tag = sml_u8_parse(buf);
|
||||
if (sml_buf_has_errors(buf)) goto error;
|
||||
|
||||
tme->data = sml_u32_parse(buf);
|
||||
tme->data.timestamp = sml_u32_parse(buf);
|
||||
if (sml_buf_has_errors(buf)) goto error;
|
||||
|
||||
return tme;
|
||||
|
@ -48,19 +58,21 @@ error:
|
|||
return 0;
|
||||
}
|
||||
|
||||
void sml_time_write(sml_time *time, sml_buffer *buf) {
|
||||
if (time == 0) {
|
||||
void sml_time_write(sml_time *t, sml_buffer *buf) {
|
||||
if (t == 0) {
|
||||
sml_buf_optional_write(buf);
|
||||
return;
|
||||
}
|
||||
printf("NYI: %s (writing optional flag instead)\n", __FUNCTION__);
|
||||
sml_buf_optional_write(buf);
|
||||
|
||||
sml_buf_set_type_and_length(buf, SML_TYPE_LIST, 2);
|
||||
sml_u8_write(t->tag, buf);
|
||||
sml_u32_write(t->data.timestamp, buf);
|
||||
}
|
||||
|
||||
void sml_time_free(sml_time *tme) {
|
||||
if (tme) {
|
||||
sml_number_free(tme->tag);
|
||||
sml_number_free(tme->data);
|
||||
sml_number_free(tme->data.timestamp);
|
||||
free(tme);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ OBJS = \
|
|||
src/sml_value_test.o \
|
||||
src/sml_status_test.o \
|
||||
src/sml_list_test.o \
|
||||
src/sml_time_test.o \
|
||||
src/sml_file_test.o \
|
||||
src/sml_message_test.o
|
||||
|
||||
|
|
87
test/src/sml_time_test.c
Normal file
87
test/src/sml_time_test.c
Normal file
|
@ -0,0 +1,87 @@
|
|||
// Copyright 2011 Juri Glass, Mathias Runge, Nadim El Sayed
|
||||
// DAI-Labor, TU-Berlin
|
||||
//
|
||||
// This file is part of libSML.
|
||||
//
|
||||
// libSML 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.
|
||||
//
|
||||
// libSML 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 libSML. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "../unity/unity_fixture.h"
|
||||
#include "test_helper.h"
|
||||
#include <sml/sml_time.h>
|
||||
|
||||
TEST_GROUP(sml_time);
|
||||
|
||||
sml_buffer *buf;
|
||||
|
||||
TEST_SETUP(sml_time) {
|
||||
buf = sml_buffer_init(512);
|
||||
}
|
||||
|
||||
TEST_TEAR_DOWN(sml_time) {
|
||||
sml_buffer_free(buf);
|
||||
}
|
||||
|
||||
TEST(sml_time, init) {
|
||||
sml_time *t = sml_time_init();
|
||||
TEST_ASSERT_NOT_NULL(t);
|
||||
}
|
||||
|
||||
TEST(sml_time, parse_sec_index) {
|
||||
hex2binary("72620165000000FF", sml_buf_get_current_buf(buf));
|
||||
sml_time *t = sml_time_parse(buf);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(t);
|
||||
TEST_ASSERT_EQUAL(SML_TIME_SEC_INDEX, *(t->tag));
|
||||
TEST_ASSERT_EQUAL(8, buf->cursor);
|
||||
}
|
||||
|
||||
TEST(sml_time, parse_timestamp) {
|
||||
hex2binary("72620265000000FF", sml_buf_get_current_buf(buf));
|
||||
sml_time *t = sml_time_parse(buf);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(t);
|
||||
TEST_ASSERT_EQUAL(SML_TIME_TIMESTAMP, *(t->tag));
|
||||
TEST_ASSERT_EQUAL(8, buf->cursor);
|
||||
}
|
||||
|
||||
TEST(sml_time, parse_optional) {
|
||||
hex2binary("01", sml_buf_get_current_buf(buf));
|
||||
sml_time *t = sml_time_parse(buf);
|
||||
|
||||
TEST_ASSERT_NULL(t);
|
||||
TEST_ASSERT_EQUAL(1, buf->cursor);
|
||||
}
|
||||
|
||||
TEST(sml_time, write_sec_index) {
|
||||
sml_time *t = sml_time_init();
|
||||
t->data.sec_index = sml_u32_init(255);
|
||||
t->tag = sml_u8_init(SML_TIME_SEC_INDEX);
|
||||
|
||||
sml_time_write(t, buf);
|
||||
expected_buf(buf, "72620165000000FF", 8);
|
||||
}
|
||||
|
||||
TEST(sml_time, write_optional) {
|
||||
sml_time_write(0, buf);
|
||||
expected_buf(buf, "01", 1);
|
||||
}
|
||||
|
||||
TEST_GROUP_RUNNER(sml_time) {
|
||||
RUN_TEST_CASE(sml_time, init);
|
||||
RUN_TEST_CASE(sml_time, parse_sec_index);
|
||||
RUN_TEST_CASE(sml_time, parse_timestamp);
|
||||
RUN_TEST_CASE(sml_time, parse_optional);
|
||||
RUN_TEST_CASE(sml_time, write_sec_index);
|
||||
RUN_TEST_CASE(sml_time, write_optional);
|
||||
}
|
|
@ -26,6 +26,7 @@ static void runAllTests() {
|
|||
RUN_TEST_GROUP(sml_value);
|
||||
RUN_TEST_GROUP(sml_status);
|
||||
RUN_TEST_GROUP(sml_list);
|
||||
RUN_TEST_GROUP(sml_time);
|
||||
RUN_TEST_GROUP(sml_message);
|
||||
RUN_TEST_GROUP(sml_file);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue