Esempio n. 1
0
        public void TokenFunctionTest(string equalsToken, string plusToken, int collectionSize)
        {
            var kvps = new KeyValuePair <string, string> [collectionSize];

            Random rand1 = new Random();

            for (int i = 0; i < collectionSize; i++)
            {
                kvps[i] = new KeyValuePair <string, string>($"key{i}", $"{Guid.NewGuid()}{Guid.NewGuid()}{Guid.NewGuid()}");
            }

            string value1 = kvps[rand1.Next(0, collectionSize - 1)].Value;
            string value2 = kvps[rand1.Next(0, collectionSize - 1)].Value;

            string queryString = $"value{equalsToken}{value1}{plusToken}{value2}";

            var sw         = Stopwatch.StartNew();
            var expression = ExpressionBuilder.BuildFunction <KeyValuePair <string, string> >(queryString);

            kvps.FirstOrDefault(expression).Should().NotBeNull().And
            .Should().BeAnyOf(kvps.Where(kvp => kvp.Value == value1 || kvp.Value == value2));

            kvps.Where(expression).Should().NotBeNullOrEmpty().And
            .Contain(kvps.Where(kvp => kvp.Value == value1 || kvp.Value == value2));

            kvps.Any(expression).Should().BeTrue();
            kvps.All(expression).Should().BeFalse();
            kvps.Count(expression).Should().Be(kvps.Count(q => q.Value == value1 || q.Value == value2));

            sw.Stop();
        }
Esempio n. 2
0
        public void Document_copies_properties_to_KeyValue_array()
        {
            // ARRANGE
            // create a Bson document with all possible value types

            var document = new BsonDocument();

            document.Add("string", new BsonValue("string"));
            document.Add("bool", new BsonValue(true));
            document.Add("objectId", new BsonValue(ObjectId.NewObjectId()));
            document.Add("DateTime", new BsonValue(DateTime.Now));
            document.Add("decimal", new BsonValue((decimal)1));
            document.Add("double", new BsonValue((double)1.0));
            document.Add("guid", new BsonValue(Guid.NewGuid()));
            document.Add("int", new BsonValue((int)1));
            document.Add("long", new BsonValue((long)1));
            document.Add("bytes", new BsonValue(new byte[] { (byte)1 }));
            document.Add("bsonDocument", new BsonDocument());

            // ACT
            // copy all properties to destination array

            var result = new KeyValuePair <string, BsonValue> [document.Count()];

            document.CopyTo(result, 0);

            // ASSERT
            // all BsonValue instances have been added to the array by reference

            Assert.IsTrue(result.All(kv => object.ReferenceEquals(document.Get(kv.Key), kv.Value)));
        }
Esempio n. 3
0
        public void IntTest(int collectionSize)
        {
            var kvps = new KeyValuePair <string, int> [collectionSize];

            Random rand1 = new Random();

            for (int i = 0; i < collectionSize; i++)
            {
                Random rand2 = new Random(rand1.Next(0, int.MaxValue));

                kvps[i] = new KeyValuePair <string, int>($"key{i}", rand2.Next(1, collectionSize));
            }

            int value1 = kvps[rand1.Next(0, collectionSize - 1)].Value;
            int value2 = kvps[rand1.Next(0, collectionSize - 1)].Value;

            string queryString = $"value={value1}%2B{value2}";

            var sw         = Stopwatch.StartNew();
            var expression = ExpressionBuilder.BuildFunction <KeyValuePair <string, int> >(queryString);

            kvps.FirstOrDefault(expression).Should().NotBeNull().And
            .Should().BeAnyOf(kvps.Where(kvp => kvp.Value == value1 || kvp.Value == value2));

            kvps.Where(expression).Should().NotBeNullOrEmpty().And
            .Contain(kvps.Where(kvp => kvp.Value == value1 || kvp.Value == value2));

            kvps.Any(expression).Should().BeTrue();
            kvps.All(expression).Should().BeFalse();
            kvps.Count(expression).Should().Be(kvps.Count(q => q.Value == value1 || q.Value == value2));

            sw.Stop();
        }
Esempio n. 4
0
        public void DateTimeListTest(int collectionSize, int maxSubCollectionSize)
        {
            var kvps = new KeyValuePair <string, List <DateTime> > [collectionSize];

            int day   = 1;
            int month = 1;
            int year  = 2016;

            for (int i = 0; i < collectionSize; i++)
            {
                string key = $"key{i}";

                KeyValuePair <string, List <DateTime> > kvp = new KeyValuePair <string, List <DateTime> >(key, new List <DateTime>());

                for (int j = 0; j < maxSubCollectionSize; j++)
                {
                    kvp.Value.Add(new DateTime(year, month, day++));

                    if (day > 28)
                    {
                        day = 1;
                        month++;
                    }

                    if (month <= 12)
                    {
                        continue;
                    }

                    month = 1;
                    year++;
                }

                kvps[i] = kvp;
            }

            Random rand = new Random();

            DateTime value1 = kvps[rand.Next(0, collectionSize - 1)].Value[0];
            DateTime value2 = kvps[rand.Next(0, collectionSize - 1)].Value[0];

            string queryString = $"value={value1}%2B{value2}";

            var sw = Stopwatch.StartNew();

            var expression = ExpressionBuilder.BuildFunction <KeyValuePair <string, List <DateTime> > >(queryString);

            kvps.FirstOrDefault(expression).Should().NotBeNull().And
            .Should().BeAnyOf(kvps.Where(kvp => kvp.Value.Contains(value1) || kvp.Value.Contains(value2)));

            kvps.Where(expression).Should().NotBeNullOrEmpty().And
            .Contain(kvps.Where(kvp => kvp.Value.Contains(value1) || kvp.Value.Contains(value2)));

            kvps.Any(expression).Should().BeTrue();
            kvps.All(expression).Should().BeFalse();
            kvps.Count(expression).Should().Be(kvps.Count(q => q.Value.Contains(value1) || q.Value.Contains(value2)));

            sw.Stop();
        }
Esempio n. 5
0
 public void CopyTo(KeyValuePair<object, ISmartLight>[] array, int arrayIndex)
 {
     if (array.All(x => x.Key is short && x.Value is ISmartLight))
     {
         foreach(KeyValuePair<object, ISmartLight> item in array)
         {
             //lights
         }
     }
     else
     {
         throw new Exception("All KeyValuePairs need to have a short as key and a Lyte as value for this controller");
     }
 }