2013-01-13 13:27:17 +01:00
|
|
|
/*
|
|
|
|
* test_assertion.c
|
|
|
|
*
|
|
|
|
* Created on: Jan 13, 2013
|
|
|
|
* Author: hathach
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Software License Agreement (BSD License)
|
2013-03-25 05:46:30 +01:00
|
|
|
* Copyright (c) 2012, hathach (tinyusb.org)
|
2013-01-13 13:27:17 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
|
|
|
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
|
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the tiny usb stack.
|
|
|
|
*/
|
|
|
|
|
2013-01-22 11:41:06 +01:00
|
|
|
#define _TEST_ASSERT_
|
|
|
|
|
2013-01-13 14:00:01 +01:00
|
|
|
#include <stdint.h>
|
2013-01-13 13:27:17 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include "unity.h"
|
2013-01-22 11:41:06 +01:00
|
|
|
#include "CException.h"
|
2013-01-31 05:05:33 +01:00
|
|
|
#include "binary.h"
|
2013-01-13 13:27:17 +01:00
|
|
|
#include "common/assertion.h"
|
2013-01-22 11:41:06 +01:00
|
|
|
#include "errors.h"
|
|
|
|
|
|
|
|
CEXCEPTION_T e;
|
2013-01-13 13:27:17 +01:00
|
|
|
|
|
|
|
void setUp(void)
|
|
|
|
{
|
2013-01-22 11:41:06 +01:00
|
|
|
e = 0;
|
2013-01-13 13:27:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-01-22 11:41:06 +01:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// Error Status
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
void test_assert_status(void)
|
|
|
|
{
|
|
|
|
Try
|
|
|
|
{
|
|
|
|
ASSERT_STATUS(TUSB_ERROR_NONE);
|
|
|
|
ASSERT_STATUS(TUSB_ERROR_INVALID_PARA);
|
|
|
|
}
|
|
|
|
Catch (e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-13 14:00:01 +01:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// Logical
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
void test_assert_logical_true(void)
|
2013-01-13 13:27:17 +01:00
|
|
|
{
|
2013-01-22 11:41:06 +01:00
|
|
|
Try
|
|
|
|
{
|
|
|
|
ASSERT (true , __LINE__);
|
|
|
|
ASSERT_TRUE (true , __LINE__);
|
|
|
|
ASSERT_TRUE (false, 0);
|
|
|
|
}
|
|
|
|
Catch(e)
|
|
|
|
{
|
|
|
|
TEST_ASSERT_EQUAL(0, e);
|
|
|
|
}
|
2013-01-13 14:00:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_assert_logical_false(void)
|
|
|
|
{
|
2013-01-22 11:41:06 +01:00
|
|
|
Try
|
|
|
|
{
|
|
|
|
ASSERT_FALSE (false, __LINE__);
|
|
|
|
ASSERT_FALSE (true , 0);
|
|
|
|
}
|
|
|
|
Catch(e)
|
|
|
|
{
|
|
|
|
TEST_ASSERT_EQUAL(0, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// Pointer
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
void test_assert_pointer_not_null(void)
|
|
|
|
{
|
|
|
|
uint32_t n;
|
|
|
|
|
|
|
|
Try
|
|
|
|
{
|
|
|
|
ASSERT_PTR_NOT_NULL(&n, __LINE__);
|
|
|
|
ASSERT_PTR(&n, __LINE__);
|
|
|
|
ASSERT_PTR(NULL, 0);
|
|
|
|
}
|
|
|
|
Catch(e)
|
|
|
|
{
|
|
|
|
TEST_ASSERT_EQUAL(0, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_assert_pointer_null(void)
|
|
|
|
{
|
|
|
|
uint32_t n;
|
2013-01-13 14:00:01 +01:00
|
|
|
|
2013-01-22 11:41:06 +01:00
|
|
|
Try
|
|
|
|
{
|
|
|
|
ASSERT_PTR_NULL(NULL, __LINE__);
|
|
|
|
ASSERT_PTR_NULL(&n, 0);
|
|
|
|
}
|
|
|
|
Catch(e)
|
|
|
|
{
|
|
|
|
TEST_ASSERT_EQUAL(0, e);
|
|
|
|
}
|
2013-01-13 13:27:17 +01:00
|
|
|
}
|
|
|
|
|
2013-01-13 14:00:01 +01:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// Integer
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
void test_assert_int_eqal(void)
|
2013-01-13 13:27:17 +01:00
|
|
|
{
|
2013-01-22 11:41:06 +01:00
|
|
|
Try
|
|
|
|
{
|
|
|
|
ASSERT_INT (1, 1, __LINE__);
|
|
|
|
ASSERT_INT_EQUAL (1, 1, __LINE__);
|
2013-01-13 14:00:01 +01:00
|
|
|
|
2013-01-22 11:41:06 +01:00
|
|
|
// test side effect
|
|
|
|
uint32_t x = 0;
|
|
|
|
uint32_t y = 0;
|
|
|
|
ASSERT_INT (x++, y++, __LINE__);
|
|
|
|
TEST_ASSERT_EQUAL(1, x);
|
|
|
|
TEST_ASSERT_EQUAL(1, y);
|
2013-01-13 14:00:01 +01:00
|
|
|
|
2013-01-22 11:41:06 +01:00
|
|
|
ASSERT_INT (1, 0, 0);
|
|
|
|
}
|
|
|
|
Catch(e)
|
|
|
|
{
|
|
|
|
TEST_ASSERT_EQUAL(0, e);
|
|
|
|
}
|
2013-01-13 13:27:17 +01:00
|
|
|
}
|
2013-01-13 14:09:51 +01:00
|
|
|
|
2013-01-22 11:41:06 +01:00
|
|
|
void test_assert_int_within(void)
|
2013-01-13 14:09:51 +01:00
|
|
|
{
|
2013-01-22 11:41:06 +01:00
|
|
|
Try
|
|
|
|
{
|
|
|
|
ASSERT_INT_WITHIN (1, 5, 3, __LINE__);
|
|
|
|
ASSERT_INT_WITHIN (1, 5, 1, __LINE__);
|
|
|
|
ASSERT_INT_WITHIN (1, 5, 5, __LINE__);
|
|
|
|
|
|
|
|
ASSERT_INT_WITHIN (1, 5, 0, 0);
|
|
|
|
ASSERT_INT_WITHIN (1, 5, 10, 0);
|
|
|
|
}
|
|
|
|
Catch(e)
|
|
|
|
{
|
|
|
|
TEST_ASSERT_EQUAL(0, e);
|
|
|
|
}
|
2013-01-13 14:26:51 +01:00
|
|
|
}
|
2013-01-13 14:09:51 +01:00
|
|
|
|
2013-01-22 11:41:06 +01:00
|
|
|
void test_assert_int_within_greater(void)
|
2013-01-13 14:26:51 +01:00
|
|
|
{
|
2013-01-22 11:41:06 +01:00
|
|
|
Try
|
|
|
|
{
|
|
|
|
ASSERT_INT_WITHIN (1, 5, 10, 0);
|
|
|
|
}
|
|
|
|
Catch(e)
|
|
|
|
{
|
|
|
|
TEST_ASSERT_EQUAL(0, e);
|
|
|
|
}
|
|
|
|
|
2013-01-13 14:26:51 +01:00
|
|
|
}
|
2013-01-13 14:09:51 +01:00
|
|
|
|
2013-01-22 11:41:06 +01:00
|
|
|
void test_assert_int_within_less(void)
|
2013-01-13 14:26:51 +01:00
|
|
|
{
|
2013-01-22 11:41:06 +01:00
|
|
|
Try
|
|
|
|
{
|
|
|
|
ASSERT_INT_WITHIN (1, 5, 0, 0);
|
|
|
|
}
|
|
|
|
Catch(e)
|
|
|
|
{
|
|
|
|
TEST_ASSERT_EQUAL(0, e);
|
|
|
|
}
|
|
|
|
|
2013-01-13 14:09:51 +01:00
|
|
|
}
|
2013-01-13 14:39:01 +01:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// HEX
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
void test_assert_hex_equal(void)
|
|
|
|
{
|
2013-01-22 11:41:06 +01:00
|
|
|
Try
|
|
|
|
{
|
|
|
|
ASSERT_HEX (0xffee, 0xffee, __LINE__);
|
|
|
|
ASSERT_HEX_EQUAL (0xffee, 0xffee, __LINE__);
|
2013-01-13 14:39:01 +01:00
|
|
|
|
2013-01-22 11:41:06 +01:00
|
|
|
// test side effect
|
|
|
|
uint32_t x = 0xf0f0;
|
|
|
|
uint32_t y = 0xf0f0;
|
|
|
|
ASSERT_HEX (x++, y++, __LINE__);
|
|
|
|
TEST_ASSERT_EQUAL(0xf0f1, x);
|
|
|
|
TEST_ASSERT_EQUAL(0xf0f1, y);
|
2013-01-13 14:39:01 +01:00
|
|
|
|
2013-01-22 11:41:06 +01:00
|
|
|
ASSERT_HEX(0x1234, 0x4321, 0);
|
|
|
|
}
|
|
|
|
Catch(e)
|
|
|
|
{
|
|
|
|
TEST_ASSERT_EQUAL(0, e);
|
|
|
|
}
|
2013-01-13 14:39:01 +01:00
|
|
|
}
|
|
|
|
|
2013-01-22 11:41:06 +01:00
|
|
|
void test_assert_hex_within(void)
|
2013-01-13 14:39:01 +01:00
|
|
|
{
|
2013-01-22 11:41:06 +01:00
|
|
|
Try
|
|
|
|
{
|
|
|
|
ASSERT_HEX_WITHIN (0xff00, 0xffff, 0xff11, __LINE__);
|
|
|
|
ASSERT_HEX_WITHIN (0xff00, 0xffff, 0xff00, __LINE__);
|
|
|
|
ASSERT_HEX_WITHIN (0xff00, 0xffff, 0xffff, __LINE__);
|
|
|
|
}
|
|
|
|
Catch (e)
|
|
|
|
{
|
|
|
|
TEST_ASSERT_EQUAL(0, e);
|
|
|
|
}
|
2013-01-13 14:39:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_assert_hex_within_less(void)
|
|
|
|
{
|
2013-01-22 11:41:06 +01:00
|
|
|
Try
|
|
|
|
{
|
|
|
|
ASSERT_HEX_WITHIN (0xff00, 0xffff, 0xeeee, 0);
|
|
|
|
}
|
|
|
|
Catch(e)
|
|
|
|
{
|
|
|
|
TEST_ASSERT_EQUAL(0, e);
|
|
|
|
}
|
2013-01-13 14:39:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_assert_hex_within_greater(void)
|
|
|
|
{
|
2013-01-22 11:41:06 +01:00
|
|
|
Try
|
|
|
|
{
|
|
|
|
ASSERT_HEX_WITHIN (0xff00, 0xffff, 0x1eeee, 0);
|
|
|
|
}
|
|
|
|
Catch(e)
|
|
|
|
{
|
|
|
|
TEST_ASSERT_EQUAL(0, e);
|
|
|
|
}
|
2013-01-13 14:39:01 +01:00
|
|
|
}
|
|
|
|
|
2013-01-13 18:41:23 +01:00
|
|
|
//--------------------------------------------------------------------+
|
2013-01-22 11:41:06 +01:00
|
|
|
// BIN
|
2013-01-13 18:41:23 +01:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
void test_assert_bin_equal(void)
|
|
|
|
{
|
2013-01-31 05:05:33 +01:00
|
|
|
Try
|
|
|
|
{
|
|
|
|
ASSERT_BIN8 (BIN8(11110000), BIN8(11110000), __LINE__);
|
|
|
|
ASSERT_BIN8_EQUAL (BIN8(00001111), BIN8(00001111), __LINE__);
|
|
|
|
|
|
|
|
// test side effect
|
|
|
|
uint32_t x = BIN8(11001100);
|
|
|
|
uint32_t y = BIN8(11001100);
|
|
|
|
ASSERT_BIN8 (x++, y++, __LINE__);
|
|
|
|
TEST_ASSERT_EQUAL(BIN8(11001101), x);
|
|
|
|
TEST_ASSERT_EQUAL(BIN8(11001101), y);
|
|
|
|
|
|
|
|
ASSERT_BIN8(BIN8(11001111), BIN8(11111100), 0);
|
|
|
|
}
|
|
|
|
Catch(e)
|
|
|
|
{
|
|
|
|
TEST_ASSERT_EQUAL(0, e);
|
|
|
|
}
|
2013-01-13 18:41:23 +01:00
|
|
|
}
|
|
|
|
|
2013-01-13 14:39:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|