df

Dependencies:   mbed

Fork of APP1 by Team APP

TestAccelerometer.cpp

Committer:
dupm2216
Date:
2017-01-14
Revision:
4:303fb83498fd
Parent:
3:1a9d0f0a50bf
Child:
5:f59b51ac4b40

File content as of revision 4:303fb83498fd:

#include "TestAccelerometer.hpp"
#include "Accelerometer.hpp"
#include "mbed.h"

#include <cassert>

namespace accelerometer
{
    void run_all_tests()
    {
        test_raw_axis_data_to_int();
        test_set_standby_and_active();
        test_is_almost_equal();
    }
    
    void test_raw_axis_data_to_int()
    {    
        assert(128 == (unsigned char)(0x80));
        assert(-128 == (signed char)(0x80));
        
        assert(0 == raw_axis_data_to_int(0x00));
        assert(1 == raw_axis_data_to_int(0x01));
        assert(127 == raw_axis_data_to_int(0x7F));
        assert(-1 == raw_axis_data_to_int(0xFF));
        assert(-128 == raw_axis_data_to_int(0x80));
    }
    
    void test_set_standby_and_active()
    {
        Accelerometer accelerometer(p9, p10, I2C_ACCELEROMETER_ADDRESS);
    
        accelerometer.set_standby();
        char value = accelerometer.read_register(CTRL_REG1_REGISTER_ADDRESS);
        if(value % 2 != 0)
        {
            printf("Fail\r\n");
        }
    
        accelerometer.set_active();
        value = accelerometer.read_register(CTRL_REG1_REGISTER_ADDRESS);
        if(value % 2 != 1)
        {
            printf("Fail\r\n");
        }
    }

    void test_is_almost_equal()
    {
        assert(is_almost_equal(0, 0, 0.01));
        assert(is_almost_equal(0.50, 0.51, 0.02));
        assert(!is_almost_equal(0.50, 0.52, 0.01));
        assert(!is_almost_equal(0, 0.50, 0.1));
    }
}