Skip to content

Instantly share code, notes, and snippets.

@gmfc
Created August 25, 2013 20:41
Show Gist options
  • Select an option

  • Save gmfc/6336170 to your computer and use it in GitHub Desktop.

Select an option

Save gmfc/6336170 to your computer and use it in GitHub Desktop.
Vetor merge com teste usando jUnit.
package testvetor;
/**
*
* @author Gabriel
*/
public class Testvetor {
public static void main(String[] args) {
int a[] = {1, 3, 5, 7};
int b[] = {2, 4, 6, 8};
int v[] = merge(a, b);
for(int p:v){
System.out.println(p);
}
}
public static int[] merge(int a[], int b[]) {
int[] v = new int[a.length + b.length];
int iA = 0, iB = 0, iV = 0;
while (iA < a.length && iB < b.length) {
if (a[iA] < b[iB]) {
v[iV] = a[iA];
iA++;
} else {
v[iV] = b[iB];
iB++;
}
iV++;
}
while (iA < a.length) {
v[iV] = a[iA];
iA++;
iV++;
}
while (iB < b.length) {
v[iV] = b[iB];
iB++;
iV++;
}
return v;
}
}//merge
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testvetor;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author Gabriel
*/
public class TestvetorTest {
public TestvetorTest() {
}
/**
* Test of main method, of class Testvetor.
*/
@Test
public void testMain() {
}
/**
* Test of merge method, of class Testvetor.
*/
@Test
public void testMerge() {
int a[] = {1, 3, 5, 7};
int b[] = {2, 4, 6, 8};
int c[] = {1,2,3,4,5,6,7,8};
int t[] = Testvetor.merge(a, b);
Assert.assertArrayEquals(t, c);
System.out.println("=== Test ===");
for(int p:t){
System.out.print(p+" ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment