• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 package com.google.protobuf.kotlin
9 
10 import com.google.common.truth.Truth.assertThat
11 import com.google.protobuf.ByteString
12 import java.lang.IndexOutOfBoundsException
13 import java.nio.Buffer
14 import java.nio.ByteBuffer
15 import kotlin.test.assertFailsWith
16 import org.junit.Test
17 import org.junit.runner.RunWith
18 import org.junit.runners.JUnit4
19 
20 /** Tests for the extension functions in ByteStrings.kt. */
21 @RunWith(JUnit4::class)
22 class ByteStringsTest {
23   @Test
toByteStringUtf8null24   fun toByteStringUtf8() {
25     assertThat("abc".toByteStringUtf8())
26       .isEqualTo(ByteString.copyFrom("abc".toByteArray(Charsets.UTF_8)))
27   }
28 
29   @Test
plusnull30   fun plus() {
31     assertThat("abc".toByteStringUtf8() + "def".toByteStringUtf8())
32       .isEqualTo(ByteString.copyFrom("abcdef".toByteArray(Charsets.UTF_8)))
33   }
34 
35   @Test
byteAtnull36   fun byteAt() {
37     val str = "abc".toByteStringUtf8()
38     assertThat(str[0]).isEqualTo('a'.code)
39     assertThat(str[2]).isEqualTo('c'.code)
40   }
41 
42   @Test
isNotEmpty_returnsTrue_whenNotEmptynull43   fun isNotEmpty_returnsTrue_whenNotEmpty() {
44     assertThat("abc".toByteStringUtf8().isNotEmpty()).isTrue()
45   }
46 
47   @Test
isNotEmpty_returnsFalse_whenEmptynull48   fun isNotEmpty_returnsFalse_whenEmpty() {
49     assertThat(ByteString.EMPTY.isNotEmpty()).isFalse()
50   }
51 
52   @Test
byteAtBelowZeronull53   fun byteAtBelowZero() {
54     val str = "abc".toByteStringUtf8()
55     assertFailsWith<IndexOutOfBoundsException> { str[-1] }
56     assertFailsWith<IndexOutOfBoundsException> { str[-2] }
57   }
58 
59   @Test
byteAtAboveLengthnull60   fun byteAtAboveLength() {
61     val str = "abc".toByteStringUtf8()
62     assertFailsWith<IndexOutOfBoundsException> { str[3] }
63     assertFailsWith<IndexOutOfBoundsException> { str[4] }
64   }
65 
66   @Test
byteArrayToByteStringnull67   fun byteArrayToByteString() {
68     assertThat("abc".toByteArray(Charsets.UTF_8).toByteString())
69       .isEqualTo(ByteString.copyFromUtf8("abc"))
70   }
71 
72   @Test
byteBufferToByteStringnull73   fun byteBufferToByteString() {
74     val buffer = ByteBuffer.wrap("abc".toByteArray(Charsets.UTF_8))
75     assertThat(buffer.toByteString()).isEqualTo(ByteString.copyFromUtf8("abc"))
76   }
77 
78   @Test
byteBufferToByteStringRespectsPositionAndLimitnull79   fun byteBufferToByteStringRespectsPositionAndLimit() {
80     val buffer = ByteBuffer.wrap("abc".toByteArray(Charsets.UTF_8))
81     (buffer as java.nio.Buffer).position(1)
82     (buffer as java.nio.Buffer).limit(2)
83     assertThat(buffer.toByteString()).isEqualTo(ByteString.copyFromUtf8("b"))
84   }
85 }
86