Skip to content

Commit

Permalink
feat: add buf test
Browse files Browse the repository at this point in the history
  • Loading branch information
mpolitzer committed Apr 17, 2024
1 parent 676b5ba commit baed41f
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
10 changes: 9 additions & 1 deletion sys-utils/libcmt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,29 @@ install-mock: $(mock_LIB)
unittests_BINS := \
$(mock_OBJDIR)/abi-multi \
$(mock_OBJDIR)/abi-single \
$(mock_OBJDIR)/buf \
$(mock_OBJDIR)/keccak \
$(mock_OBJDIR)/merkle
$(mock_OBJDIR)/merkle \
$(mock_OBJDIR)/rollup

$(mock_OBJDIR)/abi-multi: tests/abi-multi.c $(mock_LIB)
$(CC) $(CFLAGS) -o $@ $^

$(mock_OBJDIR)/abi-single: tests/abi-single.c $(mock_LIB)
$(CC) $(CFLAGS) -o $@ $^

$(mock_OBJDIR)/buf: tests/buf.c $(mock_LIB)
$(CC) $(CFLAGS) -o $@ $^

$(mock_OBJDIR)/keccak: tests/keccak.c $(mock_LIB)
$(CC) $(CFLAGS) -o $@ $^

$(mock_OBJDIR)/merkle: tests/merkle.c $(mock_LIB)
$(CC) $(CFLAGS) -o $@ $^

$(mock_OBJDIR)/rollup: tests/rollup.c $(mock_LIB)
$(CC) $(CFLAGS) -o $@ $^

test: $(unittests_BINS)
$(foreach test,$(unittests_BINS),$(test) &&) true

Expand Down
86 changes: 86 additions & 0 deletions sys-utils/libcmt/tests/buf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* Copyright Cartesi and individual authors (see AUTHORS)
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include "buf.h"

void split_in_bounds_must_succeed()
{
uint8_t _[8];
cmt_buf_t b;
cmt_buf_init(&b, sizeof _, _);
assert(cmt_buf_length(&b) == 8);

{ // everything to lhs
cmt_buf_t lhs;
cmt_buf_t rhs;
assert(cmt_buf_split(&b, 8, &lhs, &rhs) == 0);
assert(cmt_buf_length(&lhs) == 8);
assert(cmt_buf_length(&rhs) == 0);
}

{ // everything to rhs
cmt_buf_t lhs;
cmt_buf_t rhs;
assert(cmt_buf_split(&b, 0, &lhs, &rhs) == 0);
assert(cmt_buf_length(&lhs) == 0);
assert(cmt_buf_length(&rhs) == 8);
}

{ // handle alias (lhs)
cmt_buf_t tmp = b;
cmt_buf_t rhs;
assert(cmt_buf_split(&tmp, 8, &tmp, &rhs) == 0);
assert(cmt_buf_length(&tmp) == 8);
assert(cmt_buf_length(&rhs) == 0);

assert(tmp.begin == b.begin);
}

{ // handle alias (rhs)
cmt_buf_t tmp = b;
cmt_buf_t lhs;
assert(cmt_buf_split(&tmp, 8, &lhs, &tmp) == 0);
assert(cmt_buf_length(&lhs) == 8);
assert(cmt_buf_length(&tmp) == 0);

assert(tmp.begin == b.end);
}
printf("test_buf_split_in_bounds_must_succeed passed\n");
}

void split_out_of_bounds_must_fail(void)
{
uint8_t _[8];
cmt_buf_t b;
cmt_buf_t lhs;
cmt_buf_t rhs;
cmt_buf_init(&b, sizeof _, _);

assert(cmt_buf_split(&b, 9, &lhs, &rhs) == -ENOBUFS);
assert(cmt_buf_split(&b, SIZE_MAX, &lhs, &rhs) == -ENOBUFS);
printf("test_buf_split_out_of_bounds_must_fail passed\n");

}

int main(void)
{
split_in_bounds_must_succeed();
split_out_of_bounds_must_fail();
return 0;
}

0 comments on commit baed41f

Please sign in to comment.