Beispiel #1
0
        public void Check_ToData()
        {
            var target       = Guid.NewGuid().ToString();
            var targetData   = Encoding.UTF8.GetBytes(target);
            var targetLength = (ushort)targetData.Length;

            var item = new Utf8PlcItem(0, 0, targetLength);

            // Check: String → Data
            item.Value = target;
            Assert.AreEqual(target, item.Value);
            Assert.True(targetData.SequenceEqual((byte[])((IPlcItem)item).Value));
        }
Beispiel #2
0
        public void Check_Initial_Data()
        {
            // Arrange
            var targetValue = "Some test message.";
            var targetData  = Encoding.UTF8.GetBytes(targetValue);

            // Act
            var item = new Utf8PlcItem(0, 0, targetValue);

            // Assert: Number → Data
            Assert.That(item.Value, Is.EqualTo(targetValue));
            Assert.That((byte[])((IPlcItem)item).Value, Is.EqualTo(targetData));
        }
Beispiel #3
0
        public void Check_Clone()
        {
            var item  = new Utf8PlcItem(0, 0, "Some test message.");
            var clone = item.Clone();

            // Check equality (Equals is overriden).
            Assert.AreEqual(item, clone);

            // Check the value.
            Assert.AreEqual(item.Value, clone.Value);

            // Check if both items are different references.
            Assert.False(Object.ReferenceEquals(item, clone));
        }
        public void WriteText()
        {
            var target    = System.Text.Encoding.ASCII.GetString(this.Data.WriteBytes);
            var writeItem = new Utf8PlcItem(dataBlock: this.Data.Datablock, position: 4, initialValue: target);

            base.ExecuteTest
            (
                async(plc) =>
            {
                var result = await plc.WriteItemWithValidationAsync(writeItem);
                Assert.True(result);
            }
            );
        }
Beispiel #5
0
        public void ReadText()
        {
            var asciiItem = new Utf8PlcItem(dataBlock: this.Data.Datablock, position: 0, length: (ushort)this.Data.TargetBytes.Length);

            base.ExecuteTest
            (
                async(plc) =>
            {
                var result = await plc.ReadItemAsync(asciiItem);
                Assert.AreEqual((int)((IPlcItem)asciiItem).Value.ByteLength, result.Length);
                Assert.True(asciiItem.Value.Equals(result, StringComparison.Ordinal));
                Assert.AreEqual("ROCK", asciiItem.Value);
            }
            );
        }
Beispiel #6
0
        public void Check_Null_Assignment()
        {
            var item = new Utf8PlcItem(0, 0, 0);

            Assert.AreEqual(String.Empty, item.Value);

            item = new Utf8PlcItem(0, 0, null);
            Assert.AreEqual(String.Empty, item.Value);

            var customString = Guid.NewGuid().ToString();

            item = new Utf8PlcItem(0, 0, customString);
            Assert.AreEqual(customString, item.Value);
            var byteLength = ((IPlcItem)item).Value.ByteLength;

            // Set the value to null.
            item.Value = null;
            Assert.AreEqual(String.Empty, item.Value);                      // The value should now be an empty string.
            Assert.True(((IPlcItem)item).Value.ContainsOnlyZeros);          // The underlying BitCollection should contain a zero filled boolean array.
            Assert.AreEqual(byteLength, ((IPlcItem)item).Value.ByteLength); // The size of the underlying BitCollection should not have changed.
        }
Beispiel #7
0
        public void CheckItemBuilder()
        {
            var itemBuilder = new PlcItemBuilder();

            var plcItem = itemBuilder
                          .Construct("Generic")
                          .ForData()
                          .AtDatablock(0)
                          .AtPosition(0, BitPosition.X2)
                          .ForBitAmount(3)
                          .Build()
            ;

            Assert.AreEqual((uint)3, plcItem.Value.Length);

            BitsPlcItem bitsItem = itemBuilder
                                   .ConstructBitsPlcItem()
                                   .ForFlags()
                                   .AtPosition(20, BitPosition.X5)
                                   .ForBitAmount(5)
                                   .Build()
            ;

            Assert.AreEqual((uint)5, ((IPlcItem)bitsItem).Value.Length);

            BitPlcItem bitItem = itemBuilder
                                 .ConstructBitPlcItem("Bit")
                                 .ForData()
                                 .AtDatablock(0)
                                 .AtPosition(5)
                                 .AsSet()
                                 .Build()
            ;

            Assert.AreEqual((uint)1, ((IPlcItem)bitItem).Value.Length);

            BytesPlcItem bytesItem = itemBuilder
                                     .ConstructBytesPlcItem(identifier: "Bytes")
                                     .ForOutput()
                                     .AtPosition(0)
                                     .WithInitialValue(new[] { byte.MinValue, byte.MaxValue })
                                     .Build()
            ;

            Assert.AreEqual((uint)2, ((IPlcItem)bytesItem).Value.ByteLength);

            BytePlcItem byteItem = itemBuilder
                                   .ConstructBytePlcItem("Byte")
                                   .ForInput()
                                   .AtPosition(10)
                                   .WithInitialValue(Byte.MaxValue)
                                   .Build()
            ;

            Assert.AreEqual((uint)sizeof(Byte), ((IPlcItem)byteItem).Value.ByteLength);

            Int16PlcItem int16Item = itemBuilder
                                     .ConstructInt16PlcItem("Int16")
                                     .AtDatablock(0)
                                     .AtPosition(1)
                                     .WithoutInitialValue()
                                     .Build()
            ;

            Assert.AreEqual((uint)sizeof(Int16), ((IPlcItem)int16Item).Value.ByteLength);

            Int32PlcItem int32Item = itemBuilder
                                     .ConstructInt32PlcItem("Int32")
                                     .AtDatablock(0)
                                     .AtPosition(1)
                                     .WithInitialValue(int.MinValue)
                                     .Build()
            ;

            Assert.AreEqual((uint)sizeof(Int32), ((IPlcItem)int32Item).Value.ByteLength);

            Int64PlcItem int64Item = itemBuilder
                                     .ConstructInt64PlcItem("Int64")
                                     .AtDatablock(0)
                                     .AtPosition(1)
                                     .WithInitialValue(long.MinValue)
                                     .Build()
            ;

            Assert.AreEqual((uint)sizeof(Int64), ((IPlcItem)int64Item).Value.ByteLength);

            UInt16PlcItem uInt16Item = itemBuilder
                                       .ConstructUInt16PlcItem("UInt16")
                                       .AtDatablock(0)
                                       .AtPosition(1)
                                       .WithoutInitialValue()
                                       .Build()
            ;

            Assert.AreEqual((uint)sizeof(UInt16), ((IPlcItem)uInt16Item).Value.ByteLength);

            UInt32PlcItem uInt32PlcItem = itemBuilder
                                          .ConstructUInt32PlcItem("UInt32")
                                          .AtDatablock(0)
                                          .AtPosition(1)
                                          .WithInitialValue(uint.MaxValue)
                                          .Build()
            ;

            Assert.AreEqual((uint)sizeof(UInt32), ((IPlcItem)uInt32PlcItem).Value.ByteLength);

            UInt64PlcItem uInt64PlcItem = itemBuilder
                                          .ConstructUInt64PlcItem("UInt64")
                                          .AtDatablock(0)
                                          .AtPosition(1)
                                          .WithInitialValue(ulong.MaxValue)
                                          .Build()
            ;

            Assert.AreEqual((uint)sizeof(UInt64), ((IPlcItem)uInt64PlcItem).Value.ByteLength);

            WordPlcItem wordItem = itemBuilder
                                   .ConstructWordPlcItem("Word")
                                   .AtDatablock(0)
                                   .AtPosition(2)
                                   .WithInitialValue(32458)
                                   .Build()
            ;

            Assert.AreEqual((uint)2, ((IPlcItem)wordItem).Value.ByteLength);

            DWordPlcItem dwordItem = itemBuilder
                                     .ConstructDWordPlcItem("DWord")
                                     .AtDatablock(0)
                                     .AtPosition(2)
                                     .WithInitialValue(uint.MaxValue)
                                     .Build()
            ;

            Assert.AreEqual((uint)4, ((IPlcItem)dwordItem).Value.ByteLength);

            LWordPlcItem lwordItem = itemBuilder
                                     .ConstructLWordPlcItem("LWord")
                                     .AtDatablock(0)
                                     .AtPosition(2)
                                     .WithInitialValue(ulong.MaxValue)
                                     .Build()
            ;

            Assert.AreEqual((uint)8, ((IPlcItem)lwordItem).Value.ByteLength);

            TextPlcItem textItem = itemBuilder
                                   .ConstructTextPlcItem("Text")
                                   .WithEncoding(Encoding.UTF7)
                                   .AtDatablock(0)
                                   .AtPosition(3)
                                   .WithInitialValue("Some String")
                                   .Build()
            ;

            Assert.AreEqual((uint)Encoding.UTF7.GetBytes("Some String").Length, ((IPlcItem)textItem).Value.ByteLength);

            Utf8PlcItem utf8Item = itemBuilder
                                   .ConstructUtf8PlcItem("UTF-8")
                                   .AtDatablock(0)
                                   .AtPosition(4)
                                   .WithLength(10)
                                   .Build()
            ;

            Assert.AreEqual((uint)10, ((IPlcItem)utf8Item).Value.ByteLength);

            var initialText = "String whose length fits into a single byte.";
            DynamicUtf8PlcItem secondDynamicUtf8Item = itemBuilder
                                                       .ConstructUtf8PlcItem("UTF-8")
                                                       .AtDatablock(0)
                                                       .AtPosition(4)
                                                       .WithDynamicItemFromInitialValue(initialText)
                                                       .BuildDynamic()
            ;

            Assert.That(secondDynamicUtf8Item.LengthPlcItem.Value, Is.EqualTo((uint)Encoding.UTF8.GetBytes(initialText).Length));
            Assert.AreEqual(initialText, secondDynamicUtf8Item.Value);

            var items = new []
            {
                plcItem,
                bitsItem,
                bitItem,
                bytesItem,
                byteItem,
                int16Item,
                int32Item,
                int64Item,
                uInt16Item,
                uInt32PlcItem,
                uInt64PlcItem,
                wordItem,
                dwordItem,
                lwordItem,
                textItem,
                utf8Item,
                secondDynamicUtf8Item,
            };

            foreach (var item in items)
            {
                Debug.WriteLine($" -> {item}");
            }
        }