mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	[bytebuffer] Rework ByteBuffer using templates (#7638)
This commit is contained in:
		
							
								
								
									
										161
									
								
								tests/components/bytebuffer/common.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										161
									
								
								tests/components/bytebuffer/common.yaml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,161 @@ | ||||
| bytebuffer: | ||||
|  | ||||
| esphome: | ||||
|   on_boot: | ||||
|     - lambda: |- | ||||
|         using namespace bytebuffer; | ||||
|         auto buf = ByteBuffer(16); | ||||
|         assert(buf.get_endianness() == LITTLE); | ||||
|         assert(buf.get_remaining() == 16); | ||||
|         buf.set_limit(10); | ||||
|         assert(buf.get_capacity() == 16); | ||||
|         buf.put_uint8(1); | ||||
|         assert(buf.get_remaining() == 9); | ||||
|         buf.put_uint16(0xABCD); | ||||
|         auto da = buf.get_data(); | ||||
|         assert(buf.get_uint8(0) == 1); | ||||
|         auto x = buf.get_uint16(1); | ||||
|         assert(buf.get_uint16(1) == 0xABCD); | ||||
|         assert(buf.get_remaining() == 7); | ||||
|         buf.put_uint32(0x12345678UL); | ||||
|         assert(buf.get_uint32(3) == 0x12345678UL); | ||||
|         assert(buf.get_remaining() == 3); | ||||
|         assert(buf.get_data()[1] == 0xCD); | ||||
|         assert(buf.get_data()[2] == 0xAB); | ||||
|         assert(buf.get_data()[3] == 0x78); | ||||
|         assert(buf.get_data()[4] == 0x56); | ||||
|         assert(buf.get_data()[5] == 0x34); | ||||
|         assert(buf.get_data()[6] == 0x12); | ||||
|         buf.flip(); | ||||
|         assert(buf.get_capacity() == 16); | ||||
|         assert(buf.get_uint32(3) == 0x12345678UL); | ||||
|         assert(buf.get_uint8(0) == 1); | ||||
|         assert(buf.get_uint16(1) == 0xABCD); | ||||
|         buf.put_uint16(0x1234, 1); | ||||
|         assert(buf.get_uint16(1) == 0x1234); | ||||
|         assert(buf.get_remaining() == 7); | ||||
|         assert(buf.get_uint8() == 1); | ||||
|         assert(buf.get_uint16() == 0x1234); | ||||
|         assert(buf.get_uint32() == 0x12345678ul); | ||||
|         assert(buf.get_remaining() == 0); | ||||
|         assert(buf.get_remaining() == 0); | ||||
|         buf.rewind(); | ||||
|         buf.big_endian(); | ||||
|         assert(buf.get_remaining() == 7); | ||||
|         assert(buf.get_uint8() == 1); | ||||
|         assert(buf.get_uint16() == 0x3412); | ||||
|         buf.mark(); | ||||
|         assert(buf.get_uint32() == 0x78563412ul); | ||||
|         assert(buf.get_remaining() == 0); | ||||
|         buf.reset(); | ||||
|         assert(buf.get_remaining() == 4); | ||||
|         assert(buf.get_uint32() == 0x78563412ul); | ||||
|         auto buf1 = ByteBuffer::wrap(buf.get_data().data(), buf.get_limit()); | ||||
|         buf.clear(); | ||||
|         assert(buf.get_position() == 0); | ||||
|         assert(buf.get_capacity() == 16); | ||||
|         assert(buf.get_limit() == 16); | ||||
|         assert(buf1.get_remaining() == 7); | ||||
|         assert(buf1.get_capacity() == 7); | ||||
|         buf1.set_position(3); | ||||
|         assert(buf1.get_uint32() == 0x12345678ul); | ||||
|         buf1.clear(); | ||||
|         assert(buf1.get_limit() == 7); | ||||
|         assert(buf1.get_capacity() == 7); | ||||
|         assert(buf1.get_position() == 0); | ||||
|         float f = 1.2345; | ||||
|         buf1.put_float(f); | ||||
|         buf1.flip(); | ||||
|         assert(buf1.get_remaining() == 4); | ||||
|         assert(buf1.get_float() == f); | ||||
|         buf1.clear(); | ||||
|         buf1.put_uint16(-32760); | ||||
|         buf1.put_uint24(-302760); | ||||
|         buf1.flip(); | ||||
|         assert(buf1.get_int16() == -32760); | ||||
|         assert(buf1.get_int24() == -302760); | ||||
|         uint8_t arr[4] = {0x10, 0x20, 0x30, 0x40}; | ||||
|         buf1 = ByteBuffer::wrap(arr, 4); | ||||
|         assert(buf1.get_capacity() == 4); | ||||
|         assert(buf1.get_limit() == 4); | ||||
|         assert(buf1.get_position() == 0); | ||||
|         assert(buf1.get_uint32() == 0x40302010UL); | ||||
|         assert(buf1.get_position() == 4); | ||||
|         assert(buf1.get_remaining() == 0); | ||||
|         std::vector<uint8_t> vec{}; | ||||
|         vec.push_back(0x10); | ||||
|         vec.push_back(0x20); | ||||
|         vec.push_back(0x30); | ||||
|         vec.push_back(0x40); | ||||
|         buf1 = ByteBuffer::wrap(vec); | ||||
|         assert(buf1.get_capacity() == 4); | ||||
|         assert(buf1.get_limit() == 4); | ||||
|         assert(buf1.get_position() == 0); | ||||
|         buf1.mark(); | ||||
|         buf1.reset(); | ||||
|         assert(buf1.get_uint32() == 0x40302010UL); | ||||
|         buf = ByteBuffer::wrap(true); | ||||
|         assert(buf.get_bool() == true); | ||||
|         buf = ByteBuffer::wrap((uint8_t)0xFE); | ||||
|         assert(buf.get_uint8() == 0xFE); | ||||
|         buf = ByteBuffer::wrap((uint16_t)0xA5A6, BIG); | ||||
|         assert(buf.get_remaining() == 2); | ||||
|         assert(buf.get_position() == 0); | ||||
|         assert(buf.get_capacity() == 2); | ||||
|         assert(buf.get_endianness() == BIG); | ||||
|         assert(buf.get_data()[0] == 0xA5); | ||||
|         assert(buf.get_uint16() == 0xA5A6); | ||||
|         buf.flip(); | ||||
|         buf.little_endian(); | ||||
|         assert(buf.get_uint16() == 0xA6A5); | ||||
|         buf = ByteBuffer::wrap(f, BIG); | ||||
|         assert(buf.get_float() == f); | ||||
|         double d = 1.2345678E7; | ||||
|         buf = ByteBuffer::wrap(d, BIG); | ||||
|         assert(buf.get_double() == d); | ||||
|         buf = ByteBuffer::wrap({1, 2, 3, 4}, BIG); | ||||
|         assert(buf.get_endianness() == BIG); | ||||
|         assert(buf.get_remaining() == 4); | ||||
|         assert(buf.get_data()[2] == 3); | ||||
|         buf.little_endian(); | ||||
|         assert(buf.get_data()[2] == 3); | ||||
|         assert(buf.get_uint16() == 0x0201); | ||||
|         buf.big_endian(); | ||||
|         assert(buf.get_uint16() == 0x0304); | ||||
|         buf.rewind(); | ||||
|         vec = buf.get_vector(3); | ||||
|         assert(buf.get_remaining() == 1); | ||||
|         assert(vec[0] == 1); | ||||
|         assert(vec.size() == 3); | ||||
|         buf = ByteBuffer(10); | ||||
|         buf.put_vector(vec); | ||||
|         assert(buf.get_remaining() == 7); | ||||
|         buf.flip(); | ||||
|         assert(buf.get_remaining() == 3); | ||||
|         assert(buf.get_uint24() == 0x030201); | ||||
|         buf = ByteBuffer(64); | ||||
|         buf.put_uint8(1, 1); | ||||
|         buf.put_uint16(16, 2); | ||||
|         buf.put_uint32(1232, 4); | ||||
|         buf.put_uint64(123432ul, 8); | ||||
|         buf.put_float(1.2f, 16); | ||||
|         buf.put_int24(0x678, 20); | ||||
|  | ||||
|         assert(buf.get_uint8(1) == 1); | ||||
|         assert(buf.get<uint8_t>(1) == 1); | ||||
|         assert(buf.get_uint16(2) == 16); | ||||
|         assert(buf.get<uint16_t>(2) == 16); | ||||
|         assert(buf.get_uint32(4) == 1232); | ||||
|         assert(buf.get<uint32_t>(4) == 1232); | ||||
|         assert(buf.get_uint64(8) == 123432ul); | ||||
|         assert(buf.get<uint64_t>(8) == 123432ul); | ||||
|         assert(buf.get_float(16) == 1.2f); | ||||
|         assert(buf.get<float>(16) == 1.2f); | ||||
|         assert(buf.get_int24(20) == 0x678); | ||||
|         buf.clear(); | ||||
|         buf.put(1.234, 10); | ||||
|         double dx = buf.get<double>(10); | ||||
|         assert(dx == 1.234); | ||||
|         buf.put((uint16_t)1, 10); | ||||
|         assert(buf.get_uint16(10) == 1); | ||||
|         ESP_LOGD("bytebuffer", "******************** All tests succeeded"); | ||||
		Reference in New Issue
	
	Block a user