Skip to content

Instantly share code, notes, and snippets.

@devoncarew
Created July 31, 2024 22:23
Show Gist options
  • Save devoncarew/8f9fe06a67328d6c82c35c202915227e to your computer and use it in GitHub Desktop.
Save devoncarew/8f9fe06a67328d6c82c35c202915227e to your computer and use it in GitHub Desktop.
// Add two numbers x and y
// >>> add(2, 3)
// 5
// >>> add(5, 7)
// 12
int add(int x, int y) {
// generated from gemini
return x + y;
}
void main() {
final candidate = add;
expect(candidate(0, 1), 1);
expect(candidate(1, 0), 1);
expect(candidate(2, 3), 5);
expect(candidate(5, 7), 12);
expect(candidate(7, 5), 12);
print('success');
}
void expect(dynamic a, dynamic b) {
if (a == b) return;
if (a is List && b is List) {
expectList(a, b);
} else if (a is Map && b is Map) {
expectMap(a, b);
} else {
throw '$a != $b';
}
}
void expectList(List a, List b) {
if (a.length != b.length) throw 'list lengths are not equal';
for (var i = 0; i < a.length; i++) {
expect(a[i], b[i]);
}
}
void expectMap(Map a, Map b) {
if (a.length != b.length) throw 'map lengths are not equal';
for (var key in a.keys) {
expect(a[key], b[key]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment