1 /** 2 * Copyright: Copyright (c) 2011 Jacob Carlborg. All rights reserved. 3 * Authors: Jacob Carlborg 4 * Version: Initial created: Aug 6, 2011 5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) 6 */ 7 module tests.Slice; 8 9 import orange.serialization.Serializer; 10 import orange.serialization.archives.XmlArchive; 11 import orange.test.UnitTester; 12 import tests.Util; 13 14 Serializer serializer; 15 XmlArchive!(char) archive; 16 17 class J 18 { 19 int[] firstSource; 20 int[] firstSlice; 21 22 int[] secondSlice; 23 int[] secondSource; 24 25 int[4] firstStaticSource; 26 int[] firstStaticSlice; 27 28 int[] firstEmpty; 29 int[] secondEmpty; 30 31 int[][] thirdEmpty; 32 } 33 34 J j; 35 J jDeserialized; 36 37 unittest 38 { 39 archive = new XmlArchive!(char); 40 serializer = new Serializer(archive); 41 42 j = new J; 43 j.firstSource = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].dup; 44 j.firstSlice = j.firstSource[3 .. 7]; 45 46 j.secondSource = [10, 11, 12, 13, 14, 15].dup; 47 j.secondSlice = j.secondSource[1 .. 4]; 48 49 j.firstStaticSource = [16, 17, 18, 19]; 50 j.firstStaticSlice = j.firstStaticSource[1 .. 3]; 51 52 describe("serialize slices") in { 53 it("should return serialized slices") in { 54 auto expected2066 = q"xml 55 <?xml version="1.0" encoding="UTF-8"?> 56 <archive version="1.0.0" type="org.dsource.orange.xml"> 57 <data> 58 <object runtimeType="tests.Slice.J" type="tests.Slice.J" key="0" id="0"> 59 <array type="int" length="0" key="firstEmpty" id="36"/> 60 <array type="int" length="0" key="secondEmpty" id="37"/> 61 <array type="int[]" length="0" key="thirdEmpty" id="38"/> 62 <slice length="3" key="secondSlice" offset="1">21</slice> 63 <array type="int" length="4" key="firstStaticSource" id="28"> 64 <int key="0" id="29">16</int> 65 <int key="1" id="30">17</int> 66 <int key="2" id="31">18</int> 67 <int key="3" id="32">19</int> 68 </array> 69 <array type="int" length="10" key="firstSource" id="1"> 70 <int key="0" id="2">0</int> 71 <int key="1" id="3">1</int> 72 <int key="2" id="4">2</int> 73 <int key="3" id="5">3</int> 74 <int key="4" id="6">4</int> 75 <int key="5" id="7">5</int> 76 <int key="6" id="8">6</int> 77 <int key="7" id="9">7</int> 78 <int key="8" id="10">8</int> 79 <int key="9" id="11">9</int> 80 </array> 81 <array type="int" length="6" key="secondSource" id="21"> 82 <int key="0" id="22">10</int> 83 <int key="1" id="23">11</int> 84 <int key="2" id="24">12</int> 85 <int key="3" id="25">13</int> 86 <int key="4" id="26">14</int> 87 <int key="5" id="27">15</int> 88 </array> 89 <slice length="2" key="firstStaticSlice" offset="1">28</slice> 90 <slice length="4" key="firstSlice" offset="3">1</slice> 91 </object> 92 </data> 93 </archive> 94 xml"; 95 auto expected2067 = q"xml 96 <?xml version="1.0" encoding="UTF-8"?> 97 <archive version="1.0.0" type="org.dsource.orange.xml"> 98 <data> 99 <object runtimeType="tests.Slice.J" type="tests.Slice.J" id="0" key="0"> 100 <array id="36" type="int" length="0" key="firstEmpty"/> 101 <array id="37" type="int" length="0" key="secondEmpty"/> 102 <array id="38" type="int[]" length="0" key="thirdEmpty"/> 103 <array id="21" type="int" length="6" key="secondSource"> 104 <int id="22" key="0">10</int> 105 <int id="23" key="1">11</int> 106 <int id="24" key="2">12</int> 107 <int id="25" key="3">13</int> 108 <int id="26" key="4">14</int> 109 <int id="27" key="5">15</int> 110 </array> 111 <slice offset="3" length="4" key="firstSlice">1</slice> 112 <slice offset="1" length="3" key="secondSlice">21</slice> 113 <array id="28" type="int" length="4" key="firstStaticSource"> 114 <int id="29" key="0">16</int> 115 <int id="30" key="1">17</int> 116 <int id="31" key="2">18</int> 117 <int id="32" key="3">19</int> 118 </array> 119 <slice offset="1" length="2" key="firstStaticSlice">28</slice> 120 <array id="1" type="int" length="10" key="firstSource"> 121 <int id="2" key="0">0</int> 122 <int id="3" key="1">1</int> 123 <int id="4" key="2">2</int> 124 <int id="5" key="3">3</int> 125 <int id="6" key="4">4</int> 126 <int id="7" key="5">5</int> 127 <int id="8" key="6">6</int> 128 <int id="9" key="7">7</int> 129 <int id="10" key="8">8</int> 130 <int id="11" key="9">9</int> 131 </array> 132 </object> 133 </data> 134 </archive> 135 xml"; 136 137 static if (__VERSION__ >= 2067) auto expected = expected2067; 138 else auto expected = expected2066; 139 140 serializer.reset(); 141 serializer.serialize(j); 142 143 assert(expected.equalToXml(archive.data)); 144 }; 145 }; 146 147 describe("deserialize slices") in { 148 jDeserialized = serializer.deserialize!(J)(archive.untypedData); 149 150 it("should return deserialized strings equal to the original strings") in { 151 assert(j.firstSource == jDeserialized.firstSource); 152 assert(j.secondSource == jDeserialized.secondSource); 153 }; 154 155 it("should return deserialized slices equal to the original slices") in { 156 assert(j.firstSlice == jDeserialized.firstSlice); 157 assert(j.secondSlice == jDeserialized.secondSlice); 158 }; 159 160 it("the slices should be equal to a slice of the original sources") in { 161 assert(jDeserialized.firstSource[3 .. 7] == jDeserialized.firstSlice); 162 assert(jDeserialized.secondSource[1 .. 4] == jDeserialized.secondSlice); 163 assert(jDeserialized.firstStaticSource[1 .. 3] == jDeserialized.firstStaticSlice); 164 165 assert(j.firstSource[3 .. 7] == jDeserialized.firstSlice); 166 assert(j.secondSource[1 .. 4] == jDeserialized.secondSlice); 167 assert(j.firstStaticSource[1 .. 3] == jDeserialized.firstStaticSlice); 168 }; 169 170 it("the slices should be able to modify the sources") in { 171 jDeserialized.firstSlice[0] = 55; 172 jDeserialized.secondSlice[0] = 3; 173 jDeserialized.firstStaticSlice[0] = 1; 174 175 assert(jDeserialized.firstSource == [0, 1, 2, 55, 4, 5, 6, 7, 8, 9]); 176 assert(jDeserialized.secondSource == [10, 3, 12, 13, 14, 15]); 177 assert(jDeserialized.firstStaticSource == [16, 1, 18, 19]); 178 }; 179 }; 180 }