Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.wear.watchface.dfx.memory;
package com.google.wear.watchface.dfx.memory

/**
* Exception thrown when the memory footprint test encounters an unexpected issue. The test fails,
* but the watch face should not be rejected nor accepted, but the error should be escalated for
* further analysis.
*/
class InvalidTestRunException extends RuntimeException {

InvalidTestRunException(String message) {
super(message);
}
}
internal class InvalidTestRunException(message: String?) : RuntimeException(message)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2023 Google LLC
*
* 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.
*/
package com.google.wear.watchface.dfx.memory

/**
* A lazy iterator, allowing to generate all combinations of a set without getting into stack
* overflows.
*/
internal interface SizedIterator<T> : Iterator<T> {
/** The size of the collection of elements that is generated */
fun getSize(): Long

companion object {
/** Constructs a sized iterator from a plain java iterator and a size. */
fun <T> fromIterator(iterator: Iterator<T>, size: Long): SizedIterator<T> {
return object : SizedIterator<T> {
override fun getSize(): Long {
return size
}

override fun hasNext(): Boolean {
return iterator.hasNext()
}

override fun next(): T {
return iterator.next()
}
}
}

/**
* Lazily combines an existing iterator with a new collection of elements by taking every
* element of the new collection and combining it with every element of the existing
* iterator. The resulting iterator will have a size of iterator.size() * elements.size().
*
* @param iterator the existing iterator
* @param elements the collection providing the new elements that are combined with the
* already generated ones.
* @param combineFn the function used to combine each element of the elements argument with
* each element of the existing iterator.
* @param mapFn the function used to map a single element of the elements collection to
* elements of the iterator, used when the existing iterator is empty and combineFn cannot
* be called.
* @return a new iterator with the combined elements.
* @param <T> the elements of the iterator
* @param <U> the elements of the collection
*/
fun <T, U> combine(
iterator: SizedIterator<T>,
elements: Collection<U>,
combineFn: (T, U) -> T,
mapFn: (U) -> T
): SizedIterator<T> {
// if the current key does not have any configuration values, then ignore it
// and return the next iterator.
if (elements.isEmpty()) {
return iterator
}

// if the rest iterator has no configuration values, then start a new iterator
// from the current keys.
if (!iterator.hasNext()) {
return fromIterator(elements.stream().map(mapFn).iterator(), elements.size.toLong())
}

// lazily consume each partial config from the rest iterator and append each value for
// the
// current key, producing new partial configs.
return object : SizedIterator<T> {
private val _size = elements.size * iterator.getSize()
private var crtValuesIter = elements.iterator()
private var currentFromTail = iterator.next()
override fun getSize(): Long {
return _size
}

override fun hasNext(): Boolean {
return crtValuesIter.hasNext() || iterator.hasNext()
}

override fun next(): T {
if (!crtValuesIter.hasNext()) {
crtValuesIter = elements.iterator()
currentFromTail = iterator.next()
}
return combineFn(currentFromTail, crtValuesIter.next())
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.wear.watchface.dfx.memory;
package com.google.wear.watchface.dfx.memory

/**
* Exception thrown when the memory footprint test fails for an expected reason and the watch face
* must be rejected from the store.
*/
public class TestFailedException extends RuntimeException {

public TestFailedException(String message) {
super(message);
}
}
class TestFailedException(message: String?) : RuntimeException(message)
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
/*
* Copyright 2023 Google LLC
*
*
* 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.
*/

package com.google.wear.watchface.dfx.memory;
package com.google.wear.watchface.dfx.memory

/** Annotates test results to make it easier for humans to read them. */
class TestResultFormatter {

static String formatSuccess(String successMessage) {
return String.format("[MEMORY_FOOTPRINT]: ✅PASS✅ %s ✅ ", successMessage);
internal object TestResultFormatter {
@JvmStatic
fun formatSuccess(successMessage: String): String {
return "[MEMORY_FOOTPRINT]: ✅PASS✅ $successMessage"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice touch adding the emoji for clarity! One small concern is that emojis might not render consistently across all environments (encoding, terminals, IDEs). To keep things universally compatible, maybe we could stick to plain ASCII symbols like !, or x instead.

}

static String formatFailure(String failedMessage) {
return String.format("[MEMORY_FOOTPRINT]: ❌FAIL❌ %s ❌ ", failedMessage);
@JvmStatic
fun formatFailure(failedMessage: String): String {
return "[MEMORY_FOOTPRINT]: ❌FAIL❌ $failedMessage"
}

static String formatException(String exceptionMessage) {
return String.format(
"%s\n%s",
"❗❗❗❗ Something went wrong. Please retry or seek assistance.❗❗❗❗",
exceptionMessage);
@JvmStatic
fun formatException(exceptionMessage: String): String {
return "❗❗❗❗ Something went wrong. Please retry or seek assistance.❗❗❗❗\n$exceptionMessage"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ static SizedIterator<UserConfigSet> buildConfigSets(Iterable<UserConfigKey> conf

private static SizedIterator<UserConfigSet> buildConfigSets(Iterator<UserConfigKey> configs) {
if (!configs.hasNext()) {
return SizedIterator.fromIterator(emptyIterator(), 0);
return SizedIterator.Companion.fromIterator(emptyIterator(), 0);
}
UserConfigKey head = configs.next();
SizedIterator<UserConfigSet> tailExpanded = buildConfigSets(configs);
return SizedIterator.combine(
return SizedIterator.Companion.combine(
tailExpanded,
head.getConfigurationValues(),
(configSet, configValue) -> configSet.plus(head, configValue),
Expand Down
Loading
Loading