move unit tests into each file (compile with -DUNITTEST)
parent
760598ec2f
commit
b9eb81c58f
@ -1,5 +1,31 @@
|
|||||||
#include "scan.h"
|
#include "scan.h"
|
||||||
|
|
||||||
|
#ifdef UNITTEST
|
||||||
|
#undef UNITTEST
|
||||||
|
#include "scan_8longn.c"
|
||||||
|
#define UNITTEST
|
||||||
|
#endif
|
||||||
|
|
||||||
size_t scan_8long(const char *src,unsigned long *dest) {
|
size_t scan_8long(const char *src,unsigned long *dest) {
|
||||||
return scan_8longn(src,(size_t)-1,dest);
|
return scan_8longn(src,(size_t)-1,dest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef UNITTEST
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
unsigned long i;
|
||||||
|
assert(scan_8long("1234",&i)==4 && i==01234);
|
||||||
|
assert(scan_8long("12345678",&i)==7 && i==01234567);
|
||||||
|
if (sizeof(long)==sizeof(int)) {
|
||||||
|
assert(scan_8long("37777777777",&i)==11 && i==0xffffffff);
|
||||||
|
assert(scan_8long("40000000000",&i)==10 && i==04000000000);
|
||||||
|
} else if (sizeof(long)==sizeof(long long)) {
|
||||||
|
assert(scan_8long("1777777777777777777777",&i)==22 && i==0xffffffffffffffffull);
|
||||||
|
assert(scan_8long("2000000000000000000000",&i)==21 && i==0200000000000000000000ull);
|
||||||
|
}
|
||||||
|
assert(scan_8long("-4",&i)==0 && i==0);
|
||||||
|
assert(scan_8long("01234",&i)==5 && i==01234);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
@ -1,5 +1,33 @@
|
|||||||
#include "scan.h"
|
#include "scan.h"
|
||||||
|
|
||||||
|
#ifdef UNITTEST
|
||||||
|
#undef UNITTEST
|
||||||
|
#include "scan_longn.c"
|
||||||
|
#define UNITTEST
|
||||||
|
#endif
|
||||||
|
|
||||||
size_t scan_long(const char *src,long *dest) {
|
size_t scan_long(const char *src,long *dest) {
|
||||||
return scan_longn(src,(size_t)(-1),dest);
|
return scan_longn(src,(size_t)(-1),dest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef UNITTEST
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
long i;
|
||||||
|
assert(scan_long("1234",&i)==4 && i==1234);
|
||||||
|
assert(scan_long("-1234",&i)==5 && i==-1234);
|
||||||
|
assert(scan_long("+1234",&i)==5 && i==1234);
|
||||||
|
assert(scan_long("2147483647",&i)==10 && i==2147483647); // INT_MAX
|
||||||
|
assert(scan_long("-2147483648",&i)==11 && i==-2147483648); // INT_MIN
|
||||||
|
if (sizeof(int) == sizeof(long)) {
|
||||||
|
assert(scan_long("4294967295",&i)==9 && i==429496729); // overflow
|
||||||
|
assert(scan_long("2147483648",&i)==9 && i==214748364); // overflow
|
||||||
|
assert(scan_long("-2147483649",&i)==10 && i==-214748364); // underflow
|
||||||
|
} else {
|
||||||
|
assert(scan_long("9223372036854775807",&i)==19 && i==0x7fffffffffffffffull); // LONG_MAX
|
||||||
|
assert(scan_long("-9223372036854775808",&i)==20 && i==(long long)0x8000000000000000ull); // LONG_MIN
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
Loading…
Reference in New Issue