[pypy-commit] creflect default: more tests
arigo
noreply at buildbot.pypy.org
Fri Dec 19 17:10:09 CET 2014
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r204:b1521d66a224
Date: 2014-12-19 15:23 +0100
http://bitbucket.org/cffi/creflect/changeset/b1521d66a224/
Log: more tests
diff --git a/zeffir/builder.c b/zeffir/builder.c
--- a/zeffir/builder.c
+++ b/zeffir/builder.c
@@ -116,8 +116,7 @@
static _crx_type_t *zef_get_char_type(_crx_builder_t *cb)
{
return _zef_primitive(cb, sizeof(char), "char",
- CT_PRIMITIVE_SIGNED | CT_PRIMITIVE_CHAR |
- CT_PRIMITIVE_FITS_LONG);
+ CT_PRIMITIVE_CHAR | CT_PRIMITIVE_FITS_LONG);
}
static _crx_type_t *zef_get_bool_type(_crx_builder_t *cb)
@@ -289,9 +288,7 @@
ct->ct_flags = CT_POINTER;
if (totype->ct_flags & CT_VOID)
ct->ct_flags |= CT_IS_VOID_PTR;
- if ((totype->ct_flags & CT_VOID) ||
- ((totype->ct_flags & CT_PRIMITIVE_CHAR) &&
- totype->ct_size == sizeof(char)))
+ if (totype->ct_flags & (CT_VOID | CT_PRIMITIVE_CHAR))
ct->ct_flags |= CT_CAST_ANYTHING; /* 'void *' or 'char *' only */
put_cached_type(types_dict, name_obj, ct);
diff --git a/zeffir/cdata.c b/zeffir/cdata.c
--- a/zeffir/cdata.c
+++ b/zeffir/cdata.c
@@ -1087,12 +1087,8 @@
}
else if (cd->c_type->ct_flags & CT_PRIMITIVE_CHAR) {
/*READ(cd->c_data, cd->c_type->ct_size)*/
- if (cd->c_type->ct_size == sizeof(char))
- return PyInt_FromLong((unsigned char)cd->c_data[0]);
-#ifdef HAVE_WCHAR_H
- else
- return PyInt_FromLong((long)*(wchar_t *)cd->c_data);
-#endif
+ assert(cd->c_type->ct_size == sizeof(char));
+ return PyInt_FromLong((unsigned char)cd->c_data[0]);
}
else if (cd->c_type->ct_flags & CT_PRIMITIVE_FLOAT) {
PyObject *o = cdata_float(cd);
@@ -1826,8 +1822,9 @@
return NULL;
return new_simple_cdata((char *)(Py_intptr_t)value, ct);
}
- else if (ct->ct_flags & (CT_PRIMITIVE_SIGNED|CT_PRIMITIVE_UNSIGNED
- |CT_PRIMITIVE_CHAR)) {
+ else if (ct->ct_flags & (CT_PRIMITIVE_SIGNED |
+ CT_PRIMITIVE_UNSIGNED |
+ CT_PRIMITIVE_CHAR)) {
/* cast to an integer type or a char */
return (PyObject *)cast_to_integer_or_char(ct, ob);
}
diff --git a/zeffir/test/test_c.py b/zeffir/test/test_c.py
--- a/zeffir/test/test_c.py
+++ b/zeffir/test/test_c.py
@@ -70,3 +70,86 @@
assert (x != ffi.cast(t, -66)) is True
assert (x == ffi.cast("short", -66)) is False
assert (x != ffi.cast("short", -66)) is True
+
+def test_sizeof_type():
+ ffi = support.new_ffi()
+ py.test.raises(TypeError, ffi.sizeof, 42.5)
+ assert ffi.sizeof("short") == 2
+ assert ffi.sizeof(ffi.typeof("short")) == 2
+
+def test_integer_types():
+ ffi = support.new_ffi()
+ for name in ['signed char', 'short', 'int', 'long', 'long long']:
+ p = ffi.typeof(name)
+ size = ffi.sizeof(p)
+ min = -(1 << (8*size-1))
+ max = (1 << (8*size-1)) - 1
+ assert int(ffi.cast(p, min)) == min
+ assert int(ffi.cast(p, max)) == max
+ assert int(ffi.cast(p, min - 1)) == max
+ assert int(ffi.cast(p, max + 1)) == min
+ py.test.raises(TypeError, ffi.cast, p, None)
+ assert long(ffi.cast(p, min - 1)) == max
+ assert int(ffi.cast(p, b'\x08')) == 8
+ for name in ['char', 'short', 'int', 'long', 'long long']:
+ p = ffi.typeof('unsigned ' + name)
+ size = ffi.sizeof(p)
+ max = (1 << (8*size)) - 1
+ assert int(ffi.cast(p, 0)) == 0
+ assert int(ffi.cast(p, max)) == max
+ assert int(ffi.cast(p, -1)) == max
+ assert int(ffi.cast(p, max + 1)) == 0
+ assert long(ffi.cast(p, -1)) == max
+ assert int(ffi.cast(p, b'\xFE')) == 254
+
+def test_no_float_on_int_types():
+ ffi = support.new_ffi()
+ py.test.raises(TypeError, float, ffi.cast('long', 42))
+ py.test.raises(TypeError, complex, ffi.cast('long', 42))
+
+def test_float_types():
+ ffi = support.new_ffi()
+ INF = 1E200 * 1E200
+ for name in ["float", "double"]:
+ p = ffi.typeof(name)
+ assert bool(ffi.cast(p, 0))
+ assert bool(ffi.cast(p, INF))
+ assert bool(ffi.cast(p, -INF))
+ assert int(ffi.cast(p, -150)) == -150
+ assert int(ffi.cast(p, 61.91)) == 61
+ assert long(ffi.cast(p, 61.91)) == 61
+ assert type(int(ffi.cast(p, 61.91))) is int
+ assert type(int(ffi.cast(p, 1E22))) is long
+ assert type(long(ffi.cast(p, 61.91))) is long
+ assert type(long(ffi.cast(p, 1E22))) is long
+ py.test.raises(OverflowError, int, ffi.cast(p, INF))
+ py.test.raises(OverflowError, int, ffi.cast(p, -INF))
+ assert float(ffi.cast(p, 1.25)) == 1.25
+ assert float(ffi.cast(p, INF)) == INF
+ assert float(ffi.cast(p, -INF)) == -INF
+ if name == "float":
+ assert float(ffi.cast(p, 1.1)) != 1.1 # rounding error
+ assert float(ffi.cast(p, 1E200)) == INF # limited range
+
+ assert ffi.cast(p, -1.1) != ffi.cast(p, -1.1)
+ assert repr(float(ffi.cast(p, -0.0))) == '-0.0'
+ assert float(ffi.cast(p, b'\x09')) == 9.0
+ assert float(ffi.cast(p, True)) == 1.0
+ py.test.raises(TypeError, ffi.cast, p, None)
+
+def test_complex_types():
+ py.test.skip("later")
+
+def test_character_type():
+ ffi = support.new_ffi()
+ p = ffi.typeof("char")
+ assert bool(ffi.cast(p, '\x00'))
+ assert ffi.cast(p, '\x00') != ffi.cast(p, -17*256)
+ assert int(ffi.cast(p, 'A')) == 65
+ assert long(ffi.cast(p, 'A')) == 65
+ assert type(int(ffi.cast(p, 'A'))) is int
+ assert type(long(ffi.cast(p, 'A'))) is long
+ assert str(ffi.cast(p, 'A')) == repr(ffi.cast(p, 'A'))
+ assert repr(ffi.cast(p, 'A')) == "<cdata 'char' %s'A'>" % mandatory_b_prefix
+ assert repr(ffi.cast(p, 255)) == r"<cdata 'char' %s'\xff'>" % mandatory_b_prefix
+ assert repr(ffi.cast(p, 0)) == r"<cdata 'char' %s'\x00'>" % mandatory_b_prefix
More information about the pypy-commit
mailing list