private static string HashLogToString(IIonValue hashLog)
        {
            bool          multipleEntries = hashLog.Count > 1;
            StringBuilder sb = new StringBuilder();

            sb.Append("(");
            IEnumerator <IIonValue> enumerator = hashLog.GetEnumerator();

            while (enumerator.MoveNext())
            {
                IIonValue hashCall = enumerator.Current;
                if (multipleEntries)
                {
                    sb.Append("\n  ");
                }

                foreach (SymbolToken annotation in hashCall.GetTypeAnnotationSymbols())
                {
                    sb.Append(annotation.Text).Append("::");
                }

                sb.Append("(");
                int cnt = 0;
                IEnumerator <IIonValue> bytesEnum = hashCall.GetEnumerator();
                while (bytesEnum.MoveNext())
                {
                    IIonValue i = bytesEnum.Current;
                    if (cnt++ > 0)
                    {
                        sb.Append(" ");
                    }

                    sb.Append(i.IntValue.ToString("x2"));
                }

                sb.Append(")");
            }

            if (multipleEntries)
            {
                sb.Append("\n");
            }

            sb.Append(")");
            return(sb.ToString());
        }
        public IEnumerable <object[]> GetData(MethodInfo methodInfo)
        {
            var dataList = new List <object[]>();

            var loader          = IonLoader.Default;
            var file            = DirStructure.IonHashDotnetTestFile("ion_hash_tests.ion");
            var ionHashTests    = loader.Load(file);
            var testsEnumerator = ionHashTests.GetEnumerator();

            while (testsEnumerator.MoveNext())
            {
                IIonValue testCase = testsEnumerator.Current;

                string testName = "unknown";
                if (testCase.ContainsField("ion"))
                {
                    testName = testCase.GetField("ion").ToPrettyString();
                }

                IReadOnlyCollection <SymbolToken> annotations = testCase.GetTypeAnnotationSymbols();
                if (annotations.Count > 0)
                {
                    testName = annotations.ElementAt(0).Text;
                }

                IIonValue expect           = testCase.GetField("expect");
                var       expectEnumerator = expect.GetEnumerator();
                while (expectEnumerator.MoveNext())
                {
                    IIonValue expectedHashLog = expectEnumerator.Current;
                    String    hasherName      = expectedHashLog.FieldNameSymbol.Text;

                    object[] data = new object[] {
                        hasherName.Equals("identity") ? testName : testName + "." + hasherName,
                        testCase,
                        expectedHashLog,
                        TestIonHasherProvider.GetInstance(hasherName)
                    };
                    dataList.Add(data);
                }
            }

            return(dataList);
        }
        private static IIonValue FilterHashLog(IIonValue actualHashLog, IIonValue expectedHashLog)
        {
            HashSet <string>        methodCalls = new HashSet <string>();
            IEnumerator <IIonValue> enumerator  = expectedHashLog.GetEnumerator();

            while (enumerator.MoveNext())
            {
                IIonValue v = enumerator.Current;
                foreach (SymbolToken annotation in v.GetTypeAnnotationSymbols())
                {
                    methodCalls.Add(annotation.Text);
                }
            }

            IValueFactory valueFactory = new ValueFactory();
            IIonValue     result       = valueFactory.NewEmptySexp();

            if (methodCalls.Count == 1 && methodCalls.Contains("final_digest"))
            {
                IIonValue finalDigest = actualHashLog.GetElementAt(actualHashLog.Count - 1);
                finalDigest.ClearAnnotations();
                finalDigest.AddTypeAnnotation("final_digest");
                result.Add(finalDigest);
            }
            else
            {
                enumerator = actualHashLog.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    IIonValue v          = enumerator.Current;
                    String    methodCall = v.GetTypeAnnotationSymbols().ElementAt(0).Text;
                    if (methodCalls.Contains(methodCall))
                    {
                        result.Add(v);
                    }
                }
            }

            return(result);
        }
Example #4
0
        public static void AssertHasAnnotation(string annotation, IIonValue ionValue)
        {
            var annotations = string.Join(",", ionValue.GetTypeAnnotationSymbols().Select(a => a.Text));

            Assert.IsTrue(ionValue.HasAnnotation(annotation), "seeking " + annotation + " in [" + annotations + "]");
        }
Example #5
0
        private static int IonCompare(IIonValue v1, IIonValue v2, bool strict)
        {
            if (v1 == null || v2 == null)
            {
                if (v1 == null && v2 == null)
                {
                    return(0);
                }
                return(v1 == null ? -1 : 1);
            }

            var result = v1.Type.CompareTo(v2.Type);

            if (result != 0)
            {
                return(result);
            }

            var bo1 = v1.IsNull;
            var bo2 = v2.IsNull;

            if (bo1 || bo2)
            {
                if (!bo1)
                {
                    result = 1;
                }

                if (!bo2)
                {
                    result = -1;
                }
            }
            else
            {
                switch (v1.Type)
                {
                case IonType.Null:
                    break;

                case IonType.Bool:
                    result = ((IIonBool)v1).BooleanValue.CompareTo(((IIonBool)v2).BooleanValue);
                    break;

                case IonType.Int:
                    result = ((IIonInt)v1).IntValue.CompareTo(((IIonInt)v2).IntValue);
                    break;

                case IonType.Float:
                    result = ((IIonFloat)v1).FloatValue.CompareTo(((IIonFloat)v2).FloatValue);
                    break;

                case IonType.Decimal:
                    result = ((IIonDecimal)v1).DecimalValue.CompareTo(((IIonDecimal)v2).DecimalValue);
                    break;

//                    case IonType.Timestamp:
//                        result = ((IIonTimestamp) v1).TimeStampValue.CompareTo(((IIonTimestamp) v2).TimeStampValue);
//                        break;
                case IonType.String:
                    result = string.Compare(((IIonString)v1).StringValue, ((IIonString)v2).StringValue, StringComparison.Ordinal);
                    break;

                case IonType.Symbol:
                    result = CompareSymbolTokens(((IIonSymbol)v1).SymbolValue, ((IIonSymbol)v2).SymbolValue);
                    break;

                case IonType.Blob:
                case IonType.Clob:
                    result = CompareLob(((IIonLob)v1), ((IIonLob)v2));
                    break;

                case IonType.Struct:
                    result = CompareStructs(((IIonStruct)v1), ((IIonStruct)v2), strict);
                    break;

                case IonType.List:
                case IonType.Sexp:
                case IonType.Datagram:
                    result = CompareSequence(((IIonSequence)v1), ((IIonSequence)v2), strict);
                    break;
                }
            }

            if (result == 0 && strict)
            {
                result = CompareAnnotations(v1.GetTypeAnnotationSymbols(), v2.GetTypeAnnotationSymbols());
            }

            return(result);
        }