Ejemplo n.º 1
0
        public void testBasics()
        {
            HRow row = BuildRows(
                new[] { "id", "site", "geoAddr", "area", "date", "null" },
                new HVal[] { HRef.make("aaaa-bbbb"), HMarker.VAL, HStr.make("Richmond, Va"), HNum.make(1200, "ft"), HDate.make(2000, 12, 3), null })
                       .First();

            // size
            Assert.AreEqual(row.size(), 6);
            Assert.IsFalse(row.isEmpty());

            // configured tags
            Assert.IsTrue(row.get("id").hequals(HRef.make("aaaa-bbbb")));
            Assert.IsTrue(row.get("site").hequals(HMarker.VAL));
            Assert.IsTrue(row.get("geoAddr").hequals(HStr.make("Richmond, Va")));
            Assert.IsTrue(row.get("area").hequals(HNum.make(1200, "ft")));
            Assert.IsTrue(row.get("date").hequals(HDate.make(2000, 12, 3)));
            Assert.AreEqual(row.get("null", false), null);
            try
            {
                row.get("null");
                Assert.Fail();
            }
            catch (HaystackUnknownNameException)
            {
                Assert.IsTrue(true);
            }

            // missing tag
            Assert.IsFalse(row.has("foo"));
            Assert.IsTrue(row.missing("foo"));
            Assert.IsNull(row.get("foo", false));
            try { row.get("foo"); Assert.Fail(); } catch (HaystackUnknownNameException) { Assert.IsTrue(true); }
            try { row.get("foo", true); Assert.Fail(); } catch (HaystackUnknownNameException) { Assert.IsTrue(true); }
        }
Ejemplo n.º 2
0
 public void testEquality()
 {
     Assert.IsTrue(HNum.make(2).hequals(HNum.make(2.0, null)));
     Assert.IsFalse(HNum.make(2).hequals(HNum.make(2, "%")));
     Assert.IsFalse(HNum.make(2, "%").hequals(HNum.make(2)));
     Assert.IsTrue(HNum.make(0).hequals(HNum.make(0.0)));
 }
        public void testZinc()
        {
            verifyZinc(
                HDict.Empty,
                "{}");
            verifyZinc(
                new HDictBuilder().add("foo_12").toDict(),
                "{foo_12}");
            verifyZinc(
                new HDictBuilder().add("fooBar", 123, "ft").toDict(),
                "{fooBar:123ft}");
            verifyZinc(
                new HDictBuilder().add("dis", "Bob").add("bday", HDate.make(1970, 6, 3)).add("marker").toDict(),
                "{dis:\"Bob\" bday:1970-06-03 marker}");

            // nested dict
            verifyZinc(
                new HDictBuilder().add("auth", HDict.Empty).toDict(),
                "{auth:{}}");
            verifyZinc(
                new HDictBuilder().add("auth",
                                       new HDictBuilder().add("alg", "scram").add("c", 10000).add("marker").toDict()
                                       ).toDict(),
                "{auth:{alg:\"scram\" c:10000 marker}}");

            // nested list
            verifyZinc(
                new HDictBuilder().add("arr", HList.make(new HVal[] { HNum.make(1.0), HNum.make(2), HNum.make(3) }))
                .add("x").toDict(),
                "{arr:[1,2,3] x}"); // Was "{arr:[1.0,2,3] x}" - double in .NET will not recognise the difference between 1.0 and 1
        }
Ejemplo n.º 4
0
        public void testZinc()
        {
            verifyZinc(
                HRow.Empty,
                "{}");
            verifyZinc(
                BuildRows(new[] { "fooBar" }, new[] { HNum.make(123, "ft") }).First(),
                "{fooBar:123ft}");
            verifyZinc(
                BuildRows(new[] { "dis", "bday", "marker" }, new HVal[] { HStr.make("Bob"), HDate.make(1970, 6, 3), HMarker.VAL }).First(),
                "{dis:\"Bob\" bday:1970-06-03 marker}");

            // nested dict
            verifyZinc(
                BuildRows(new[] { "auth" }, new HVal[] { HDict.Empty }).First(),
                "{auth:{}}");
            verifyZinc(
                BuildRows(new[] { "auth" }, BuildRows(new[] { "alg", "c", "marker" }, new HVal[] { HStr.make("scram"), HNum.make(10000), HMarker.VAL }).ToArray()).First(),
                "{auth:{alg:\"scram\" c:10000 marker}}");

            // nested list
            verifyZinc(
                BuildRows(new[] { "arr", "x" }, new HVal[] { HList.make(new HVal[] { HNum.make(1.0), HNum.make(2), HNum.make(3) }), HMarker.VAL }).First(),
                "{arr:[1,2,3] x}"); // Was "{arr:[1.0,2,3] x}" - double in .NET will not recognise the difference between 1.0 and 1
        }
Ejemplo n.º 5
0
        public void testFormatDecimalWithDot()
        {
            string defaultLanguage = CultureInfo.InvariantCulture.ToString();

            Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
            verifyZinc(HNum.make(2.4), "2.4");
            Thread.CurrentThread.CurrentCulture = new CultureInfo(defaultLanguage);
        }
Ejemplo n.º 6
0
        public void testSimple()
        {
            HGridBuilder b = new HGridBuilder();

            b.addCol("id");
            b.addCol("dis");
            b.addCol("area");
            b.addRow(new HVal[] { HRef.make("a"), HStr.make("Alpha"), HNum.make(1200) });
            b.addRow(new HVal[] { HRef.make("b"), null, HNum.make(1400) });

            // meta
            HGrid g = b.toGrid();

            Assert.AreEqual(g.meta.size(), 0);

            // cols
            //HCol c;
            Assert.AreEqual(g.numCols, 3);
            verifyCol(g, 0, "id");
            verifyCol(g, 1, "dis");
            verifyCol(g, 2, "area");

            // rows
            Assert.AreEqual(g.numRows, 2);
            Assert.IsFalse(g.isEmpty());
            HRow r;

            r = g.row(0);
            Assert.IsTrue(r.get("id").hequals(HRef.make("a")));
            Assert.IsTrue(r.get("dis").hequals(HStr.make("Alpha")));
            Assert.IsTrue(r.get("area").hequals(HNum.make(1200)));
            r = g.row(1);
            Assert.IsTrue(r.get("id").hequals(HRef.make("b")));
            Assert.IsNull(r.get("dis", false));
            Assert.IsTrue(r.get("area").hequals(HNum.make(1400)));
            try { r.get("dis"); Assert.Fail(); } catch (UnknownNameException) { Assert.IsTrue(true); }
            Assert.IsNull(r.get("fooBar", false));
            try { r.get("fooBar"); Assert.Fail(); } catch (UnknownNameException) { Assert.IsTrue(true); }

            // HRow no-nulls
            HRow it = g.row(0);

            Assert.IsFalse(it.Size > 3);
            verifyRowIterator(it, 0, "id", HRef.make("a"));
            verifyRowIterator(it, 1, "dis", HStr.make("Alpha"));
            verifyRowIterator(it, 2, "area", HNum.make(1200));


            // HRow with nulls
            it = g.row(1);
            Assert.IsFalse(it.Size > 3);
            verifyRowIterator(it, 0, "id", HRef.make("b"));
            verifyRowIterator(it, 2, "area", HNum.make(1400));

            // iterating
            verifyGridIterator(g);
        }
Ejemplo n.º 7
0
        protected override HGrid onReadAll(String filter, int limit)
        {
            HGridBuilder b = new HGridBuilder();

            b.addCol("filter");
            b.addCol("limit");
            b.addRow(new HVal[] { HStr.make(filter), HNum.make(limit) });
            HGrid req = b.toGrid();

            return(call("read", req));
        }
Ejemplo n.º 8
0
 public void testBadUnitConstruction()
 {
     string[] badunitNames = new string[]
     {
         "foo bar",
         "foo,bar"
     };
     foreach (string curUnit in badunitNames)
     {
         HNum.make(123.4, curUnit);
     }
 }
Ejemplo n.º 9
0
        public Task <HGrid> readAllAsync(string filter, int limit)
        {
            HGridBuilder b = new HGridBuilder();

            b.addCol("filter");
            if (limit > 0)
            {
                b.addCol("limit");
                b.addRow(new HVal[] { HStr.make(filter), HNum.make(limit) });
            }
            else
            {
                b.addRow(new HVal[] { HStr.make(filter) });
            }
            HGrid req = b.toGrid();

            return(CallAsync("read", req, "text/zinc"));
        }
Ejemplo n.º 10
0
        public void testWriter()
        {
            HGridBuilder gb = new HGridBuilder();

            gb.addCol("a");
            gb.addCol("b");
            gb.addRow(new HVal[] { HNA.VAL, HBool.TRUE }); // Original Unit test had null - this is not valid
            gb.addRow(new HVal[] { HMarker.VAL, HNA.VAL });
            gb.addRow(new HVal[] { HRemove.VAL, HNA.VAL });
            gb.addRow(new HVal[] { HStr.make("test"), HStr.make("with:colon") });
            gb.addRow(new HVal[] { HNum.make(12), HNum.make(72.3, "\u00b0F") });
            gb.addRow(new HVal[] { HNum.make(double.NegativeInfinity), HNum.make(Double.NaN) });
            gb.addRow(new HVal[] { HDate.make(2015, 6, 9), HTime.make(1, 2, 3) });
            var tz = HTimeZone.make("UTC", true);

            gb.addRow(new HVal[] { HDateTime.make(634429600180690000L, tz), HUri.make("foo.txt") });
            gb.addRow(new HVal[] { HRef.make("abc"), HRef.make("abc", "A B C") });
            gb.addRow(new HVal[] { HBin.make("text/plain"), HCoord.make(90, -123) });
            HGrid grid = gb.toGrid();

            string actual = HJsonWriter.gridToString(grid);

            // System.out.println(actual);
            string[] lines = actual.Split('\n');
            Assert.AreEqual(lines[0], "{");
            Assert.AreEqual(lines[1], "\"meta\": {\"ver\":\"2.0\"},");
            Assert.AreEqual(lines[2], "\"cols\":[");
            Assert.AreEqual(lines[3], "{\"name\":\"a\"},");
            Assert.AreEqual(lines[4], "{\"name\":\"b\"}");
            Assert.AreEqual(lines[5], "],");
            Assert.AreEqual(lines[6], "\"rows\":[");
            Assert.AreEqual(lines[7], "{\"a\":\"z:\", \"b\":true},");
            Assert.AreEqual(lines[8], "{\"a\":\"m:\", \"b\":\"z:\"},");
            Assert.AreEqual(lines[9], "{\"a\":\"x:\", \"b\":\"z:\"},");
            Assert.AreEqual(lines[10], "{\"a\":\"test\", \"b\":\"s:with:colon\"},");
            Assert.AreEqual(lines[11], "{\"a\":\"n:12\", \"b\":\"n:72.3 \u00b0F\"},");
            Assert.AreEqual(lines[12], "{\"a\":\"n:-INF\", \"b\":\"n:NaN\"},");
            Assert.AreEqual(lines[13], "{\"a\":\"d:2015-06-09\", \"b\":\"h:01:02:03\"},");
            Assert.AreEqual(lines[14], "{\"a\":\"t:2011-06-06T12:26:58.069Z UTC\", \"b\":\"u:foo.txt\"},");
            Assert.AreEqual(lines[15], "{\"a\":\"r:abc\", \"b\":\"r:abc A B C\"},");
            Assert.AreEqual(lines[16], "{\"a\":\"b:text/plain\", \"b\":\"c:90.0,-123.0\"}");
            Assert.AreEqual(lines[17], "]");
            Assert.AreEqual(lines[18], "}");
        }
Ejemplo n.º 11
0
 private HaystackToken number(string s, int unitIndex)
 {
     try
     {
         if (unitIndex == 0)
         {
             m_val = HNum.make(Double.Parse(s, m_numberFormat));
         }
         else
         {
             string doubleStr = s.Substring(0, unitIndex);
             string unitStr   = s.Substring(unitIndex);
             m_val = HNum.make(Double.Parse(doubleStr, m_numberFormat), unitStr);
         }
     }
     catch (Exception)
     {
         err("[HaystackTokenizer::number]Invalid Number literal: " + s);
     }
     return(HaystackToken.num);
 }
        public void testBasics()
        {
            HDict tags = new HDictBuilder()
                         .add("id", HRef.make("aaaa-bbbb"))
                         .add("site")
                         .add("geoAddr", "Richmond, Va")
                         .add("area", 1200, "ft")
                         .add("date", HDate.make(2000, 12, 3))
                         .add("null", (HVal)null)
                         .toDict();

            // size
            Assert.AreEqual(tags.size(), 5);
            Assert.IsFalse(tags.isEmpty());

            // configured tags
            Assert.IsTrue(tags.get("id").hequals(HRef.make("aaaa-bbbb")));
            Assert.IsTrue(tags.get("site").hequals(HMarker.VAL));
            Assert.IsTrue(tags.get("geoAddr").hequals(HStr.make("Richmond, Va")));
            Assert.IsTrue(tags.get("area").hequals(HNum.make(1200, "ft")));
            Assert.IsTrue(tags.get("date").hequals(HDate.make(2000, 12, 3)));
            Assert.AreEqual(tags.get("null", false), null);
            try
            {
                tags.get("null");
                Assert.Fail();
            }
            catch (HaystackUnknownNameException)
            {
                Assert.IsTrue(true);
            }

            // missing tag
            Assert.IsFalse(tags.has("foo"));
            Assert.IsTrue(tags.missing("foo"));
            Assert.IsNull(tags.get("foo", false));
            try { tags.get("foo"); Assert.Fail(); } catch (HaystackUnknownNameException) { Assert.IsTrue(true); }
            try { tags.get("foo", true); Assert.Fail(); } catch (HaystackUnknownNameException) { Assert.IsTrue(true); }
        }
Ejemplo n.º 13
0
        public void testZinc()
        {
            verifyZinc(HNum.make(123), "123");
            verifyZinc(HNum.make(123.4, "m/s"), "123.4m/s");
            verifyZinc(HNum.make(9.6, "m/s"), "9.6m/s");
            verifyZinc(HNum.make(-5.2, "\u00b0F"), "-5.2\u00b0F");
            verifyZinc(HNum.make(23, "%"), "23%");
            verifyZinc(HNum.make(2.4e-3, "fl_oz"), "0.0024fl_oz");
            verifyZinc(HNum.make(2.4e5, "$"), "240000$");
            Assert.IsTrue(read("1234.56fl_oz").hequals(HNum.make(1234.56, "fl_oz")));
            Assert.IsTrue(read("0.000028fl_oz").hequals(HNum.make(0.000028, "fl_oz")));

            // specials
            verifyZinc(HNum.make(double.NegativeInfinity), "-INF");
            verifyZinc(HNum.make(double.PositiveInfinity), "INF");
            verifyZinc(HNum.make(double.NaN), "NaN");

            // verify units never serialized for special values
            Assert.AreEqual(HNum.make(double.NaN, "ignore").toZinc(), "NaN");
            Assert.AreEqual(HNum.make(double.PositiveInfinity, "%").toZinc(), "INF");
            Assert.AreEqual(HNum.make(double.NegativeInfinity, "%").toZinc(), "-INF");
        }
Ejemplo n.º 14
0
        //////////////////////////////////////////////////////////////////////////
        // PointWrite
        //////////////////////////////////////////////////////////////////////////

        /**
         * Write to a given level of a writable point, and return the current status
         * of a writable point's priority array (see pointWriteArray()).
         *
         * @param id Ref identifier of writable point
         * @param level Number from 1-17 for level to write
         * @param val value to write or null to auto the level
         * @param who optional username performing the write, otherwise user dis is used
         * @param dur Number with duration unit if setting level 8
         */
        public HGrid pointWrite(HRef id, int level, string who,
                                HVal val, HNum dur)
        {
            HGridBuilder b = new HGridBuilder();

            b.addCol("id");
            b.addCol("level");
            b.addCol("who");
            b.addCol("val");
            b.addCol("duration");

            b.addRow(new HVal[] {
                id,
                HNum.make(level),
                HStr.make(who),
                val,
                dur
            });

            HGrid req = b.toGrid();
            HGrid res = call("pointWrite", req);

            return(res);
        }
Ejemplo n.º 15
0
 protected virtual HNum n(double val, string unit)
 {
     return(HNum.make(val, unit));
 }
Ejemplo n.º 16
0
 protected virtual HNum n(double val)
 {
     return(HNum.make(val));
 }
Ejemplo n.º 17
0
 protected virtual HNum n(long val)
 {
     return(HNum.make(val));
 }
Ejemplo n.º 18
0
 public void testComplexString()
 {
     verifyToks(
         @"{ id: ""1234:ab"", date: 2019-01-01T12:13:14 UTC, num: 100ms, def: ^tagdef }",
         new object[] {
         HaystackToken.lbrace, null,
         HaystackToken.id, "id", HaystackToken.colon, null, HaystackToken.str, HRef.make("1234:ab"),
         HaystackToken.comma, null,
         HaystackToken.id, "date", HaystackToken.colon, null, HaystackToken.dateTime, HDateTime.make("2019-01-01T12:13:14 UTC", false),
         HaystackToken.comma, null,
         HaystackToken.id, "num", HaystackToken.colon, null, HaystackToken.num, HNum.make(100, "ms"),
         HaystackToken.comma, null,
         HaystackToken.id, "def", HaystackToken.colon, null, HaystackToken.id, "^tagdef",
         HaystackToken.rbrace, null,
     });
 }
Ejemplo n.º 19
0
        private HaystackToken num()
        {
            string strDecNumSep = m_numberFormat.NumberDecimalSeparator;
            char   cNumSep      = '.';

            if (strDecNumSep.Length == 1)
            {
                cNumSep = strDecNumSep[0];
            }
            bool bHex = false;

            if (m_cCur < 0)
            {
                err("unexpected eof in num");
            }
            if (!(m_cCur < 0 || m_cPeek < 0))
            {
                // hex number (no unit allowed)
                bHex = (char)m_cCur == '0' && (char)m_cPeek == 'x';
            }
            bool bExit = false;

            if (bHex)
            {
                consume('0');
                consume('x');
                StringBuilder sb01 = new StringBuilder();
                bExit = false;
                while (!bExit)
                {
                    if (HaystackTokenizer.isHex(m_cCur))
                    {
                        sb01.Append((char)m_cCur);
                        consume();
                    }
                    else if (m_cCur == '_')
                    {
                        consume();
                    }
                    else
                    {
                        bExit = true;
                    }
                }
                m_val = HNum.make(Int64.Parse(sb01.ToString(), System.Globalization.NumberStyles.AllowHexSpecifier));
                return(HaystackToken.num);
            }
            // consume all things that might be part of this number token
            StringBuilder s = new StringBuilder().Append((char)m_cCur);

            consume();
            int  colons    = 0;
            int  dashes    = 0;
            int  unitIndex = 0;
            bool exp       = false;

            bExit = false;
            while (!bExit)
            {
                if (m_cCur >= 0) // Check for eof
                {
                    if (!char.IsDigit((char)m_cCur))
                    {
                        if (exp && (m_cCur == '+' || m_cCur == '-'))
                        {
                        }
                        else if (m_cCur == '-')
                        {
                            ++dashes;
                        }
                        else if ((m_cPeek >= 0) && (m_cCur == ':') && (char.IsDigit((char)m_cPeek)))
                        {
                            ++colons;
                        }
                        else if ((exp || colons >= 1) && m_cCur == '+')
                        {
                        }
                        else if (m_cCur == cNumSep)
                        {
                            if (m_cPeek >= 0)
                            {
                                if (!char.IsDigit((char)m_cPeek))
                                {
                                    break;
                                }
                            }
                        }
                        else if ((m_cPeek >= 0) && (((char)m_cCur == 'e' || (char)m_cCur == 'E') && ((char)m_cPeek == '-' || (char)m_cPeek == '+' || char.IsDigit((char)m_cPeek))))
                        {
                            exp = true;
                        }
                        else if (char.IsLetter((char)m_cCur) || m_cCur == '%' || m_cCur == '$' || m_cCur == '/' || m_cCur > 128)
                        {
                            if (unitIndex == 0)
                            {
                                unitIndex = s.Length;
                            }
                        }
                        else if ((m_cPeek >= 0) && (m_cCur == '_'))
                        {
                            if (unitIndex == 0 && char.IsDigit((char)m_cPeek))
                            {
                                consume();
                                continue;
                            }
                            else
                            {
                                if (unitIndex == 0)
                                {
                                    unitIndex = s.Length;
                                }
                            }
                        }
                        else
                        {
                            bExit = true;
                        }
                    }
                }
                else
                {
                    bExit = true;
                }
                if (!bExit)
                {
                    s.Append((char)m_cCur);
                    consume();
                }
            }

            if (dashes == 2 && colons == 0)
            {
                return(date(s.ToString()));
            }
            if (dashes == 0 && colons >= 1)
            {
                return(time(s, colons == 1));
            }
            if (dashes >= 2)
            {
                return(dateTime(s));
            }
            return(number(s.ToString(), unitIndex));
        }
Ejemplo n.º 20
0
 private HNum n(long val)
 {
     return(HNum.make(val));
 }
Ejemplo n.º 21
0
 private HNum n(double val)
 {
     return(HNum.make(val));
 }
Ejemplo n.º 22
0
 private HNum n(double val, String unit)
 {
     return(HNum.make(val, unit));
 }
Ejemplo n.º 23
0
 public void testCompare()
 {
     Assert.IsTrue(HNum.make(9).compareTo(HNum.make(11)) < 0);
     Assert.IsTrue(HNum.make(-3).compareTo(HNum.make(-4)) > 0);
     Assert.AreEqual(HNum.make(-23).compareTo(HNum.make(-23)), 0);
 }