xref: /aosp_15_r20/external/okio/okio/src/commonTest/kotlin/okio/OkioTesting.kt (revision f9742813c14b702d71392179818a9e591da8620c)
1 /*
2  * Copyright (C) 2019 Square, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package okio
17 
18 import kotlin.random.Random
19 
segmentSizesnull20 fun segmentSizes(buffer: Buffer): List<Int> {
21   var segment = buffer.head ?: return emptyList()
22 
23   val sizes = mutableListOf(segment.limit - segment.pos)
24   segment = segment.next!!
25   while (segment !== buffer.head) {
26     sizes.add(segment.limit - segment.pos)
27     segment = segment.next!!
28   }
29   return sizes
30 }
31 
bufferWithRandomSegmentLayoutnull32 fun bufferWithRandomSegmentLayout(dice: Random, data: ByteArray): Buffer {
33   val result = Buffer()
34 
35   // Writing to result directly will yield packed segments. Instead, write to
36   // other buffers, then write those buffers to result.
37   var pos = 0
38   var byteCount: Int
39   while (pos < data.size) {
40     byteCount = Segment.SIZE / 2 + dice.nextInt(Segment.SIZE / 2)
41     if (byteCount > data.size - pos) byteCount = data.size - pos
42     val offset = dice.nextInt(Segment.SIZE - byteCount)
43 
44     val segment = Buffer()
45     segment.write(ByteArray(offset))
46     segment.write(data, pos, byteCount)
47     segment.skip(offset.toLong())
48 
49     result.write(segment, byteCount.toLong())
50     pos += byteCount
51   }
52 
53   return result
54 }
55 
bufferWithSegmentsnull56 fun bufferWithSegments(vararg segments: String): Buffer {
57   val result = Buffer()
58   for (s in segments) {
59     val offsetInSegment = if (s.length < Segment.SIZE) (Segment.SIZE - s.length) / 2 else 0
60     val buffer = Buffer()
61     buffer.writeUtf8('_'.repeat(offsetInSegment))
62     buffer.writeUtf8(s)
63     buffer.skip(offsetInSegment.toLong())
64     result.write(buffer.copyTo(Buffer()), buffer.size)
65   }
66   return result
67 }
68 
makeSegmentsnull69 fun makeSegments(source: ByteString): ByteString {
70   val buffer = Buffer()
71   for (i in 0 until source.size) {
72     val segment = buffer.writableSegment(Segment.SIZE)
73     segment.data[segment.pos] = source[i]
74     segment.limit++
75     buffer.size++
76   }
77   return buffer.snapshot()
78 }
79 
80 /**
81  * Returns a string with all '\' slashes replaced with '/' slashes. This is useful for test
82  * assertions that intend to ignore slashes.
83  */
Pathnull84 fun Path.withUnixSlashes(): String {
85   return toString().replace('\\', '/')
86 }
87 
assertRelativeTonull88 expect fun assertRelativeTo(
89   a: Path,
90   b: Path,
91   bRelativeToA: Path,
92   sameAsNio: Boolean = true,
93 )
94 
95 expect fun assertRelativeToFails(
96   a: Path,
97   b: Path,
98   sameAsNio: Boolean = true,
99 ): IllegalArgumentException
100