Skip to content

Instantly share code, notes, and snippets.

@kou1okada
Last active September 15, 2016 12:28
Show Gist options
  • Save kou1okada/7cc9e43cf5eb62c9f1bd30200c8a00db to your computer and use it in GitHub Desktop.
Save kou1okada/7cc9e43cf5eb62c9f1bd30200c8a00db to your computer and use it in GitHub Desktop.
test code for SCNu8.
/**
* test code for SCNu8
* Copyright (c) 2016 Koichi OKADA. All rights reserved.
* This code is distributed under the MIT license.
*
* References:
* - MSDN / Visual Studio 2013 / [scanf Width Specification](https://msdn.microsoft.com/en-us/library/xdb9w69d(v=vs.120).aspx)
* - MSDN / Visual Studio 2015 / [scanf Width Specification](https://msdn.microsoft.com/en-us/library/xdb9w69d(v=vs.140).aspx)
* - die.net / [vscanf(3)](http://linux.die.net/man/3/vscanf)
* - JM / [scanf(3)](https://linuxjm.osdn.jp/html/LDP_man-pages/man3/scanf.3.html)
*/
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cinttypes>
#include <cstring>
#include <cassert>
#ifndef SCNu8 // for Cygwin64 x86_64-w64-mingw32-g++ 5.4.0
#define SCNu8 "hhu"
#endif
#ifndef PRIzu
#if __STDC__ || _MSC_VER >= 1900
#define PRIzu "zu"
#elif _MSC_VER
#ifdef _WIN64
#define PRIzu "llu"
#else//_WIN32
#define PRIzu "u"
#endif
#else
#error "PRIzu could not be determined."
#endif
#endif
#define TOSTR(X) # X
#define DUMP(V) {printf("%s: ", #V); dump(V);}
template<class T>
inline void dump(T &v)
{
uint8_t *p = reinterpret_cast<uint8_t *>((void *)&v);
printf("%p(%" PRIzu "):", p, sizeof(T));
for (int i = -4; i < 8; i++) {
printf(" %02x", p[i]);
}
printf("\n");
}
bool testSNCu8()
{
char x[12] = {0};
char y[12] = {0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0};
printf("SCNu8 = \"%s\"\n", SCNu8);
DUMP(x[4]);
printf("%s\n", TOSTR(sscanf("-1", "%" SCNu8 "", &x[4]);));
; sscanf("-1", "%" SCNu8 "", &x[4]);
DUMP(x[4]);
return memcmp(x, y, sizeof(x)) == 0;
}
int main(int argc, char *argv[])
{
assert(testSNCu8());
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment