-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Testdata for transpiling Java into Go
Part of #201
- Loading branch information
1 parent
e6767e3
commit a201c6a
Showing
20 changed files
with
393 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package balancedBrackets | ||
|
||
func hasBalancedBrackets(charArray string) bool {} |
51 changes: 51 additions & 0 deletions
51
testdata/java/transpile/balancedBrackets/balancedBrackets_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package balancedBrackets | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSymflowerHasBalancedBrackets(t *testing.T) { | ||
type testCase struct { | ||
Name string | ||
|
||
CharArray string | ||
|
||
ExpectedBool bool | ||
} | ||
|
||
validate := func(t *testing.T, tc *testCase) { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
actualBool := hasBalancedBrackets(tc.CharArray) | ||
|
||
assert.Equal(t, tc.ExpectedBool, actualBool) | ||
}) | ||
} | ||
|
||
validate(t, &testCase{ | ||
CharArray: "", | ||
|
||
ExpectedBool: true, | ||
}) | ||
validate(t, &testCase{ | ||
CharArray: "[", | ||
|
||
ExpectedBool: false, | ||
}) | ||
validate(t, &testCase{ | ||
CharArray: "[[[]]", | ||
|
||
ExpectedBool: false, | ||
}) | ||
validate(t, &testCase{ | ||
CharArray: "[[]]", | ||
|
||
ExpectedBool: true, | ||
}) | ||
validate(t, &testCase{ | ||
CharArray: "[[[[]]]]", | ||
|
||
ExpectedBool: true, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module balancedBrackets | ||
|
||
go 1.21.5 | ||
|
||
require github.com/stretchr/testify v1.9.0 // indirect |
21 changes: 21 additions & 0 deletions
21
testdata/java/transpile/balancedBrackets/implementation/BalancedBrackets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (C) rosettacode.org | ||
package com.eval; | ||
|
||
public class BalancedBrackets { | ||
static boolean hasBalancedBrackets(char[] charArray) { | ||
int brackets = 0; | ||
for (char ch : charArray) { | ||
if (ch == '[') { | ||
brackets++; | ||
} else if (ch == ']') { | ||
brackets--; | ||
} else { | ||
return false; // Non-bracket characters. | ||
} | ||
if (brackets < 0) { // Closing bracket before opening bracket. | ||
return false; | ||
} | ||
} | ||
return brackets == 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package binarySearch | ||
|
||
func binarySearch(a []int, x int) int { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package binarySearch | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSymflowerBinarySearch(t *testing.T) { | ||
type testCase struct { | ||
Name string | ||
|
||
A []int | ||
X int | ||
|
||
ExpectedInt int | ||
} | ||
|
||
validate := func(t *testing.T, tc *testCase) { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
actualInt := binarySearch(tc.A, tc.X) | ||
|
||
assert.Equal(t, tc.ExpectedInt, actualInt) | ||
}) | ||
} | ||
|
||
validate(t, &testCase{ | ||
A: []int(nil), | ||
X: 0, | ||
|
||
ExpectedInt: -1, | ||
}) | ||
validate(t, &testCase{ | ||
A: []int{0}, | ||
X: 5, | ||
|
||
ExpectedInt: -1, | ||
}) | ||
validate(t, &testCase{ | ||
A: []int{1, 2, 3, 4, 5}, | ||
X: 6, | ||
|
||
ExpectedInt: -1, | ||
}) | ||
validate(t, &testCase{ | ||
A: []int{1, 2, 3, 4, 5}, | ||
X: 3, | ||
|
||
ExpectedInt: 2, | ||
}) | ||
validate(t, &testCase{ | ||
A: []int{1, 5, 10, 15, 20, 25}, | ||
X: 25, | ||
|
||
ExpectedInt: 5, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module binarySearch | ||
|
||
go 1.21.5 | ||
|
||
require github.com/stretchr/testify v1.9.0 // indirect |
24 changes: 24 additions & 0 deletions
24
testdata/java/transpile/binarySearch/implementation/BinarySearch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.eval; | ||
|
||
class BinarySearch { | ||
static int binarySearch(int[] a, int x) { | ||
int index = -1; | ||
|
||
int min = 0; | ||
int max = a.length - 1; | ||
|
||
while (index == -1 && min <= max) { | ||
int m = (min + max) / 2; | ||
|
||
if (x == a[m]) { | ||
index = m; | ||
} else if (x < a[m]) { | ||
max = m - 1; | ||
} else { | ||
min = m + 1; | ||
} | ||
} | ||
|
||
return index; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package cascadingIfElse | ||
|
||
func cascadingIfElse(i int) int { | ||
} |
41 changes: 41 additions & 0 deletions
41
testdata/java/transpile/cascadingIfElse/cascadingIfElse_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cascadingIfElse | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSymflowerCascadingIfElse(t *testing.T) { | ||
type testCase struct { | ||
Name string | ||
|
||
I int | ||
|
||
ExpectedInt int | ||
} | ||
|
||
validate := func(t *testing.T, tc *testCase) { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
actualInt := cascadingIfElse(tc.I) | ||
|
||
assert.Equal(t, tc.ExpectedInt, actualInt) | ||
}) | ||
} | ||
|
||
validate(t, &testCase{ | ||
I: 0, | ||
|
||
ExpectedInt: 5, | ||
}) | ||
validate(t, &testCase{ | ||
I: 1, | ||
|
||
ExpectedInt: 2, | ||
}) | ||
validate(t, &testCase{ | ||
I: 3, | ||
|
||
ExpectedInt: 4, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module cascadingIfElse | ||
|
||
go 1.21.5 | ||
|
||
require github.com/stretchr/testify v1.9.0 // indirect |
13 changes: 13 additions & 0 deletions
13
testdata/java/transpile/cascadingIfElse/implementation/CascadingIfElse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.eval; | ||
|
||
class CascadingIfElse { | ||
static int cascadingIfElse(int i) { | ||
if (i == 1) { | ||
return 2; | ||
} else if (i == 3) { | ||
return 4; | ||
} else { | ||
return 5; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module pascalsTriangle | ||
|
||
go 1.21.5 | ||
|
||
require github.com/stretchr/testify v1.9.0 // indirect |
21 changes: 21 additions & 0 deletions
21
testdata/java/transpile/pascalsTriangle/implementation/PascalsTriangle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.eval; | ||
|
||
public class PascalsTriangle { | ||
int[][] generateTriangle(int rows) { | ||
if (rows < 0) { | ||
throw new IllegalArgumentException("Rows can't be negative!"); | ||
} | ||
|
||
int[][] triangle = new int[rows][]; | ||
|
||
for (int i = 0; i < rows; i++) { | ||
triangle[i] = new int[i + 1]; | ||
triangle[i][0] = 1; | ||
for (int j = 1; j < i; j++) { | ||
triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j]; | ||
} | ||
triangle[i][i] = 1; | ||
} | ||
return triangle; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package pascalsTriangle | ||
|
||
func pascalsTriangle(rows int) ([][]int, error) { | ||
} |
58 changes: 58 additions & 0 deletions
58
testdata/java/transpile/pascalsTriangle/pascalsTriangle_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package pascalsTriangle | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSymflowerPascalsTriangle(t *testing.T) { | ||
type testCase struct { | ||
Name string | ||
|
||
Rows int | ||
|
||
ExpectedSlice [][]int | ||
ExpectedError error | ||
} | ||
|
||
validate := func(t *testing.T, tc *testCase) { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
actualSlice, actualError := pascalsTriangle(tc.Rows) | ||
|
||
assert.Equal(t, tc.ExpectedSlice, actualSlice) | ||
assert.Equal(t, tc.ExpectedError, actualError) | ||
}) | ||
} | ||
|
||
validate(t, &testCase{ | ||
Rows: -1, | ||
|
||
ExpectedSlice: [][]int(nil), | ||
ExpectedError: errors.New("Rows can't be negative!"), | ||
}) | ||
validate(t, &testCase{ | ||
Rows: 0, | ||
|
||
ExpectedSlice: [][]int{}, | ||
}) | ||
validate(t, &testCase{ | ||
Rows: 1, | ||
|
||
ExpectedSlice: [][]int{ | ||
[]int{1}, | ||
}, | ||
}) | ||
validate(t, &testCase{ | ||
Rows: 5, | ||
|
||
ExpectedSlice: [][]int{ | ||
[]int{1}, | ||
[]int{1, 1}, | ||
[]int{1, 2, 1}, | ||
[]int{1, 3, 3, 1}, | ||
[]int{1, 4, 6, 4, 1}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module isSorted | ||
|
||
go 1.21.5 | ||
|
||
require github.com/stretchr/testify v1.9.0 // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.eval; | ||
|
||
class Sort { | ||
static boolean isSorted(int[] a) { | ||
int i = 0; | ||
while (i < a.length - 1 && a[i] <= a[i + 1]) { | ||
i++; | ||
} | ||
|
||
return i == a.length - 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package isSorted | ||
|
||
func isSorted(a []int) bool { | ||
} |
Oops, something went wrong.