2020-12-15 20:17:43 +01:00
// SPDX-License-Identifier: GPL-2.0-only
/* Unit tests for IIO formatting functions
*
* Copyright ( c ) 2020 Lars - Peter Clausen < lars @ metafoo . de >
*/
# include <kunit/test.h>
# include <linux/iio/iio.h>
# define IIO_TEST_FORMAT_EXPECT_EQ(_test, _buf, _ret, _val) do { \
2021-05-13 12:31:59 -07:00
KUNIT_EXPECT_EQ ( _test , strlen ( _buf ) , _ret ) ; \
2020-12-15 20:17:43 +01:00
KUNIT_EXPECT_STREQ ( _test , ( _buf ) , ( _val ) ) ; \
} while ( 0 )
static void iio_test_iio_format_value_integer ( struct kunit * test )
{
2021-11-05 12:05:00 +02:00
char * buf ;
2020-12-15 20:17:43 +01:00
int val ;
int ret ;
2021-11-05 12:05:00 +02:00
buf = kunit_kmalloc ( test , PAGE_SIZE , GFP_KERNEL ) ;
KUNIT_ASSERT_NOT_ERR_OR_NULL ( test , buf ) ;
2020-12-15 20:17:43 +01:00
val = 42 ;
ret = iio_format_value ( buf , IIO_VAL_INT , 1 , & val ) ;
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 42 \n " ) ;
val = - 23 ;
ret = iio_format_value ( buf , IIO_VAL_INT , 1 , & val ) ;
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " -23 \n " ) ;
val = 0 ;
ret = iio_format_value ( buf , IIO_VAL_INT , 1 , & val ) ;
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 0 \n " ) ;
val = INT_MAX ;
ret = iio_format_value ( buf , IIO_VAL_INT , 1 , & val ) ;
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 2147483647 \n " ) ;
val = INT_MIN ;
ret = iio_format_value ( buf , IIO_VAL_INT , 1 , & val ) ;
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " -2147483648 \n " ) ;
}
static void iio_test_iio_format_value_fixedpoint ( struct kunit * test )
{
int values [ 2 ] ;
2021-11-05 12:05:00 +02:00
char * buf ;
2020-12-15 20:17:43 +01:00
int ret ;
2021-11-05 12:05:00 +02:00
buf = kunit_kmalloc ( test , PAGE_SIZE , GFP_KERNEL ) ;
KUNIT_ASSERT_NOT_ERR_OR_NULL ( test , buf ) ;
2020-12-15 20:17:43 +01:00
/* positive >= 1 */
values [ 0 ] = 1 ;
values [ 1 ] = 10 ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_INT_PLUS_MICRO , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 1.000010 \n " ) ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_INT_PLUS_MICRO_DB , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 1.000010 dB \n " ) ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_INT_PLUS_NANO , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 1.000000010 \n " ) ;
/* positive < 1 */
values [ 0 ] = 0 ;
values [ 1 ] = 12 ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_INT_PLUS_MICRO , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 0.000012 \n " ) ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_INT_PLUS_MICRO_DB , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 0.000012 dB \n " ) ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_INT_PLUS_NANO , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 0.000000012 \n " ) ;
/* negative <= -1 */
values [ 0 ] = - 1 ;
values [ 1 ] = 10 ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_INT_PLUS_MICRO , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " -1.000010 \n " ) ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_INT_PLUS_MICRO_DB , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " -1.000010 dB \n " ) ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_INT_PLUS_NANO , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " -1.000000010 \n " ) ;
/* negative > -1 */
values [ 0 ] = 0 ;
values [ 1 ] = - 123 ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_INT_PLUS_MICRO , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " -0.000123 \n " ) ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_INT_PLUS_MICRO_DB , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " -0.000123 dB \n " ) ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_INT_PLUS_NANO , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " -0.000000123 \n " ) ;
}
static void iio_test_iio_format_value_fractional ( struct kunit * test )
{
int values [ 2 ] ;
2021-11-05 12:05:00 +02:00
char * buf ;
2020-12-15 20:17:43 +01:00
int ret ;
2021-11-05 12:05:00 +02:00
buf = kunit_kmalloc ( test , PAGE_SIZE , GFP_KERNEL ) ;
KUNIT_ASSERT_NOT_ERR_OR_NULL ( test , buf ) ;
2020-12-15 20:17:43 +01:00
/* positive < 1 */
values [ 0 ] = 1 ;
values [ 1 ] = 10 ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_FRACTIONAL , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 0.100000000 \n " ) ;
/* positive >= 1 */
values [ 0 ] = 100 ;
values [ 1 ] = 3 ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_FRACTIONAL , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 33.333333333 \n " ) ;
/* negative > -1 */
values [ 0 ] = - 1 ;
values [ 1 ] = 1000000000 ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_FRACTIONAL , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " -0.000000001 \n " ) ;
/* negative <= -1 */
values [ 0 ] = - 200 ;
values [ 1 ] = 3 ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_FRACTIONAL , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " -66.666666666 \n " ) ;
/* Zero */
values [ 0 ] = 0 ;
values [ 1 ] = - 10 ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_FRACTIONAL , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 0.000000000 \n " ) ;
}
static void iio_test_iio_format_value_fractional_log2 ( struct kunit * test )
{
int values [ 2 ] ;
2021-11-05 12:05:00 +02:00
char * buf ;
2020-12-15 20:17:43 +01:00
int ret ;
2021-11-05 12:05:00 +02:00
buf = kunit_kmalloc ( test , PAGE_SIZE , GFP_KERNEL ) ;
KUNIT_ASSERT_NOT_ERR_OR_NULL ( test , buf ) ;
2020-12-15 20:17:43 +01:00
/* positive < 1 */
values [ 0 ] = 123 ;
values [ 1 ] = 10 ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_FRACTIONAL_LOG2 , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 0.120117187 \n " ) ;
/* positive >= 1 */
values [ 0 ] = 1234567 ;
values [ 1 ] = 10 ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_FRACTIONAL_LOG2 , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 1205.631835937 \n " ) ;
/* negative > -1 */
values [ 0 ] = - 123 ;
values [ 1 ] = 10 ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_FRACTIONAL_LOG2 , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " -0.120117187 \n " ) ;
/* negative <= -1 */
values [ 0 ] = - 1234567 ;
values [ 1 ] = 10 ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_FRACTIONAL_LOG2 , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " -1205.631835937 \n " ) ;
/* Zero */
values [ 0 ] = 0 ;
values [ 1 ] = 10 ;
2021-11-05 12:05:00 +02:00
ret = iio_format_value ( buf , IIO_VAL_FRACTIONAL_LOG2 , ARRAY_SIZE ( values ) , values ) ;
2020-12-15 20:17:43 +01:00
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 0.000000000 \n " ) ;
}
static void iio_test_iio_format_value_multiple ( struct kunit * test )
{
int values [ ] = { 1 , - 2 , 3 , - 4 , 5 } ;
2021-11-05 12:05:00 +02:00
char * buf ;
2020-12-15 20:17:43 +01:00
int ret ;
2021-11-05 12:05:00 +02:00
buf = kunit_kmalloc ( test , PAGE_SIZE , GFP_KERNEL ) ;
KUNIT_ASSERT_NOT_ERR_OR_NULL ( test , buf ) ;
2020-12-15 20:17:43 +01:00
ret = iio_format_value ( buf , IIO_VAL_INT_MULTIPLE ,
ARRAY_SIZE ( values ) , values ) ;
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 1 -2 3 -4 5 \n " ) ;
}
2021-11-05 12:05:01 +02:00
static void iio_test_iio_format_value_integer_64 ( struct kunit * test )
{
int values [ 2 ] ;
s64 value ;
char * buf ;
int ret ;
buf = kunit_kmalloc ( test , PAGE_SIZE , GFP_KERNEL ) ;
KUNIT_ASSERT_NOT_ERR_OR_NULL ( test , buf ) ;
value = 24 ;
values [ 0 ] = lower_32_bits ( value ) ;
values [ 1 ] = upper_32_bits ( value ) ;
ret = iio_format_value ( buf , IIO_VAL_INT_64 , ARRAY_SIZE ( values ) , values ) ;
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 24 \n " ) ;
value = - 24 ;
values [ 0 ] = lower_32_bits ( value ) ;
values [ 1 ] = upper_32_bits ( value ) ;
ret = iio_format_value ( buf , IIO_VAL_INT_64 , ARRAY_SIZE ( values ) , values ) ;
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " -24 \n " ) ;
value = 0 ;
values [ 0 ] = lower_32_bits ( value ) ;
values [ 1 ] = upper_32_bits ( value ) ;
ret = iio_format_value ( buf , IIO_VAL_INT_64 , ARRAY_SIZE ( values ) , values ) ;
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 0 \n " ) ;
value = UINT_MAX ;
values [ 0 ] = lower_32_bits ( value ) ;
values [ 1 ] = upper_32_bits ( value ) ;
ret = iio_format_value ( buf , IIO_VAL_INT_64 , ARRAY_SIZE ( values ) , values ) ;
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 4294967295 \n " ) ;
value = - ( ( s64 ) UINT_MAX ) ;
values [ 0 ] = lower_32_bits ( value ) ;
values [ 1 ] = upper_32_bits ( value ) ;
ret = iio_format_value ( buf , IIO_VAL_INT_64 , ARRAY_SIZE ( values ) , values ) ;
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " -4294967295 \n " ) ;
value = LLONG_MAX ;
values [ 0 ] = lower_32_bits ( value ) ;
values [ 1 ] = upper_32_bits ( value ) ;
ret = iio_format_value ( buf , IIO_VAL_INT_64 , ARRAY_SIZE ( values ) , values ) ;
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " 9223372036854775807 \n " ) ;
value = LLONG_MIN ;
values [ 0 ] = lower_32_bits ( value ) ;
values [ 1 ] = upper_32_bits ( value ) ;
ret = iio_format_value ( buf , IIO_VAL_INT_64 , ARRAY_SIZE ( values ) , values ) ;
IIO_TEST_FORMAT_EXPECT_EQ ( test , buf , ret , " -9223372036854775808 \n " ) ;
}
2020-12-15 20:17:43 +01:00
static struct kunit_case iio_format_test_cases [ ] = {
KUNIT_CASE ( iio_test_iio_format_value_integer ) ,
KUNIT_CASE ( iio_test_iio_format_value_fixedpoint ) ,
KUNIT_CASE ( iio_test_iio_format_value_fractional ) ,
KUNIT_CASE ( iio_test_iio_format_value_fractional_log2 ) ,
KUNIT_CASE ( iio_test_iio_format_value_multiple ) ,
2021-11-05 12:05:01 +02:00
KUNIT_CASE ( iio_test_iio_format_value_integer_64 ) ,
2020-12-15 20:17:43 +01:00
{ }
} ;
static struct kunit_suite iio_format_test_suite = {
. name = " iio-format " ,
. test_cases = iio_format_test_cases ,
} ;
kunit_test_suite ( iio_format_test_suite ) ;
2022-07-09 21:31:06 -04:00
MODULE_AUTHOR ( " Lars-Peter Clausen <lars@metafoo.de> " ) ;
MODULE_DESCRIPTION ( " Test IIO formatting functions " ) ;
MODULE_LICENSE ( " GPL v2 " ) ;