Project

Profile

Help

How to connect?
Download (6.12 KB) Statistics
| Branch: | Revision:

he / src / test / nunit / SaxonNUnit / SaxonNUnit / TestXdmArray.cs @ 3df04590

1
using System;
2
using System.Collections.Generic;
3
using Saxon.Api;
4
using NUnit.Framework;
5

    
6
namespace Saxon9.NUnit
7
{
8
    class TestXdmArray
9
    {
10

    
11
        [Test]
12
        public void testArrayOperations()
13
        {
14
            XdmArray a = new XdmArray();
15
            Assert.True(0== a.ArrayLength());
16
            Assert.True(1== a.Count);
17

    
18
            a = new XdmArray(new XdmValue[] { new XdmAtomicValue(1), new XdmAtomicValue(2), new XdmAtomicValue(3) });
19
            Assert.True(3== a.ArrayLength());
20
            Assert.True(1== a.Count);
21
            try
22
            {
23
                Assert.True(((XdmAtomicValue)a.Get(1)).GetLongValue()== 2);
24
            }
25
            catch (Exception e)
26
            {
27
                Assert.Fail(e.Message);
28
            }
29
            a = a.Put(1, new XdmAtomicValue(17));
30
            try
31
            {
32
                Assert.True(((XdmAtomicValue)a.Get(1)).GetLongValue()== 17);
33
            }
34
            catch (Exception e)
35
            {
36
                Assert.Fail(e.Message);
37
            }
38

    
39
            List<XdmValue> values = a.AsList();
40
            try
41
            {
42
                Assert.True(((XdmAtomicValue)values[1]).GetLongValue()== 17);
43
            }
44
            catch (Exception e)
45
            {
46
                Assert.Fail(e.Message);
47
            }
48

    
49
        }
50

    
51
    [Test]
52
       public void TestArrayAsFunction()
53
        {
54
            XdmArray a = new XdmArray(new XdmValue[] { new XdmAtomicValue(1), new XdmAtomicValue(2), new XdmAtomicValue(3) });
55
            Processor proc = new Processor(true);
56
            try
57
            {
58
                XdmValue result = a.Invoke(new XdmValue[]{ new XdmAtomicValue(1)}, proc);  // 1-based indexing here
59
                Assert.True(result is XdmAtomicValue);
60
                Assert.True(((XdmAtomicValue)result).GetLongValue() == 1);
61
            }
62
            catch (Exception e)
63
            {
64
              
65
                Assert.Fail(e.Message);
66
            }
67
        }
68
       
69
       [Test]
70
       public void TestArrayAsQueryParameter()
71
       {
72
           try
73
           {
74
               XdmArray a = new XdmArray(new XdmValue[] { new XdmAtomicValue(1), new XdmAtomicValue(2), new XdmAtomicValue(3) });
75
               Processor proc = new Processor(true);
76
               String query =
77
                       "declare namespace a='http://www.w3.org/2005/xpath-functions/array';" +
78
                       "declare variable $a as array(*) external; " +
79
                       "a:size($a)";
80
               XQueryCompiler comp = proc.NewXQueryCompiler();
81
               XQueryEvaluator q = comp.Compile(query).Load();
82
               q.SetExternalVariable(new QName("a"), a);
83
               XdmAtomicValue result = (XdmAtomicValue)q.EvaluateSingle();
84
               Assert.True(3 == result.GetLongValue());
85
           }
86
           catch (Exception e)
87
           {
88
               Assert.Fail(e.Message);
89
           }
90
       }
91

    
92
        [Test]
93
       public void TestArrayAsQueryResult()
94
       {
95
           try
96
           {
97
               Processor proc = new Processor(true);
98
               String query = "[1, 2, 3]";
99
               XQueryCompiler comp = proc.NewXQueryCompiler();
100
               XQueryEvaluator q = comp.Compile(query).Load();
101
               XdmArray result = (XdmArray)q.EvaluateSingle();
102
               Assert.True(3 == result.ArrayLength());
103
           }
104
           catch (Exception e)
105
           {
106
              
107
               Assert.Fail(e.Message);
108
           }
109
       }
110

    
111
        [Test]
112
       public void TestArrayConvertIntegers()
113
       {
114
           int[] inn = new int[] { 5, 6, 7, 8 };
115
           XdmArray result = XdmArray.MakeArray(inn);
116
           Assert.True(4 == result.ArrayLength());
117
           Assert.True(new XdmAtomicValue(5).Equals(result.Get(0)));
118
           //Assert.True(XdmItemType.INT.matches((XdmAtomicValue)result.get(3)));
119
       }
120

    
121
        [Test]
122
       public void TestArrayConvertLongs()
123
       {
124
           long[] inn = new long[] { 5, 6, 7, 8 };
125
           XdmArray result = XdmArray.MakeArray(inn);
126
           Assert.True(4== result.ArrayLength());
127
           Assert.True(new XdmAtomicValue(5).Equals(result.Get(0)));
128
            Console.WriteLine(((XdmAtomicValue)result.Get(3)).GetTypeName(new Processor(true)).ToString());
129
          // Assert.True(ItemType.LONG.matches((XdmAtomicValue)result.Get(3)));
130
       }
131

    
132
        [Test]
133
        public void TestArrayConvertStrings()
134
       {
135
           String[] inn = new String[] { "a", "b", "c", "d" };
136
           XdmArray result = XdmArray.MakeArray(inn);
137
           Assert.True(4 == result.ArrayLength());
138
           Assert.True(new XdmAtomicValue("a").Equals(result.Get(0)));
139
           //Assert.True(ItemType.ANY_ARRAY.matches(result));
140
           //Assert.True(ItemType.STRING.matches((XdmAtomicValue)result.Get(3)));
141
       }
142

    
143
        [Test]
144
        public void TestArrayConvertBooleans()
145
       {
146
           bool[] inn = new bool[] { true, true, false };
147
           XdmArray result = XdmArray.MakeArray(inn);
148
           Assert.True(3 == result.ArrayLength());
149
           Assert.True(XdmValue.MakeValue(true).Equals(result.Get(0)));
150
         //Assert.True(ItemType.BOOLEAN.matches((XdmAtomicValue)result.Get(2)));
151
       }
152

    
153
        [Test]
154
        public void TestArrayConvertArrays()
155
       {
156
           String[] in0 = new String[] { "a", "b", "c", "d" };
157
           String[] in1 = new String[] { "w", "x", "y", "z" };
158
           XdmArray result = XdmArray.MakeArray(new String[][] { in0, in1 });
159
           Assert.True(2 == result.ArrayLength());
160
           Assert.True(new XdmAtomicValue("a").Equals(((XdmArray)result.Get(0)).Get(0)));
161
          // Assert.True(ItemType.ANY_ARRAY.matches((XdmItem)result.get(1)));
162
          // Assert.True(ItemType.STRING.matches((XdmItem)((XdmArray)result.get(1)).get(2)));
163
          // Processor proc = new Processor(true);
164
          // ItemTypeFactory factory = new ItemTypeFactory(proc);
165
          // ItemType arrayOfString = factory.getArrayType(SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE));
166
          // ItemType arrayOfArray = factory.getArrayType(SequenceType.makeSequenceType(arrayOfString, OccurrenceIndicator.ONE));
167
         //  Assert.True(arrayOfArray.matches(result));
168
       }
169
    }
170
}
(16-16/19)