1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
// Copyright (C) 2004 Max Gilead <gilead@yellowhedgehog.com>
// This file is part of Mauve.
// Mauve is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
// Mauve is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warrant of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Mauve; see the file COPYING. If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
package gnu.testlet.java.nio.Buffer;
import gnu.testlet.TestHarness;
import java.nio.Buffer;
import java.nio.InvalidMarkException;
public class PlainBufferTest
{
public void test(TestHarness h, BufferFactory factory)
{
try
{
intialState(h, factory);
position(h, factory);
mark(h, factory);
limit(h, factory);
rewind(h, factory);
clear(h, factory);
flip(h, factory);
}
catch(Exception e)
{
h.fail("Unexpected excetpion: "+ e);
h.debug(e);
}
}
private void intialState(TestHarness h, BufferFactory factory)
{
Buffer buf = null;
buf = factory.newInstance();
checkStatus(h, buf, "intialState", 10, 10, true, 10, 0);
}
private void position(TestHarness h, BufferFactory factory)
{
Buffer buf = null;
buf = factory.newInstance();
h.check(buf.position(1), buf, "position: buf.position(1)");
checkStatus(h, buf, "position", 10, 10, true, 9, 1);
buf.position(10);
checkStatus(h, buf, "position", 10, 10, false, 0, 10);
// position can't be negative
buf = factory.newInstance();
try
{
buf.position(-1);
h.check(false, "position: is negative");
}
catch(IllegalArgumentException iae)
{
h.check(true, "position: can't be negative");
}
// position can't be larger than limit
buf = factory.newInstance();
buf.limit(5);
try
{
buf.position(6);
h.check(false, "position: is larger than capacity");
}
catch(IllegalArgumentException iae)
{
h.check(true, "position: can't be larger than capacity");
}
}
private void mark(TestHarness h, BufferFactory factory)
{
Buffer buf = null;
// mark at default position
buf = factory.newInstance();
h.check(buf.mark(), buf, "mark: buf.mark()");
checkStatus(h, buf, "mark", 10, 10, true, 10, 0);
buf.position(5);
checkStatus(h, buf, "mark", 10, 10, true, 5, 5);
h.check(buf.reset(), buf, "mark: buf.reset()");
checkStatus(h, buf, "mark", 10, 10, true, 10, 0);
buf.position(6);
checkStatus(h, buf, "mark", 10, 10, true, 4, 6);
buf.reset();
checkStatus(h, buf, "mark", 10, 10, true, 10, 0);
// mark at specified position
buf = factory.newInstance();
buf.position(5);
buf.mark();
checkStatus(h, buf, "mark", 10, 10, true, 5, 5);
buf.position(6);
checkStatus(h, buf, "mark", 10, 10, true, 4, 6);
buf.reset();
checkStatus(h, buf, "mark", 10, 10, true, 5, 5);
buf.position(7);
checkStatus(h, buf, "mark", 10, 10, true, 3, 7);
buf.reset();
checkStatus(h, buf, "mark", 10, 10, true, 5, 5);
// mark should be discarded if new position is smaller than mark
buf = factory.newInstance();
buf.position(5);
buf.mark();
buf.position(4);
try
{
buf.reset();
h.check(false, "mark: mark not invalidated");
}
catch(InvalidMarkException ime)
{
h.check(true, "mark: invalidated mark");
}
checkStatus(h, buf, "mark", 10, 10, true, 6, 4);
}
private void limit(TestHarness h, BufferFactory factory)
{
Buffer buf = null;
buf = factory.newInstance();
buf.position(2);
buf.mark();
buf.position(3);
h.check(buf.limit(4), buf, "limit: buf.limit(4)");
checkStatus(h, buf, "limit", 10, 4, true, 1, 3);
buf.reset();
checkStatus(h, buf, "limit", 10, 4, true, 2, 2);
// mark should be discarded if new limit is smaller than mark
// and position should be set to new limit
buf = factory.newInstance();
buf.position(5);
buf.mark();
buf.position(6);
buf.limit(4);
checkStatus(h, buf, "limit", 10, 4, false, 0, 4);
try
{
buf.reset();
h.check(false, "limit: mark not invalidated");
}
catch(InvalidMarkException ime)
{
h.check(true, "limit: invalidated mark");
}
// limit can't be negative
buf = factory.newInstance();
try
{
buf.limit(-1);
h.check(false, "limit: is negative");
}
catch(IllegalArgumentException iae)
{
h.check(true, "limit: can't be negative");
}
// limit can't be larger than capacity
buf = factory.newInstance();
try
{
buf.limit(11);
h.check(false, "limit: is larger than capacity");
}
catch(IllegalArgumentException iae)
{
h.check(true, "limit: can't be larger than capacity");
}
}
private void rewind(TestHarness h, BufferFactory factory)
{
Buffer buf = null;
buf = factory.newInstance();
buf.position(5);
buf.mark();
buf.position(6);
buf.limit(9);
h.check(buf.rewind(), buf, "rewind: buf.rewind()");
checkStatus(h, buf, "rewind", 10, 9, true, 9, 0);
try
{
buf.reset();
h.check(false, "rewind: mark not invalidated");
}
catch(InvalidMarkException ime)
{
h.check(true, "rewind: invalidated mark");
}
}
private void clear(TestHarness h, BufferFactory factory)
{
Buffer buf = null;
buf = factory.newInstance();
buf.position(5);
buf.mark();
buf.position(6);
buf.limit(7);
h.check(buf.clear(), buf, "clear: buf.clear()");
checkStatus(h, buf, "clear", 10, 10, true, 10, 0);
try
{
buf.reset();
h.check(false, "clear: mark not invalidated");
}
catch(InvalidMarkException ime)
{
h.check(true, "clear: invalidated mark");
}
}
private void flip(TestHarness h, BufferFactory factory)
{
Buffer buf = null;
buf = factory.newInstance();
buf.position(5);
buf.mark();
buf.position(6);
h.check(buf.flip(), buf, "flip: buf.flip()");
checkStatus(h, buf, "flip", 10, 6, true, 6, 0);
try
{
buf.reset();
h.check(false, "flip: mark not invalidated");
}
catch(InvalidMarkException ime)
{
h.check(true, "flip: invalidated mark");
}
}
private void checkStatus(TestHarness h, Buffer buf, String prefix,
int cap, int lim, boolean hasRem, int rem, int pos)
{
h.check(buf.capacity(), cap, prefix +": buf.capacity()");
h.check(buf.limit(), lim, prefix +": buf.limit()");
h.check(buf.hasRemaining(), hasRem, prefix +": buf.hasRemaining()");
h.check(buf.remaining(), rem, prefix +": buf.remaining()");
h.check(buf.position(), pos, prefix +": buf.position()");
}
}
|