Skip to content

Commit

Permalink
Testdata for transpiling Java into Go
Browse files Browse the repository at this point in the history
Part of #201
  • Loading branch information
ruiAzevedo19 committed Jul 4, 2024
1 parent 7612dbd commit 367a004
Show file tree
Hide file tree
Showing 20 changed files with 393 additions and 0 deletions.
3 changes: 3 additions & 0 deletions testdata/java/transpile/balancedBrackets/balancedBrackets.go
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 testdata/java/transpile/balancedBrackets/balancedBrackets_test.go
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,
})
}
5 changes: 5 additions & 0 deletions testdata/java/transpile/balancedBrackets/go.mod
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
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;
}
}
4 changes: 4 additions & 0 deletions testdata/java/transpile/binarySearch/binarySearch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package binarySearch

func binarySearch(a []int, x int) int {
}
57 changes: 57 additions & 0 deletions testdata/java/transpile/binarySearch/binarySearch_test.go
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,
})
}
5 changes: 5 additions & 0 deletions testdata/java/transpile/binarySearch/go.mod
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
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;
}
}
4 changes: 4 additions & 0 deletions testdata/java/transpile/cascadingIfElse/cascadingIfElse.go
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 testdata/java/transpile/cascadingIfElse/cascadingIfElse_test.go
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,
})
}
5 changes: 5 additions & 0 deletions testdata/java/transpile/cascadingIfElse/go.mod
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
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;
}
}
}
5 changes: 5 additions & 0 deletions testdata/java/transpile/pascalsTriangle/go.mod
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
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;
}
}
4 changes: 4 additions & 0 deletions testdata/java/transpile/pascalsTriangle/pascalsTriangle.go
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 testdata/java/transpile/pascalsTriangle/pascalsTriangle_test.go
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},
},
})
}
5 changes: 5 additions & 0 deletions testdata/java/transpile/sort/go.mod
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
12 changes: 12 additions & 0 deletions testdata/java/transpile/sort/implementation/Sort.java
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;
}
}
4 changes: 4 additions & 0 deletions testdata/java/transpile/sort/sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package isSorted

func isSorted(a []int) bool {
}
Loading

0 comments on commit 367a004

Please sign in to comment.