package io.openems.edge.common.modbusslave;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import java.nio.ByteBuffer;

import org.junit.jupiter.api.Test;

public class ModbusRecordFloat32Test {

	@Test
	public void testUndefined() {
		assertEquals(//
				ModbusRecordFloat32.UNDEFINED_VALUE, //
				ByteBuffer.wrap(ModbusRecordFloat32.UNDEFINED_BYTE_ARRAY).getFloat(), //
				0.001);
		assertArrayEquals(//
				ModbusRecordFloat32.UNDEFINED_BYTE_ARRAY, //
				ModbusRecordFloat32.toByteArray(ModbusRecordFloat32.UNDEFINED_VALUE));
		assertArrayEquals(//
				new byte[] { (byte) 0x7F, (byte) 0xC0, (byte) 0x00, (byte) 0x00 }, //
				ModbusRecordFloat32.UNDEFINED_BYTE_ARRAY);
		assertEquals(//
				"NaN", //
				Float.toHexString(ModbusRecordFloat32.UNDEFINED_VALUE));
		assertEquals(ModbusRecordFloat32.UNDEFINED_BYTE_ARRAY.length, ModbusRecordFloat32.BYTE_LENGTH);
	}

	@Test
	public void testValue() {
		var sut = new ModbusRecordFloat32(0, "foo", 1234567.89F);
		assertEquals("ModbusRecordFloat32 [value=1234567.9, type=float32]", sut.toString());
		assertEquals("\"1234567.9\"", sut.getValueDescription());
	}

	@Test
	public void testNull() {
		var sut = new ModbusRecordFloat32(0, "bar", null);
		assertEquals("ModbusRecordFloat32 [value=UNDEFINED, type=float32]", sut.toString());
		assertEquals("", sut.getValueDescription());
	}

	@Test
	public void testReserved() {
		var sut = new ModbusRecordFloat32Reserved(0);
		assertEquals("ModbusRecordFloat32Reserved [type=float32]", sut.toString());
		assertEquals("", sut.getValueDescription());
	}

}
