public void ResponseReadValue() { #if !DEBUG Assert.Inconclusive("This test requires DEBUG context"); #endif foreach (Medium_T _enumIdx in Enum.GetValues(typeof(Medium_T))) { using (SBUSNet_message _frame = new SBUSNet_message(null)) { Assert.IsNotNull(_frame, "Message is not created."); BlockAddress _tb = new BlockAddress((short)_enumIdx, c_station, C_startAddress, C_RegistersCount); _frame.Test_PrepareRequest(_tb.station, _tb); List <byte> _frameList = _frame.GetManagedBuffer().ToList(); Assert.AreEqual(m_TestTable[_enumIdx].Count, _frame.userDataLength, "The length of the request is not valid"); string _msg = String.Format("Frame must be equal to the template. Failed for {0} ", _enumIdx); m_TestTable[_enumIdx][7] = _frameList[7]; // sequence numbers must be the same CollectionAssert.AreEqual(m_TestTable[_enumIdx], _frameList, _msg); using (SBUSNet_message _dateFrame = new SBUSNet_message(null)) { _dateFrame.InitMsg(_frame); ushort _expectedFrameLength = c_HeaderLength + c_ATLength; switch (_enumIdx) { case Medium_T.Flag: case Medium_T.Input: case Medium_T.Output: _expectedFrameLength += (C_RegistersCount - 1) / 8 + 1; //<header><AT>{fio-count}+ CheckExpectedLength(_enumIdx, _dateFrame, _expectedFrameLength); TestContent <bool>(_dateFrame, false, CompareType <bool>); break; case Medium_T.Register: case Medium_T.Timer: case Medium_T.Counter: _expectedFrameLength += C_RegistersCount * 4; CheckExpectedLength(_enumIdx, _dateFrame, _expectedFrameLength); TestContent <int>(_dateFrame, 123, CompareType <int>); break; case Medium_T.Text: _expectedFrameLength += C_RegistersCount; CheckExpectedLength(_enumIdx, _dateFrame, _expectedFrameLength); TestContent <string>(_dateFrame, "1234567890123".Substring(0, _tb.length), CompareString); break; default: Assert.Fail("Application error: unknown requested data type"); break; } } } } }
public void RequestWrite_RequestContentTest() { #if !DEBUG Assert.Inconclusive("This test requires DEBUG context"); #endif const int c_station = 231; const ushort c_startAddress = 1; const byte c_RegistersCount = 1; const byte c_HeaderLength = 8; const byte c_ATLength = 1; foreach (Medium_T _enumIdx in Enum.GetValues(typeof(Medium_T))) { if (_enumIdx == Medium_T.Input) { continue; } using (SBUSNet_message _frame = new SBUSNet_message(null)) { Assert.IsNotNull(_frame, "Message is not created."); BlockAddress _tb = new BlockAddress((short)_enumIdx, c_station, c_startAddress, c_RegistersCount); _frame.Test_PrepareReqWriteValue(_tb, _tb.station); ushort _expectedFrameLength = c_HeaderLength + c_ATLength + 1 + 1; //<head><attribute><station><cmd> byte[] _frameBuff = null; RequestSimulator _sim = null; switch (_enumIdx) { case Medium_T.Flag: case Medium_T.Input: case Medium_T.Output: _expectedFrameLength += 1 + 2 + 1 + c_RegistersCount / 8 + 1; //<w-count> <address-IOF> <fio-count> {<fio-byte>}+ bool _testBool = true; for (int _bx = 0; _bx < c_RegistersCount; _bx++) { _frame.WriteValue(_testBool, _bx); } CheckExpectedLength(_enumIdx, _frame, _expectedFrameLength); _frameBuff = _frame.GetManagedBuffer(); _sim = new RequestSimulator(c_RegistersCount, _testBool, _expectedFrameLength, c_station, _enumIdx, c_startAddress); _sim[7] = _frameBuff[7]; byte _mask = 0xff >> 8 - c_RegistersCount % 8; _sim[_expectedFrameLength - 1] &= _mask; _frameBuff[_expectedFrameLength - 1] &= _mask; CollectionAssert.AreEqual(_sim, _frameBuff, "Frame and its template must be equal"); break; case Medium_T.Register: case Medium_T.Timer: case Medium_T.Counter: _expectedFrameLength += 1 + 2 + c_RegistersCount * 4; //<w-count> <address-RTC> {<4-byte>}+ CheckExpectedLength(_enumIdx, _frame, _expectedFrameLength); int[] _testValuesArray = new int[] { int.MaxValue, int.MinValue, 0, -123456, 7654321 }; foreach (int _itv in _testValuesArray) { for (int _iix = 0; _iix < c_RegistersCount; _iix++) { _frame.WriteValue(_itv, _iix); } _frameBuff = _frame.GetManagedBuffer(); _sim = new RequestSimulator(c_RegistersCount, _itv, _expectedFrameLength, c_station, _enumIdx, c_startAddress); _sim[7] = _frameBuff[7]; CollectionAssert.AreEqual(_sim, _frameBuff, "Frame and its template must be equal"); } break; case Medium_T.Text: _expectedFrameLength += 1 + 1 + 2 + 1 + c_RegistersCount; //<w-count> <text-number> <char-position> {<ascii-char>}+ CheckExpectedLength(_enumIdx, _frame, _expectedFrameLength); string[] _testString = new string[] { "0123456789012345678901", "0123456789012", "012345678", null, String.Empty }; foreach (string _ts in _testString) { _frame.WriteValue(_ts, 0); _frameBuff = _frame.GetManagedBuffer(); _sim = new RequestSimulator(c_RegistersCount, _ts, _expectedFrameLength, c_station, _enumIdx, c_startAddress); _sim[7] = _frameBuff[7]; CollectionAssert.AreEqual(_sim, _frameBuff, "Frame and its template must be equal"); } break; default: Assert.Fail("Application error: unknown requested data type"); break; } } } }