add more tests and remove debug code

This commit is contained in:
xyb 2014-01-15 20:33:48 +08:00
parent 3270fb4be6
commit 3e4ed25138
3 changed files with 22 additions and 74 deletions

View File

@ -19,11 +19,9 @@ static mp_obj_t int_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args)
return MP_OBJ_NEW_SMALL_INT(0);
case 1:
// TODO allow string as arg and parse it
return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
case 2:
// TODO make args[0] and args[1] correct
return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int_base(args[1], args[0]));
default:

View File

@ -2,9 +2,7 @@
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "misc.h"
#include "mpconfig.h"
@ -21,8 +19,7 @@ long strtonum(const char *restrict s, int base) {
// check radix base
if ((base != 0 && base < 2) || base > 36) {
printf("ValueError: int() arg 2 must be >=2 and <= 36\n");
return 0;
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "ValueError: int() arg 2 must be >=2 and <= 36"));
}
// skip surrounded whitespace
while (isspace((c = *(p++))));
@ -87,75 +84,8 @@ done:
value_error:
nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_ValueError, "invalid literal for int() with base %d: '%s'", (void*)(machine_uint_t)base, s));
//printf("invalid literal for int() with base %d: '%s'\n", base, s);
//return 0;
}
/*
int main(void) {
printf(" 0 %ld\n", strtonum("0", 0));
printf(" 1 %ld\n", strtonum("1", 0));
printf(" 1 %ld\n", strtonum("+1", 0));
printf(" -1 %ld\n", strtonum("-1", 0));
printf(" 0 %ld\n", strtonum("+0", 0));
printf(" 0 %ld\n", strtonum("-0", 0));
printf(" 1 %ld\n", strtonum("01", 0));
printf(" 9 %ld\n", strtonum("9", 0));
printf(" 10 %ld\n", strtonum("10", 0));
printf(" 10 %ld\n", strtonum("+10", 0));
printf("-10 %ld\n", strtonum("-10", 0));
printf(" 12 %ld\n", strtonum("12", 0));
printf("-12 %ld\n", strtonum("-12", 0));
printf(" 99 %ld\n", strtonum("99", 0));
printf("100 %ld\n", strtonum("100", 0));
printf("314 %ld\n", strtonum("314", 0));
printf("314 %ld\n", strtonum(" 314", 0));
printf("314 %ld\n", strtonum("314 ", 0));
printf("314 %ld\n", strtonum(" \t\t 314 \t\t ", 0));
printf(" 1 %ld\n", strtonum(" 1 ", 0));
printf(" -3 %ld\n", strtonum(" -3 ", 0));
printf(" 83 %ld\n", strtonum("0o123", 0));
printf(" 18 %ld\n", strtonum("0x12", 0));
printf(" 10 %ld\n", strtonum("0o12", 0));
printf(" 10 %ld\n", strtonum("0o12", 0));
printf(" 10 %ld\n", strtonum(" \t 0o12", 0));
printf(" 10 %ld\n", strtonum("0o12 \t ", 0));
printf(" 10 %ld\n", strtonum("0O12", 0));
printf(" 5 %ld\n", strtonum("0b101", 0));
printf(" 5 %ld\n", strtonum("0b101", 0));
printf(" 0 %ld\n", strtonum("0", 10));
printf(" 1 %ld\n", strtonum("1", 10));
printf(" 1 %ld\n", strtonum(" \t 1 \t ", 10));
printf(" 11 %ld\n", strtonum("11", 10));
printf(" 17 %ld\n", strtonum("11", 16));
printf(" 9 %ld\n", strtonum("11", 8));
printf(" 3 %ld\n", strtonum("11", 2));
printf(" 37 %ld\n", strtonum("11", 36));
printf("4294967296 %ld\n", strtonum("4294967296", 0));
printf("%ld\n", strtonum("x", 0));
printf("%ld\n", strtonum("1x", 0));
printf("%ld\n", strtonum(" 1x", 0));
printf("%ld\n", strtonum(" 1\02 ", 0));
printf("%ld\n", strtonum("", 0));
printf("%ld\n", strtonum(" ", 0));
printf("%ld\n", strtonum(" \t\t ", 0));
printf("%ld\n", strtonum("0x", 0));
printf("%ld\n", strtonum("0x", 16));
printf("%ld\n", strtonum("0o", 0));
printf("%ld\n", strtonum("0o", 8));
printf("%ld\n", strtonum("0b", 0));
printf("%ld\n", strtonum("0b", 2));
printf("%ld\n", strtonum("0b2", 2));
printf("%ld\n", strtonum("0o8", 8));
printf("%ld\n", strtonum("0xg", 16));
printf("%ld\n", strtonum("1 1", 0));
printf("%ld\n", strtonum("18446744073709551616", 0));
}
*/
#else /* defined(UNIX) */
long strtonum(const char *restrict s, int base) {

View File

@ -9,10 +9,13 @@ print(int('-0'))
print(int('1'))
print(int('+1'))
print(int('-1'))
print(int('01'))
print(int('9'))
print(int('10'))
print(int('+10'))
print(int('-10'))
print(int('12'))
print(int('-12'))
print(int('99'))
print(int('100'))
print(int('314'))
@ -22,12 +25,26 @@ print(int(' \t\t 314 \t\t '))
print(int(' 1 '))
print(int(' -3 '))
print(int('10', 16))
print(int('0', 10))
print(int('1', 10))
print(int(' \t 1 \t ', 10))
print(int('11', 10))
print(int('11', 16))
print(int('11', 8))
print(int('11', 2))
print(int('11', 36))
print(int('0o123', 0))
print(int('8388607'))
print(int('0x123', 16))
print(int('0X123', 16))
print(int('0o123', 8))
print(int('0O123', 8))
print(int('0123', 8))
print(int('0b100', 2))
print(int('0B100', 2))
print(int('0100', 2))
print(int(' \t 0o12', 8))
print(int('0o12 \t ', 8))
def test(value, base):
@ -37,6 +54,8 @@ def test(value, base):
print('ValueError')
test('x', 0)
test('1x', 0)
test(' 1x', 0)
test(' 1' + chr(2) + ' ', 0)
test('', 0)
@ -52,3 +71,4 @@ test('0b', 0)
test('0b2', 2)
test('0o8', 8)
test('0xg', 16)
test('1 1', 16)