Permalink
Browse files

Added support for writing ArraySignatures

  • Loading branch information...
1 parent 7ee025b commit e2b748d2f5d965546e5446a48238be420435f998 unknown committed Feb 25, 2012
Showing with 67 additions and 34 deletions.
  1. +12 −2 src/Tao/Metadata/Signatures/ArraySignature.n
  2. +55 −32 src/Tests/Signatures/ArrayShapeTests.n
@@ -20,13 +20,23 @@ namespace Tao
{
// Read the array type
ArrayType = TypeSignature.ReadFrom(stream);
-
-
+
// Read the shape
def reader = ArrayShapeReader();
Shape = reader.ReadFrom(stream);
}
+ protected override Write(elementType : ElementType, [NotNull] stream : Stream) : void
+ requires ArrayType != null
+ requires ArrayShape != null
+ {
+ // Write the array type
+ ArrayType.Write(stream);
+
+ def writer = ArrayShapeWriter();
+ writer.Write(Shape, stream);
+ }
+
protected override SupportsElement(elementType : ElementType) : bool
{
elementType == ElementType.Array;
@@ -66,15 +66,66 @@ namespace Tests
currentLoBounds[1].ShouldBeEqualTo(loBounds[1], "Invalid LoBound");
}
public ShouldBeAbleToReadArraySignature() : void
+ {
+ def stream = CreateSampleSignatureStream();
+
+ def signature = TypeSignature.ReadFrom(stream) :> ArraySignature;
+ def resultShape = signature.Shape;
+
+ def arrayType = signature.ArrayType;
+
+ signature.ElementType.ShouldBeEqualTo(ElementType.Array, "Expected: Array Element Type");
+ arrayType.ElementType.ShouldBeEqualTo(ElementType.I4, "Wrong array type");
+
+ def currentSizes = List.[uint](resultShape.Sizes);
+ def currentLoBounds = List.[uint](resultShape.LoBounds);
+
+ def rank = 1 : uint;
+ resultShape.Rank.ShouldBeEqualTo(rank, "Invalid rank");
+ currentSizes.Count.ShouldBeEqualTo(2, "Invalid SizeCount");
+ currentSizes[0].ShouldBeEqualTo(1, "Invalid Sizes");
+ currentSizes[1].ShouldBeEqualTo(2, "Invalid Sizes");
+
+ def numLowBounds = resultShape.LoBounds.Count();
+ numLowBounds.ShouldBeEqualTo(currentLoBounds.Count, "Invalid LoBoundCount");
+ currentLoBounds[0].ShouldBeEqualTo(0, "Invalid LoBound");
+ currentLoBounds[1].ShouldBeEqualTo(1, "Invalid LoBound");
+ }
+ public ShouldBeAbleToWriteArraySignature() : void
+ {
+ def signature = ArraySignature();
+ signature.ElementType = ElementType.Array;
+
+ def arrayType = TypeSignature();
+ arrayType.ElementType = ElementType.I4;
+ signature.ArrayType = arrayType;
+
+ signature.Shape = CreateSampleShape();
+
+ def stream = MemoryStream();
+ signature.Write(stream);
+
+ stream.ShouldMatch(CreateSampleSignatureStream());
+ }
+ private CreateSampleSignatureStream() : Stream
{
def stream = MemoryStream();
- // Forge the sample signature
+ // Forge the sample signature stream
def writer = BinaryWriter(stream);
writer.Write(ElementType.Array :> byte);
writer.Write(ElementType.I4 :> byte);
- // Create the sample array shape
+ // Create the sample array shape
+ def shape = CreateSampleShape();
+ def writer = ArrayShapeWriter();
+ writer.Write(shape, stream);
+
+ stream.Seek(0);
+ stream;
+ }
+ private CreateSampleShape() : ArrayShape
+ {
def shape = ArrayShape();
shape.Rank = 1;
@@ -89,35 +140,7 @@ namespace Tests
loBounds.Add(1);
shape.LoBounds = loBounds;
-
- def writer = ArrayShapeWriter();
- writer.Write(shape, stream);
-
- stream.Seek(0);
- def signature = TypeSignature.ReadFrom(stream) :> ArraySignature;
- def resultShape = signature.Shape;
-
- def arrayType = signature.ArrayType;
-
- signature.ElementType.ShouldBeEqualTo(ElementType.Array, "Expected: Array Element Type");
- arrayType.ElementType.ShouldBeEqualTo(ElementType.I4, "Wrong array type");
-
- def currentSizes = List.[uint](resultShape.Sizes);
- def currentLoBounds = List.[uint](resultShape.LoBounds);
-
- def rank = 1 : uint;
- resultShape.Rank.ShouldBeEqualTo(rank, "Invalid rank");
- currentSizes.Count.ShouldBeEqualTo(2, "Invalid SizeCount");
- currentSizes[0].ShouldBeEqualTo(sizes[0], "Invalid Sizes");
- currentSizes[1].ShouldBeEqualTo(sizes[1], "Invalid Sizes");
-
- def numLowBounds = resultShape.LoBounds.Count();
- numLowBounds.ShouldBeEqualTo(currentLoBounds.Count, "Invalid LoBoundCount");
- currentLoBounds[0].ShouldBeEqualTo(loBounds[0], "Invalid LoBound");
- currentLoBounds[1].ShouldBeEqualTo(loBounds[1], "Invalid LoBound");
- }
- public stub ShouldBeAbleToWriteArraySignature() : void
- {
+ shape;
}
private GetExpectedStream() : Stream
{
@@ -128,7 +151,7 @@ namespace Tests
def sizes = array[1 : byte, 2];
def loBounds = array[0 : byte, 1];
- // Create the sample input stream
+ // Create the sample input stream
def stream = MemoryStream();
def integerWriter = CompressedIntegerWriter();

0 comments on commit e2b748d

Please sign in to comment.