Beispiel #1
0
        public void TestNumber()
        {
            {
                Number64 value = 32;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosNumber64        cosmosNumber = (CosmosNumber64)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                sbyte value = 32;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosInt8            cosmosNumber = (CosmosInt8)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                short value = 32;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosInt16           cosmosNumber = (CosmosInt16)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                int value = 32;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosInt32           cosmosNumber = (CosmosInt32)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                long value = 32;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosInt64           cosmosNumber = (CosmosInt64)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                uint value = 32;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosUInt32          cosmosNumber = (CosmosUInt32)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                float value = 32.1337f;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosFloat32         cosmosNumber = (CosmosFloat32)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                double value = 32.1337;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosFloat64         cosmosNumber = (CosmosFloat64)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }
        }
Beispiel #2
0
            public UInt128 Visit(CosmosFloat32 cosmosFloat32, UInt128 seed)
            {
                UInt128 hash  = MurmurHash3.Hash128(CosmosNumberHasher.Float32HashSeed, seed);
                float   value = cosmosFloat32.GetValue();

                // Normalize 0.0f and -0.0f value
                // https://stackoverflow.com/questions/3139538/is-minus-zero-0-equivalent-to-zero-0-in-c-sharp
                if (value == 0.0f)
                {
                    value = 0;
                }

                hash = MurmurHash3.Hash128((UInt128)BitConverter.DoubleToInt64Bits(value), hash);
                return(hash);
            }
        public void VerifyNumberSort()
        {
            List <CosmosElement> manuallySortedList = new List <CosmosElement>()
            {
                CosmosNumber64.Create(-1),
                CosmosNumber64.Create(0),
                CosmosNumber64.Create(1),

                CosmosInt8.Create(-1),
                CosmosInt8.Create(0),
                CosmosInt8.Create(1),

                CosmosInt16.Create(-1),
                CosmosInt16.Create(0),
                CosmosInt16.Create(1),

                CosmosInt32.Create(-1),
                CosmosInt32.Create(0),
                CosmosInt32.Create(1),


                CosmosInt64.Create(-1),
                CosmosInt64.Create(0),
                CosmosInt64.Create(1),

                CosmosUInt32.Create(0),
                CosmosUInt32.Create(1),

                CosmosFloat32.Create(-1),
                CosmosFloat32.Create(0),
                CosmosFloat32.Create(1),

                CosmosFloat64.Create(-1),
                CosmosFloat64.Create(0),
                CosmosFloat64.Create(1),
            };

            VerifySort(manuallySortedList);
        }
Beispiel #4
0
 public void Visit(CosmosFloat32 cosmosFloat32)
 {
     this.stringBuilder.AppendFormat("C_Float32({0:G7})", cosmosFloat32.GetValue());
 }
        public void TestOrderByQueryLiterals()
        {
            StringBuilder sb      = new StringBuilder();
            CosmosElement element = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { "item", CosmosString.Create("asdf") }
            });

            element.Accept(new CosmosElementToQueryLiteral(sb));
            Assert.AreEqual(
                @"{""item"":""asdf""}",
                sb.ToString());

            element = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { "item", CosmosBoolean.Create(true) }
            });
            sb.Clear();
            element.Accept(new CosmosElementToQueryLiteral(sb));
            Assert.AreEqual(
                @"{""item"":true}",
                sb.ToString());

            element = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { "item", CosmosBoolean.Create(false) }
            });
            sb.Clear();
            element.Accept(new CosmosElementToQueryLiteral(sb));
            Assert.AreEqual(
                @"{""item"":false}",
                sb.ToString());

            element = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { "item", CosmosNull.Create() }
            });
            sb.Clear();
            element.Accept(new CosmosElementToQueryLiteral(sb));
            Assert.AreEqual(
                @"{""item"":null}",
                sb.ToString());

            element = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { "item", CosmosNumber64.Create(1.0) }
            });
            sb.Clear();
            element.Accept(new CosmosElementToQueryLiteral(sb));
            Assert.AreEqual(
                @"{""item"":1}",
                sb.ToString());

            element = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { "item", CosmosNumber64.Create(1L) }
            });
            sb.Clear();
            element.Accept(new CosmosElementToQueryLiteral(sb));
            Assert.AreEqual(
                @"{""item"":1}",
                sb.ToString());

            element = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { "item", CosmosInt8.Create(3) },
                { "item2", CosmosInt16.Create(4) },
                { "item3", CosmosInt32.Create(5) },
                { "item5", CosmosUInt32.Create(7) },
                { "item6", CosmosInt64.Create(8) },
                { "item7", CosmosFloat32.Create(9.1f) },
                { "item8", CosmosFloat64.Create(10.2) },
            });
            sb.Clear();
            element.Accept(new CosmosElementToQueryLiteral(sb));
            Assert.AreEqual(
                @"{""item"":C_Int8(3),""item2"":C_Int16(4),""item3"":C_Int32(5),""item5"":C_UInt32(7),""item6"":C_Int64(8)," +
                @"""item7"":C_Float32(9.1),""item8"":C_Float64(10.2)}",
                sb.ToString());

            Guid guid = Guid.NewGuid();

            byte[] randomBytes = Guid.NewGuid().ToByteArray();
            string hexString   = PartitionKeyInternal.HexConvert.ToHex(randomBytes, 0, randomBytes.Length);

            element = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { "item", CosmosGuid.Create(guid) },
                { "item2", CosmosBinary.Create(new ReadOnlyMemory <byte>(randomBytes)) },
            });
            sb.Clear();
            element.Accept(new CosmosElementToQueryLiteral(sb));
            Assert.AreEqual(
                $@"{{""item"":C_Guid(""{guid.ToString()}""),""item2"":C_Binary(""0x{hexString}"")}}",
                sb.ToString());

            // deeply nested arrays and objects
            element = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { "item", CosmosGuid.Create(guid) },

                // empty array
                { "item2", CosmosArray.Create(new CosmosElement[] { }) },

                // empty object
                { "item3", CosmosObject.Create(new Dictionary <string, CosmosElement>()) },

                // array of objects with numbers
                { "item4", CosmosArray.Create(new CosmosElement[]
                    {
                        CosmosObject.Create(new Dictionary <string, CosmosElement>()
                        {
                            { "a", CosmosInt8.Create(3) },
                            { "b", CosmosString.Create("adf") },
                        }),
                        CosmosInt16.Create(25)
                    }) },
            });
            sb.Clear();
            element.Accept(new CosmosElementToQueryLiteral(sb));
            Assert.AreEqual(
                $@"{{""item"":C_Guid(""{guid.ToString()}""),""item2"":[],""item3"":{{}},""item4"":[{{""a"":C_Int8(3),""b"":""adf""}},C_Int16(25)]}}",
                sb.ToString());
        }