mirror of https://github.com/arendst/Tasmota.git
Berry refactored bytes class (#13208)
* Berry refactored bytes class * Fix
This commit is contained in:
parent
5eb8f78046
commit
883a9fb12c
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -1,42 +1,44 @@
|
|||
#include "be_constobj.h"
|
||||
|
||||
static be_define_const_map_slots(be_class_bytes_map) {
|
||||
{ be_const_key(deinit, -1), be_const_func(m_deinit) },
|
||||
{ be_const_key(opt_connect, -1), be_const_func(m_connect) },
|
||||
{ be_const_key(item, 3), be_const_func(m_item) },
|
||||
{ be_const_key(seti, 22), be_const_func(m_set) },
|
||||
{ be_const_key(asstring, -1), be_const_func(m_asstring) },
|
||||
{ be_const_key(getbits, -1), be_const_closure(getbits_closure) },
|
||||
{ be_const_key(copy, -1), be_const_func(m_copy) },
|
||||
{ be_const_key(get, -1), be_const_func(m_getu) },
|
||||
{ be_const_key(_buffer, -1), be_const_func(m_buffer) },
|
||||
{ be_const_key(fromb64, -1), be_const_func(m_fromb64) },
|
||||
{ be_const_key(add, -1), be_const_func(m_add) },
|
||||
{ be_const_key(dot_p, -1), be_const_var(0) },
|
||||
{ be_const_key(size, 0), be_const_func(m_size) },
|
||||
{ be_const_key(tostring, -1), be_const_func(m_tostring) },
|
||||
{ be_const_key(opt_add, 10), be_const_func(m_merge) },
|
||||
{ be_const_key(resize, -1), be_const_func(m_resize) },
|
||||
{ be_const_key(setitem, -1), be_const_func(m_setitem) },
|
||||
{ be_const_key(set, -1), be_const_func(m_set) },
|
||||
{ be_const_key(opt_connect, -1), be_const_func(m_connect) },
|
||||
{ be_const_key(geti, -1), be_const_func(m_geti) },
|
||||
{ be_const_key(init, -1), be_const_func(m_init) },
|
||||
{ be_const_key(clear, -1), be_const_func(m_clear) },
|
||||
{ be_const_key(opt_neq, 23), be_const_func(m_nequal) },
|
||||
{ be_const_key(tob64, 24), be_const_func(m_tob64) },
|
||||
{ be_const_key(deinit, -1), be_const_func(m_deinit) },
|
||||
{ be_const_key(add, 18), be_const_func(m_add) },
|
||||
{ be_const_key(get, -1), be_const_func(m_getu) },
|
||||
{ be_const_key(asstring, 9), be_const_func(m_asstring) },
|
||||
{ be_const_key(dot_p, -1), be_const_var(0) },
|
||||
{ be_const_key(copy, 25), be_const_func(m_copy) },
|
||||
{ be_const_key(size, -1), be_const_func(m_size) },
|
||||
{ be_const_key(getbits, -1), be_const_closure(getbits_closure) },
|
||||
{ be_const_key(opt_eq, -1), be_const_func(m_equal) },
|
||||
{ be_const_key(fromstring, -1), be_const_func(m_fromstring) },
|
||||
{ be_const_key(tob64, 3), be_const_func(m_tob64) },
|
||||
{ be_const_key(init, -1), be_const_func(m_init) },
|
||||
{ be_const_key(opt_add, -1), be_const_func(m_merge) },
|
||||
{ be_const_key(setbits, -1), be_const_closure(setbits_closure) },
|
||||
{ be_const_key(_buffer, -1), be_const_func(m_buffer) },
|
||||
{ be_const_key(tostring, -1), be_const_func(m_tostring) },
|
||||
{ be_const_key(dot_len, -1), be_const_var(1) },
|
||||
{ be_const_key(fromb64, 13), be_const_func(m_fromb64) },
|
||||
{ be_const_key(dot_size, 6), be_const_var(2) },
|
||||
{ be_const_key(resize, -1), be_const_func(m_resize) },
|
||||
{ be_const_key(seti, -1), be_const_func(m_set) },
|
||||
{ be_const_key(opt_neq, 5), be_const_func(m_nequal) },
|
||||
{ be_const_key(item, -1), be_const_func(m_item) },
|
||||
{ be_const_key(fromstring, -1), be_const_func(m_fromstring) },
|
||||
{ be_const_key(clear, 24), be_const_func(m_clear) },
|
||||
{ be_const_key(set, 10), be_const_func(m_set) },
|
||||
};
|
||||
|
||||
static be_define_const_map(
|
||||
be_class_bytes_map,
|
||||
26
|
||||
28
|
||||
);
|
||||
|
||||
BE_EXPORT_VARIABLE be_define_const_class(
|
||||
be_class_bytes,
|
||||
1,
|
||||
3,
|
||||
NULL,
|
||||
bytes
|
||||
);
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -7,8 +7,6 @@ b=bytes(0)
|
|||
assert(str(b) == "bytes('')")
|
||||
b=bytes(1)
|
||||
assert(str(b) == "bytes('')")
|
||||
b=bytes(-1)
|
||||
assert(str(b) == "bytes('')")
|
||||
assert(b.size() == 0)
|
||||
|
||||
b=bytes("a")
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
#- test for bytes with fixed sizes -#
|
||||
def assert_attribute_error(f)
|
||||
try
|
||||
f()
|
||||
assert(false, 'unexpected execution flow')
|
||||
except .. as e, m
|
||||
assert(e == 'attribute_error')
|
||||
end
|
||||
end
|
||||
|
||||
#- simple initialization -#
|
||||
b=bytes(-1)
|
||||
assert(str(b) == "bytes('00')")
|
||||
b=bytes(-5)
|
||||
assert(str(b) == "bytes('0000000000')")
|
||||
|
||||
#- initialization with hex -#
|
||||
b=bytes("11", -1)
|
||||
assert(str(b) == "bytes('11')")
|
||||
b=bytes("11", -5)
|
||||
assert(str(b) == "bytes('1100000000')")
|
||||
b=bytes("11223344", -4)
|
||||
assert(str(b) == "bytes('11223344')")
|
||||
assert(str(bytes(-3).fromb64('RFVm')) == "bytes('445566')")
|
||||
|
||||
#- check that get and set still works -#
|
||||
b=bytes("11223344",-4)
|
||||
assert(b.get(0,2) == 0x2211)
|
||||
b.set(0,0x5566,2)
|
||||
assert(b.get(0,2) == 0x5566)
|
||||
assert(b[0] == 0x66)
|
||||
b[0]=12
|
||||
assert(b[0] == 12)
|
||||
|
||||
#- resize -#
|
||||
b=bytes("11223344",-4)
|
||||
assert(b.resize(4) == bytes('11223344'))
|
||||
|
||||
#- check that changing size raises an exception -#
|
||||
b=bytes("112233", -3)
|
||||
assert_attribute_error(/-> b.add(1))
|
||||
assert_attribute_error(/-> b.add(2,2))
|
||||
assert_attribute_error(/-> b.resize(4))
|
||||
assert_attribute_error(/-> b.fromstring("aaaaa"))
|
||||
assert_attribute_error(/-> b.fromb64('RFVmdw=='))
|
||||
assert_attribute_error(/-> b.clear())
|
||||
assert_attribute_error(/-> b.bytes())
|
||||
|
||||
#- add -#
|
||||
b=bytes("112233", -3)
|
||||
assert(b+b == bytes("112233112233"))
|
||||
|
||||
#- bytes mapped -#
|
||||
b1=bytes("112233445566")
|
||||
ptr=b1._buffer()
|
||||
b2=bytes(ptr, -4)
|
||||
assert(b2 == bytes("11223344"))
|
||||
b2=bytes(ptr, 4)
|
||||
assert(b2 == bytes("11223344"))
|
||||
b2=bytes(ptr, 6)
|
||||
assert(b2 == bytes("112233445566"))
|
||||
assert(b1 == b2)
|
||||
|
||||
b2.set(0,0xAABB,2)
|
||||
assert(b2 == bytes("BBAA33445566"))
|
||||
assert(b1 == bytes("BBAA33445566"))
|
||||
assert(b1 == b2)
|
Loading…
Reference in New Issue