public void ListBlobs()
        {
            var prefix = Guid.NewGuid().ToString("N");

            var prefixed   = Range.Array(10).Select(i => prefix + Guid.NewGuid().ToString("N")).ToArray();
            var unprefixed = Range.Array(13).Select(i => Guid.NewGuid().ToString("N")).ToArray();

            foreach (var n in prefixed)
            {
                BlobStorage.PutBlob(ContainerName, n, n);
            }

            foreach (var n in unprefixed)
            {
                BlobStorage.PutBlob(ContainerName, n, n);
            }

            var list = BlobStorage.ListBlobs <string>(ContainerName, prefix).ToArray();

            Assert.AreEqual(prefixed.Length, list.Length, "#A00");

            foreach (var n in list)
            {
                Assert.IsTrue(prefixed.Contains(n), "#A01");
                Assert.IsFalse(unprefixed.Contains(n), "#A02");
            }
        }
Example #2
0
        public void IdempotenceOfDeleteMethod()
        {
            var pkey = Guid.NewGuid().ToString("N");

            var entities = Range.Array(10).Select(i =>
                                                  new CloudEntity <string>
            {
                PartitionKey = pkey,
                RowKey       = Guid.NewGuid().ToString("N"),
                Value        = "nothing"
            }).ToArray();

            // Insert/delete entity.
            TableStorage.Insert(TableName, entities);

            // partial deletion
            TableStorage.Delete <string>(TableName, pkey, entities.Take(5).Select(e => e.RowKey));

            // complete deletion, but with overlap
            TableStorage.Delete <string>(TableName, pkey, entities.Select(e => e.RowKey));

            // checking that all entities have been deleted
            var list = TableStorage.Get <string>(TableName, pkey, entities.Select(e => e.RowKey));

            Assert.That(!list.Any(), "#A00");
        }
        public void ToArray()
        {
            var actual   = Range.Create(10).ToArray((i, index) => i + index);
            var expected = Range.Array(10, i => i * 2);

            CollectionAssert.AreEqual(expected, actual);
        }
        public void ContextTesting()
        {
            const int outerCount = 100000;

            Func <int, double> operation = i =>
            {
                double val   = 0;
                var    limit = Rand.Next(5000, 10000);
                for (int j = 0; j < limit; j++)
                {
                    val = Math.Sin(val + j);
                }
                return(val);
            };

            using (Counter("Lokad"))
            {
                Range
                .Array(outerCount)
                .SelectInParallel(operation);
            }

            var wait = new WaitFor <double>(2.Seconds());

            using (Counter("Lokad+WaitFor"))
            {
                Range
                .Array(outerCount)
                .SelectInParallel(i => wait.Run(() => operation(i)));
            }
        }
Example #5
0
        public void ForEach()
        {
            var i = Rand.Next(20);

            Range.Array(i, n => 1).ForEach(n => i -= 1);
            Assert.AreEqual(i, 0);
        }
        public void IncrementMultiThread()
        {
            var provider = GlobalSetup.Container.Resolve <IBlobStorageProvider>();

            provider.CreateContainerIfNotExist(ContainerName);

            //creating thread parameters
            var counter = new BlobCounter(provider, ContainerName, "SomeBlobName");

            counter.Reset(0);

            var       random       = new Random();
            const int threadsCount = 4;
            var       increments   = Range.Array(threadsCount).Select(e => Range.Array(5).Select(i => random.Next(20)).ToArray()).ToArray();

            var localSums = increments.AsParallel().Select(
                e =>
            {
                var c = new BlobCounter(provider, ContainerName, "SomeBlobName");
                foreach (var increment in e)
                {
                    c.Increment(increment);
                }
                return(e.Sum());
            }).ToList();

            Assert.AreEqual(increments.Sum(i => i.Sum()), localSums.Sum(), "Broken invariant.");
            Assert.AreEqual(localSums.Sum(), counter.GetValue(), "Values should be equal, BlobCounter supposed to be thread-safe");
        }
        public void SelectInParallel_SmokeTest()
        {
            var results = Range
                          .Array(100, i => i)
                          .SelectInParallel(i => i * 2);

            CollectionAssert.AreEquivalent(results, Range.Array(100, i => i * 2));
        }
Example #8
0
        public void Convert_With_Int()
        {
            var ints = Range.Array(Rand.Next(10)).Convert((n, i) => n - i);

            foreach (var i in ints)
            {
                Assert.AreEqual(0, i);
            }
        }
Example #9
0
        public void Append()
        {
            var i = Rand.Next(10);
            var a = Range.Array(i).Append(Range.Array(i));

            for (int j = 0; j < i; j++)
            {
                Assert.AreEqual(a[j], a[j + i]);
            }
        }
        public void SelectInParallel_is_ordered()
        {
            const int count   = 100000;
            var       results = Range.Array(count).SelectInParallel(i => i * 2);

            for (int i = 0; i < count; i++)
            {
                Assert.AreEqual(i * 2, results[i], "#A00");
            }
        }
Example #11
0
        public void Slice_Works_For_Incomplete_Slices()
        {
            var slices = Range.Array(7).SliceArray(4);

            var expected = new[]
            {
                new[] { 0, 1, 2, 3 },
                new[] { 4, 5, 6 }
            };

            CollectionAssert.AreEqual(expected, slices);
        }
Example #12
0
        public void Usage_patterns_for_building_rules()
        {
            var domain = new Domain
            {
                Properties = Range.Array(10, i => i.ToString()),
                Values     = Range.Array(10, i => (double)(i + 1)),
                Property   = "Something",
                Value      = Math.PI
            };

            Enforce.That(() => domain, Usage_Rule);
            Scope.GetMessages(new Domain(), "domain", Usage_Rule);
        }
        public void Test()
        {
            Console.WriteLine("CPUcount: {0}", Environment.ProcessorCount);
            var watch = Stopwatch.StartNew();
            var work  = Range.Array(10, i => i);

            var parallel = work.SelectInParallel(span =>
            {
                SystemUtil.Sleep(span.Seconds());
                return(string.Format("This operation took {0} seconds", span));
            });

            foreach (var span in parallel)
            {
                Console.WriteLine("@{0}s: {1}", watch.Elapsed.TotalSeconds.Round(2), span);
            }

            Console.WriteLine("Total running time: {0}", watch.Elapsed);
        }
        public void SelectInParallel_Exception()
        {
            try
            {
                Range.Array(10)
                .SelectInParallel(i =>
                {
                    if (i == 5)
                    {
                        throw new InvalidOperationException("Part of the test.");
                    }
                    return(i * 2);
                });

                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(typeof(InvalidOperationException), ex.InnerException, "Unexpected ex: {0}", ex.ToString());
            }
        }
        public void Distinct()
        {
            var sequence = Range.Array(10).Distinct(i => i % 3).ToArray();

            CollectionAssert.AreEqual(new[] { 0, 1, 2 }, sequence);
        }
Example #16
0
        static ILInstruction ReadInstruction(BinaryReader il, Module module, Type[] genericTypeArguments,
                                             Type[] genericMethodArguments)
        {
            OpCode code;

            var    position = il.BaseStream.Position;
            ushort value    = il.ReadByte();

            if (value != 0xfe)
            {
                code = _singleByteCodes[value];
            }
            else
            {
                value = il.ReadByte();
                code  = _multiByteCodes[value];
            }

            switch (code.OperandType)
            {
            case OperandType.InlineBrTarget:
                return(From(code, il.ReadInt32(), position));

            case OperandType.InlineField:
                return(From(code, module.ResolveField(il.ReadInt32(), genericTypeArguments, genericMethodArguments), position));

            case OperandType.InlineMethod:
                try
                {
                    return(From(code, module.ResolveMethod(il.ReadInt32(), genericTypeArguments, genericMethodArguments), position));
                }
                catch
                {
                    return(From(code, module.ResolveMember(il.ReadInt32()), position));
                }

            case OperandType.InlineSig:
                return(From(code, module.ResolveSignature(il.ReadInt32()), position));

            case OperandType.InlineType:
                return(From(code, module.ResolveType(il.ReadInt32(), genericTypeArguments, genericMethodArguments), position));

            //instruction.Operand = module.ResolveType(metadataToken, this.mi.DeclaringType.GetGenericArguments(), this.mi.GetGenericArguments());
            case OperandType.InlineI:
                return(From(code, il.ReadInt32(), position));

            case OperandType.InlineI8:
                return(From(code, il.ReadInt64(), position));

            case OperandType.InlineNone:
                return(From(code, 0, position));

            case OperandType.InlineR:
                return(From(code, il.ReadDouble(), position));

            case OperandType.InlineString:
                var stringToken = il.ReadInt32();
                return(From(code, module.ResolveString(stringToken), position));

            case OperandType.InlineSwitch:
                int count     = il.ReadInt32();
                var addresses = Range.Array(count, i => il.ReadInt32());
                return(From(code, addresses, position));

            case OperandType.InlineVar:
                return(From(code, il.ReadUInt16(), position));

            case OperandType.ShortInlineBrTarget:
                return(From(code, il.ReadSByte(), position));

            case OperandType.ShortInlineI:
                return(From(code, il.ReadSByte(), position));

            case OperandType.ShortInlineR:
                return(From(code, il.ReadSingle(), position));

            case OperandType.ShortInlineVar:
                return(From(code, il.ReadByte(), position));

            default:
                throw new InvalidOperationException(code.OperandType.ToString());
            }
        }
        public void SelectValues()
        {
            var array = Range.Array(10, n => Rand.NextMaybe(n));

            Assert.AreEqual(array.Sum(s => s.GetValue(0)), array.SelectValues().Sum());
        }
Example #18
0
        public void Test_Valid_Collection()
        {
            var array = Range.Array(10, i => Program.CreateValid());

            Enforce.That(array, BusinessRules.ProgramRules);
        }