2013-04-16 23:57:42 -04:00
|
|
|
/* -*- mode: C; c-basic-offset: 4 -*- */
|
|
|
|
#ident "Copyright (c) 2007, 2008 Tokutek Inc. All rights reserved."
|
|
|
|
|
2013-04-16 23:57:53 -04:00
|
|
|
#include <test.h>
|
2013-04-16 23:57:42 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2013-04-16 23:58:56 -04:00
|
|
|
#include <toku_assert.h>
|
2013-04-16 23:57:42 -04:00
|
|
|
#include "toku_os.h"
|
|
|
|
|
2013-04-16 23:57:44 -04:00
|
|
|
static void
|
2013-04-16 23:57:42 -04:00
|
|
|
check_snprintf(int i) {
|
|
|
|
char buf_before[8];
|
2013-04-16 23:57:42 -04:00
|
|
|
char target[5];
|
2013-04-16 23:57:42 -04:00
|
|
|
char buf_after[8];
|
|
|
|
memset(target, 0xFF, sizeof(target));
|
|
|
|
memset(buf_before, 0xFF, sizeof(buf_before));
|
|
|
|
memset(buf_after, 0xFF, sizeof(buf_after));
|
|
|
|
int64_t n = 1;
|
|
|
|
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < i; j++) n *= 10;
|
|
|
|
|
|
|
|
int bytes = snprintf(target, sizeof target, "%"PRId64, n);
|
2013-04-16 23:57:42 -04:00
|
|
|
assert(bytes==i+1 ||
|
|
|
|
(i+1>=sizeof target && bytes>=sizeof target));
|
2013-04-16 23:57:42 -04:00
|
|
|
if (bytes>=sizeof target) {
|
|
|
|
//Overflow prevented by snprintf
|
|
|
|
assert(target[sizeof target - 1] == '\0');
|
|
|
|
assert(strlen(target)==sizeof target-1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert(target[bytes] == '\0');
|
|
|
|
assert(strlen(target)==bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-16 23:57:53 -04:00
|
|
|
int test_main(int argc, char *argv[]) {
|
2013-04-16 23:57:42 -04:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
check_snprintf(i);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|