Skip to content

Commit c68860f

Browse files
yulian-gaponenkoiText-CI
authored andcommitted
Add JavaUtil.Fill method overloads for different primitive types
DEVSIX-5199
1 parent 8c7ade2 commit c68860f

File tree

2 files changed

+146
-17
lines changed

2 files changed

+146
-17
lines changed

itext.tests/itext.io.tests/itext/io/JavaUtilTest.cs

Lines changed: 103 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,17 @@ source product.
4040
For more information, please contact iText Software Corp. at this
4141
4242
*/
43+
4344
using System;
4445
using System.Collections.Generic;
4546
using System.Linq;
4647
using iText.IO.Util;
4748
using NUnit.Framework;
4849

49-
namespace iText.IO
50-
{
51-
public class JavaUtilTest
52-
{
50+
namespace iText.IO {
51+
public class JavaUtilTest {
5352
[Test]
54-
public virtual void LinkedHashSetTest()
55-
{
53+
public virtual void LinkedHashSetTest() {
5654
LinkedHashSet<int> set1 = new LinkedHashSet<int>();
5755
set1.Add(5);
5856
set1.Add(2);
@@ -82,8 +80,7 @@ public virtual void LinkedHashSetTest()
8280
}
8381

8482
[Test]
85-
public virtual void JavaCollectionsUtilTest()
86-
{
83+
public virtual void JavaCollectionsUtilTest() {
8784
IList<int> emptyList = JavaCollectionsUtil.EmptyList<int>();
8885
Assert.IsEmpty(emptyList);
8986
Assert.Throws<NotSupportedException>(() => emptyList.Add(10));
@@ -95,7 +92,8 @@ public virtual void JavaCollectionsUtilTest()
9592
IEnumerator<int> emptyIterator = JavaCollectionsUtil.EmptyIterator<int>();
9693
Assert.False(emptyIterator.MoveNext());
9794

98-
IList<int> unmodifiableList = JavaCollectionsUtil.UnmodifiableList<int>(new int[] { 10, 20, 30, 20 }.ToList());
95+
IList<int> unmodifiableList =
96+
JavaCollectionsUtil.UnmodifiableList<int>(new int[] {10, 20, 30, 20}.ToList());
9997
Assert.Throws<NotSupportedException>(() => unmodifiableList.Insert(0, 20));
10098
Assert.Throws<NotSupportedException>(() => { unmodifiableList[2] = 50; });
10199
int test = unmodifiableList[3];
@@ -107,22 +105,114 @@ public virtual void JavaCollectionsUtilTest()
107105
{70, 80},
108106
});
109107
test = unodifiableMap[2];
110-
Assert.Throws<KeyNotFoundException>(() => { int temp = unodifiableMap[3]; });
108+
Assert.Throws<KeyNotFoundException>(() => {
109+
int temp = unodifiableMap[3];
110+
});
111111
Assert.Throws<NotSupportedException>(() => { unodifiableMap[11] = 11; });
112112

113113
IList<int> singletonList = JavaCollectionsUtil.SingletonList(4);
114114
Assert.AreEqual(4, singletonList[0]);
115115
Assert.Throws<NotSupportedException>(() => singletonList.Add(9));
116116

117-
List<int> x = new int[] { 60, 50, 20 }.ToList();
117+
List<int> x = new int[] {60, 50, 20}.ToList();
118118
JavaCollectionsUtil.Sort(x);
119119
Assert.AreEqual(20, x[0]);
120120
Assert.AreEqual(60, x[2]);
121121

122-
x = new int[] { -1, 0, 1 }.ToList();
122+
x = new int[] {-1, 0, 1}.ToList();
123123
JavaCollectionsUtil.Reverse(x);
124124
Assert.AreEqual(1, x[0]);
125125
Assert.AreEqual(0, x[1]);
126126
}
127+
128+
[Test]
129+
public virtual void JavaUtilFillBytesTest() {
130+
byte[] bytes = new byte[5];
131+
byte fillVal = 1;
132+
JavaUtil.Fill(bytes, fillVal);
133+
foreach (byte b in bytes) {
134+
Assert.AreEqual(fillVal, b);
135+
}
136+
}
137+
138+
[Test]
139+
public virtual void JavaUtilFillCharsTest() {
140+
char[] chars = new char[5];
141+
char fillVal = 'a';
142+
JavaUtil.Fill(chars, fillVal);
143+
foreach (char c in chars) {
144+
Assert.AreEqual(fillVal, c);
145+
}
146+
}
147+
148+
[Test]
149+
public virtual void JavaUtilFillBoolTest() {
150+
bool[] bools = new bool[5];
151+
bool fillVal = true;
152+
JavaUtil.Fill(bools, fillVal);
153+
foreach (bool b in bools) {
154+
Assert.AreEqual(fillVal, b);
155+
}
156+
}
157+
158+
[Test]
159+
public virtual void JavaUtilFillShortTest() {
160+
short[] shorts = new short[5];
161+
short fillVal = 1;
162+
JavaUtil.Fill(shorts, fillVal);
163+
foreach (short b in shorts) {
164+
Assert.AreEqual(fillVal, b);
165+
}
166+
}
167+
168+
[Test]
169+
public virtual void JavaUtilFillIntTest() {
170+
int[] ints = new int[5];
171+
int fillVal = 1;
172+
JavaUtil.Fill(ints, fillVal);
173+
foreach (int b in ints) {
174+
Assert.AreEqual(fillVal, b);
175+
}
176+
}
177+
178+
[Test]
179+
public virtual void JavaUtilFillLongTest() {
180+
long[] longs = new long[5];
181+
long fillVal = 1;
182+
JavaUtil.Fill(longs, fillVal);
183+
foreach (long b in longs) {
184+
Assert.AreEqual(fillVal, b);
185+
}
186+
}
187+
188+
[Test]
189+
public virtual void JavaUtilFillFloatTest() {
190+
float[] floats = new float[5];
191+
float fillVal = 1;
192+
JavaUtil.Fill(floats, fillVal);
193+
foreach (float b in floats) {
194+
Assert.AreEqual(fillVal, b);
195+
}
196+
}
197+
198+
[Test]
199+
public virtual void JavaUtilFillDoubleTest() {
200+
double[] doubles = new double[5];
201+
double fillVal = 1;
202+
JavaUtil.Fill(doubles, fillVal);
203+
foreach (double d in doubles) {
204+
Assert.AreEqual(fillVal, d);
205+
}
206+
}
207+
208+
[Test]
209+
public virtual void JavaUtilFillObjectTest() {
210+
string[] strings = new string[5];
211+
string fillVal = "hello";
212+
JavaUtil.Fill(strings, fillVal);
213+
foreach (string s in strings) {
214+
Assert.AreEqual(fillVal, s);
215+
}
216+
}
127217
}
128-
}
218+
}

itext/itext.io/itext/io/util/JavaUtil.cs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,19 +230,58 @@ public static double Random() {
230230
return new Random().NextDouble();
231231
}
232232

233+
public static void Fill(byte[] a, byte val) {
234+
for (int i = 0, len = a.Length; i < len; i++) {
235+
a[i] = val;
236+
}
237+
}
238+
239+
public static void Fill(char[] a, char val) {
240+
for (int i = 0, len = a.Length; i < len; i++) {
241+
a[i] = val;
242+
}
243+
}
244+
245+
public static void Fill(bool[] a, bool val) {
246+
for (int i = 0, len = a.Length; i < len; i++) {
247+
a[i] = val;
248+
}
249+
}
250+
233251
public static void Fill(short[] a, short val) {
234-
for (int i = 0, len = a.Length; i < len; i++)
252+
for (int i = 0, len = a.Length; i < len; i++) {
235253
a[i] = val;
254+
}
255+
}
256+
257+
public static void Fill(int[] a, int val) {
258+
for (int i = 0, len = a.Length; i < len; i++) {
259+
a[i] = val;
260+
}
261+
}
262+
263+
public static void Fill(long[] a, long val) {
264+
for (int i = 0, len = a.Length; i < len; i++) {
265+
a[i] = val;
266+
}
236267
}
237268

238269
public static void Fill(float[] a, float val) {
239-
for (int i = 0, len = a.Length; i < len; i++)
270+
for (int i = 0, len = a.Length; i < len; i++) {
240271
a[i] = val;
272+
}
241273
}
242274

243-
public static void Fill(byte[] a, byte val) {
244-
for (int i = 0, len = a.Length; i < len; i++)
275+
public static void Fill(double[] a, double val) {
276+
for (int i = 0, len = a.Length; i < len; i++) {
245277
a[i] = val;
278+
}
279+
}
280+
281+
public static void Fill(object[] a, object val) {
282+
for (int i = 0, len = a.Length; i < len; i++) {
283+
a[i] = val;
284+
}
246285
}
247286

248287
public static void Sort<T>(T[] array) {

0 commit comments

Comments
 (0)