printf testing: exercise integer parsing in the test framework

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2022-12-03 22:38:14 +01:00
parent 187932639b
commit bc3db2e30a
2 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,57 @@
# The test cases for printf and integers have two purposes: they exercise
# the printf function family, and they exercise the passing of integers
# through the test framework.
printf "%d", 0
printf_int:"%d":0:"0"
printf "%d", -0
printf_int:"%d":-0:"0"
printf "%d", 0x0
printf_int:"%d":0x0:"0"
printf "%d", 0x00
printf_int:"%d":0x00:"0"
printf "%d", 0x000000000000000000000000000000000000000000
printf_int:"%d":0x000000000000000000000000000000000000000000:"0"
printf "%d", -0x0
printf_int:"%d":-0x0:"0"
printf "%d", 1
printf_int:"%d":1:"1"
printf "%d", 0x1
printf_int:"%d":0x1:"1"
printf "%d", 0x0000000000000000000000000000000000000000001
printf_int:"%d":0x0000000000000000000000000000000000000000001:"1"
printf "%d", -1
printf_int:"%d":-1:"-1"
printf "%d", -0x1
printf_int:"%d":-0x1:"-1"
printf "%d", -0x0000000000000000000000000000000000000000001
printf_int:"%d":-0x0000000000000000000000000000000000000000001:"-1"
printf "%d", 2147483647
printf_int:"%d":2147483647:"2147483647"
printf "%d", 0x7fffffff
printf_int:"%d":0x7fffffff:"2147483647"
printf "%d", -2147483647
printf_int:"%d":-2147483647:"-2147483647"
printf "%d", -0x7fffffff
printf_int:"%d":-0x7fffffff:"-2147483647"
printf "%d", -2147483648
printf_int:"%d":-2147483648:"-2147483648"
printf "%d", -0x80000000
printf_int:"%d":-0x80000000:"-2147483648"

View File

@ -0,0 +1,25 @@
/* BEGIN_HEADER */
#include "mbedtls/platform.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* END_HEADER */
/* BEGIN_CASE */
void printf_int(char *format, int x, char *result)
{
char *output = NULL;
const size_t n = strlen(result);
/* Nominal case: buffer just large enough */
ASSERT_ALLOC(output, n + 1);
TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, x));
ASSERT_COMPARE(result, n + 1, output, n + 1);
mbedtls_free(output);
output = NULL;
exit:
mbedtls_free(output);
}
/* END_CASE */