Skip to content

Commit

Permalink
Added methods and tests for startsWithIterable, endsWithIterable. Tes…
Browse files Browse the repository at this point in the history
…ts for mapInPlace
  • Loading branch information
c committed Jan 16, 2025
1 parent 1035be0 commit 58e3a3c
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 13 deletions.
25 changes: 24 additions & 1 deletion docs/FeatureGrid.md
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ def startsWith[B >: T](that: IterableOnce[B], offset: Int = 0): Boolean
</td>
<td>✓</td>
<td>✓</td>
<td>❌</td>
<td>❌*</td>
</tr>
<tr>
<td>
Expand All @@ -1240,7 +1240,30 @@ def endsWith[B >: T](that: Iterable[B]): Boolean
</td>
<td>✓</td>
<td>✓</td>
<td>❌*</td>
</tr>
<tr>
<tr>
<td>

```scala
def startsWithIterable[B >: T](that: IterableOnce[B], offset: Int = 0): Boolean
```
</td>
<td>❌</td>
<td>❌</td>
<td>✓</td>
</tr>
<tr>
<td>

```scala
def endsWithIterable[B >: T](that: Iterable[B]): Boolean
```
</td>
<td>❌</td>
<td>❌</td>
<td>✓</td>
</tr>
<tr>
</table>
21 changes: 11 additions & 10 deletions narr/js/src/main/scala/narr/native/Extensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1641,15 +1641,16 @@ object Extensions {
* `true` if the sequence `that` is contained in this array at index
* `offset`, otherwise `false`.
*/
// def startsWith[B >: T](that: IterableOnce[B], offset: Int = 0): Boolean = {
// val itr = that.iterator
// var out = true
// var i = offset; while (itr.hasNext && i < a.length && out) {
// out = itr.next == a(i)
// i = i + 1
// }
// out
// }
def startsWithIterable[B >: T](that: IterableOnce[B], offset: Int = 0): Boolean = {
val itr = that.iterator
var out = true
var i = offset;
while (itr.hasNext && i < a.length && out) {
out = itr.next == a(i)
i = i + 1
}
out
}

/** Tests whether this array ends with the given sequence.
*
Expand All @@ -1658,7 +1659,7 @@ object Extensions {
* @return
* `true` if this array has `that` as a suffix, `false` otherwise.
*/
//def endsWith[B >: T](that: scala.collection.Iterable[B]): Boolean = ???
def endsWithIterable[B >: T](that: scala.collection.Iterable[B]): Boolean = a.toSeq.endsWith(that)

}
}
2 changes: 2 additions & 0 deletions narr/jvm-native/src/main/scala/narr/native/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ package object native {
destination
}

def startsWithIterable[B >: T](that: IterableOnce[B], offset: Int = 0): Boolean = a.startsWith(that, offset)

def endsWithIterable[B >: T](that: scala.collection.Iterable[B]): Boolean = a.endsWith(that)
}
}
}
77 changes: 77 additions & 0 deletions tests/shared/src/test/scala/MapInPlaceTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2023 dragonfly.ai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Util.*
import Util.NArrayType.*
import narr.*

import scala.reflect.ClassTag
import scala.util.Random

class MapInPlaceTest extends munit.FunSuite {

val N:Int = 30

private case class TestMapInPlace[T](a: NArray[T], f: T => T)(using ClassTag[T]) {
def test(): Unit = {
assertArray2NArrayEquality(a.toArray[T].mapInPlace(f), a.mapInPlace(f))
}
}

test("TestMapInPlace[Byte]") {
TestMapInPlace[Byte](
NArray.tabulate[Byte](N)((i: Int) => i.toByte),
(b: Byte) => (b + 1).toByte
).test()
}

test("TestMapInPlace[Short]") {
TestMapInPlace[Short](
NArray.tabulate[Short](N)((i: Int) => i.toShort),
(s: Short) => (s + 1).toShort
).test()
}

test("TestMapInPlace[Int]") {
TestMapInPlace[Int](
NArray.tabulate[Int](N)((i: Int) => i),
(i: Int) => i + 1
).test()
}

test("TestMapInPlace[Float]") {
TestMapInPlace[Float](
NArray.tabulate[Float](N)((i: Int) => i.toFloat),
(f: Float) => f + 1f
).test()
}

test("TestMapInPlace[Double]") {
TestMapInPlace[Double](
NArray.tabulate[Double](N)((i: Int) => i.toDouble),
(d: Double) => d + 1.0
).test()
}

test("TestMapInPlace[String]") {
val rs = new Random()
TestMapInPlace[String](
NArray.tabulate[String](N)((i: Int) => rs.nextString(i)),
(s: String) => s + s
).test()
}

}
6 changes: 4 additions & 2 deletions tests/shared/src/test/scala/NArrayOpsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,14 @@ class NArrayOpsTest extends munit.FunSuite {
assertEquals(a.startsWith(left), true)
assertEquals(a.startsWith(right, fulcrum), true)

// assertEquals(a.startsWith(left.toSeq), true)
// assertEquals(a.startsWith(right.toSeq, fulcrum), true)
assertEquals(a.startsWithIterable(left.toSeq), true)
assertEquals(a.startsWithIterable(right.toSeq, fulcrum), true)

// endsWith
assertEquals(a.endsWith(right), true)

assertEquals(a.endsWithIterable(right.toSeq), true)

// diff
assertArray2NArrayEquality(arr.diff(lArr), a.diff(left.toSeq))
assertArray2NArrayEquality(arr.diff(rArr), a.diff(right.toSeq))
Expand Down

0 comments on commit 58e3a3c

Please sign in to comment.