Exemplo n.º 1
0
        private static void CompareScalars(IonType t, bool isNull, IIonReader it1, IIonReader it2)
        {
            switch (t)
            {
            case IonType.Bool:
            {
                if (!isNull)
                {
                    Assert.AreEqual(it1.BoolValue(), it2.BoolValue());
                }

                break;
            }

            case IonType.Int:
            {
                if (!isNull)
                {
                    Assert.AreEqual(it1.BigIntegerValue(), it2.BigIntegerValue());
                }

                break;
            }

            case IonType.Float:
            {
                if (!isNull)
                {
                    double v1 = it1.DoubleValue();
                    double v2 = it2.DoubleValue();
                    if (double.IsNaN(v1) && double.IsNaN(v2))
                    {
                        Assert.AreEqual(v1, v2);
                    }
                    else if (double.IsNaN(v1) || double.IsNaN(v2))
                    {
                        Assert.AreNotEqual(v1, v2, 0);
                    }
                    else
                    {
                        Assert.AreEqual(v1, v2, 0);
                        // The last param is a delta, and we want exact match.
                    }
                }

                break;
            }

            case IonType.Decimal:
            {
                if (!isNull)
                {
                    BigDecimal bigDec1 = it1.DecimalValue();
                    BigDecimal bigDec2 = it2.DecimalValue();
                    AssertPreciselyEquals(bigDec1, bigDec2);

                    try
                    {
                        decimal dec1 = bigDec1.ToDecimal();
                        decimal dec2 = bigDec2.ToDecimal();
                        Assert.AreEqual(dec1, dec2);

                        Assert.AreEqual(decimal.ToDouble(dec1), decimal.ToDouble(dec2));
                        Assert.AreEqual(decimal.ToInt32(dec1), decimal.ToInt32(dec2));
                        Assert.AreEqual(decimal.ToInt64(dec1), decimal.ToInt64(dec2));
                    }
                    catch (OverflowException)
                    {
                        // Certain Ion Decimals in test file exceed C# decmial range. Just AssertPreciselyEquals.
                    }
                }

                break;
            }

            case IonType.Timestamp:
            {
                if (!isNull)
                {
                    Timestamp t1 = it1.TimestampValue();
                    Timestamp t2 = it2.TimestampValue();
                    Assert.AreEqual(t1, t2);
                }

                break;
            }

            case IonType.String:
            {
                string s1 = it1.StringValue();
                string s2 = it2.StringValue();
                Assert.AreEqual(s1, s2);
                break;
            }

            case IonType.Symbol:
            {
                SymbolToken tok1 = it1.SymbolValue();
                SymbolToken tok2 = it2.SymbolValue();
                if (isNull)
                {
                    Assert.IsNull(tok1.Text);
                    Assert.IsNull(tok2.Text);
                }
                else if (tok1.Text == null || tok2.Text == null)
                {
                    Assert.AreEqual(tok1.Sid, tok2.Sid, "sids");
                }
                else
                {
                    string s1 = tok1.Text;
                    string s2 = tok2.Text;
                    Assert.AreEqual(s1, s2);
                }

                break;
            }

            case IonType.Blob:
            case IonType.Clob:
            {
                if (!isNull)
                {
                    byte[] b1 = it1.NewByteArray();
                    byte[] b2 = it2.NewByteArray();
                    Assert.IsTrue(b1 != null && b2 != null);
                    Assert.IsTrue(b1.Length == b2.Length);
                    for (int ii = 0; ii < b1.Length; ii++)
                    {
                        byte v1 = b1[ii];
                        byte v2 = b2[ii];
                        Assert.AreEqual(v1, v2);
                    }
                }

                break;
            }

            default:
                throw new InvalidOperationException("iterated to a type that's not expected");
            }
        }
Exemplo n.º 2
0
        private void WriteValueRecursively(IonType type, IIonReader reader)
        {
            this.TryWriteFieldName(reader);
            this.TryWriteAnnotationSymbols(reader);

            if (reader.CurrentIsNull)
            {
                this.WriteNull(type);
                return;
            }

            switch (type)
            {
            case IonType.Bool:
                this.WriteBool(reader.BoolValue());
                break;

            case IonType.Int:
                switch (reader.GetIntegerSize())
                {
                case IntegerSize.Int:
                    this.WriteInt(reader.IntValue());
                    break;

                case IntegerSize.Long:
                    this.WriteInt(reader.LongValue());
                    break;

                case IntegerSize.BigInteger:
                    this.WriteInt(reader.BigIntegerValue());
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                break;

            case IonType.Float:
                this.WriteFloat(reader.DoubleValue());
                break;

            case IonType.Decimal:
                this.WriteDecimal(reader.DecimalValue());
                break;

            case IonType.Timestamp:
                this.WriteTimestamp(reader.TimestampValue());
                break;

            case IonType.Symbol:
                this.WriteSymbolToken(reader.SymbolValue());
                break;

            case IonType.String:
                this.WriteString(reader.StringValue());
                break;

            case IonType.Clob:
                this.WriteClob(reader.NewByteArray());
                break;

            case IonType.Blob:
                this.WriteBlob(reader.NewByteArray());
                break;

            case IonType.List:
            case IonType.Sexp:
            case IonType.Struct:
                this.WriteContainerRecursively(type, reader);
                break;
            }
        }
Exemplo n.º 3
0
        private void InternalWriteValue(IIonReader reader, int depth = 0)
        {
            IonType type = reader.CurrentType;

            if (type == IonType.None)
            {
                return;
            }

            if (depth > 0)
            {
                string fieldName = reader.CurrentFieldName;
                if (fieldName != null)
                {
                    this.SetFieldName(fieldName);
                }
            }

            foreach (var annotation in reader.GetTypeAnnotations())
            {
                this.AddTypeAnnotationSymbol(annotation);
            }

            if (reader.CurrentIsNull)
            {
                this.WriteNull(type);
            }
            else
            {
                switch (type)
                {
                case IonType.Bool:
                    this.WriteBool(reader.BoolValue());
                    break;

                case IonType.Int:
                    this.WriteInt(reader.BigIntegerValue());
                    break;

                case IonType.Float:
                    this.WriteFloat(reader.DoubleValue());
                    break;

                case IonType.Decimal:
                    this.WriteDecimal(reader.DecimalValue());
                    break;

                case IonType.Timestamp:
                    this.WriteTimestamp(reader.TimestampValue());
                    break;

                case IonType.Symbol:
                    this.WriteSymbolToken(reader.SymbolValue());
                    break;

                case IonType.String:
                    this.WriteString(reader.StringValue());
                    break;

                case IonType.Clob:
                    this.WriteClob(reader.NewByteArray());
                    break;

                case IonType.Blob:
                    this.WriteBlob(reader.NewByteArray());
                    break;

                case IonType.List:
                    this.StepIn(IonType.List);
                    break;

                case IonType.Sexp:
                    this.StepIn(IonType.Sexp);
                    break;

                case IonType.Struct:
                    this.StepIn(IonType.Struct);
                    break;

                default:
                    throw new InvalidOperationException("Unexpected type '" + type + "'");
                }

                if (type.IsContainer())
                {
                    reader.StepIn();
                    this.InternalWriteValues(reader, depth + 1);
                    this.StepOut();
                    reader.StepOut();
                }
            }
        }