This commit is contained in:
Ziver Koc 2008-11-14 16:38:36 +00:00
commit 613bef2496
108 changed files with 8397 additions and 0 deletions

View file

@ -0,0 +1,25 @@
package zutil.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import zutil.ui.Console;
public class ConsoleTest {
public static void main(String[] args) throws IOException{
new Console("Console Test", true);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("hello= "+in.readLine());
for(int i=0; i<20 ;i++){
System.out.println(i+"Hello World!!!sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss");
System.err.println(i+"Hello World!!!sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss");
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
}
}
}
}

View file

@ -0,0 +1,38 @@
package zutil.test;
import zutil.Encrypter;
public class EncryptionTest {
public static String data = "Hello there wats yor name my is a secret 123456789";
public static Encrypter enc;
public static Encrypter enc2;
public static void main(String[] args) throws Exception {
System.out.println("input text : " + data);
//****************************************************************************************
System.out.println("Test1 passphrase");
Encrypter.randomizeSalt();
enc = new Encrypter("Hello World!!", Encrypter.PASSPHRASE_DES_ALGO);
enc2 = new Encrypter("Hello World!!", Encrypter.PASSPHRASE_DES_ALGO);
byte[] encrypted = enc.encrypt(data.getBytes());
System.out.println("cipher text: " + new String(encrypted) + " bytes: " + encrypted.length);
byte[] decrypted = enc2.decrypt(encrypted);
System.out.println("plain text : " + new String(decrypted) + " bytes: " + decrypted.length);
//****************************************************************************************
System.out.println("Test2 randome");
Encrypter.randomizeSalt();
enc = new Encrypter(Encrypter.BLOWFISH_ALGO);
Encrypter.randomizeSalt();
enc2 = new Encrypter(enc.getKey());
encrypted = enc.encrypt(data.getBytes());
System.out.println("cipher text: " + new String(encrypted) + " bytes: " + encrypted.length);
decrypted = enc2.decrypt(encrypted);
System.out.println("plain text : " + new String(decrypted) + " bytes: " + decrypted.length);
}
}

View file

@ -0,0 +1,20 @@
package zutil.test;
import java.io.File;
import zutil.algo.sort.ExternalSort;
public class ExternalSortTest {
public static void main(String[] args){
try {
File file = new File("C:\\Users\\Ziver\\Desktop\\IndexFile.txt");
File sortedFile = new File("C:\\Users\\Ziver\\Desktop\\SortedIndexFile.txt");
ExternalSort sort = new ExternalSort(file, sortedFile);
sort.sort();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,29 @@
package zutil.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URISyntaxException;
import zutil.FileChangeListener;
import zutil.FileFinder;
import zutil.FileWatcher;
public class FileChangedTest implements FileChangeListener{
public static void main(String[] args) throws URISyntaxException, FileNotFoundException{
FileWatcher watcher = new FileWatcher(FileFinder.find("test.txt"));
watcher.setListener(new FileChangedTest());
while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void fileChangedEvent(File file) {
System.out.println(file);
}
}

View file

@ -0,0 +1,26 @@
package zutil.test;
import java.io.File;
import java.net.URISyntaxException;
import java.util.ArrayList;
import zutil.FileFinder;
import zutil.Hasher;
public class FileFinderHasherTest {
public static void main(String[] args) throws URISyntaxException{
String relativePath = "zutil/test";
File path = FileFinder.find(relativePath);
ArrayList<File> files = FileFinder.search(path);
for(int i=0; i<files.size(); i++){
try {
System.out.println(
FileFinder.relativePath(files.get(i), relativePath)+
": "+Hasher.hash(files.get(i),"MD5"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

View file

@ -0,0 +1,79 @@
package zutil.test;
import java.io.IOException;
import java.util.HashMap;
import zutil.network.http.HttpPage;
import zutil.network.http.HttpPrintStream;
import zutil.network.http.HttpServer;
public class HTTPGuessTheNumber implements HttpPage{
public static void main(String[] args) throws IOException{
HttpServer server = new HttpServer("localhost", 80);
server.setDefaultPage(new HTTPGuessTheNumber());
server.run();
}
public void respond(HttpPrintStream out,
HashMap<String, String> client_info,
HashMap<String, String> session, HashMap<String, String> cookie,
HashMap<String, String> request) {
out.enableBuffering(true);
out.println("<html>");
out.println("<H2>Welcome To The Number Guess Game!</H2>");
if(session.containsKey("random_nummber") && request.containsKey("guess")){
int guess = Integer.parseInt(request.get("guess"));
int nummber = Integer.parseInt(session.get("random_nummber"));
try {
if(guess == nummber){
session.remove("random_nummber");
out.println("You Guessed Right! Congrats!");
out.println("</html>");
return;
}
else if(guess > nummber){
out.println("<b>To High</b><br>");
if(Integer.parseInt(cookie.get("high")) > guess){
out.setCookie("high", ""+guess);
cookie.put("high", ""+guess);
}
}
else{
out.println("<b>To Low</b><br>");
if(Integer.parseInt(cookie.get("low")) < guess){
out.setCookie("low", ""+guess);
cookie.put("low", ""+guess);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
else{
session.put("random_nummber", ""+(int)(Math.random()*99+1));
try {
out.setCookie("low", "0");
out.setCookie("high", "100");
cookie.put("low", "0");
cookie.put("high", "100");
} catch (Exception e) {
e.printStackTrace();
}
}
out.println("<form method='post'>");
out.println(cookie.get("low")+" < X < "+cookie.get("high")+"<br>");
out.println("Guess a number between 0 and 100:<br>");
out.println("<input type='text' name='guess'>");
out.println("<input type='hidden' name='test' value='test'>");
out.println("<input type='submit' value='Guess'>");
out.println("</form>");
out.println("<script>document.all.guess.focus();</script>");
//out.println("<b>DEBUG: nummber="+session.get("random_nummber")+"</b><br>");
out.println("</html>");
}
}

View file

@ -0,0 +1,116 @@
package zutil.test;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JLabel;
import zutil.ProgressListener;
import zutil.image.ImageFilterProcessor;
import zutil.image.filters.BlurFilter;
import zutil.image.filters.ColorIntensityFilter;
import zutil.image.filters.ContrastBrightnessFilter;
import zutil.image.filters.DitheringFilter;
import zutil.image.filters.FaceDetectionFilter;
import zutil.image.filters.MedianFilter;
import zutil.image.filters.ResizeImage;
import zutil.image.filters.SpotLightFilter;
@SuppressWarnings("unused")
public class ImageProcessorTest implements ProgressListener{
private static String imgPath = "Image6.gif";
private JLabel processedLabel;
private JLabel orginalLabel;
private JProgressBar progress;
public static void main(String[] args){
new ImageProcessorTest();
}
public ImageProcessorTest(){
JFrame frame = getJFrame();
BufferedImage img = null;
try {
// Read from an input stream
InputStream is = new BufferedInputStream(new FileInputStream(imgPath));
img = ImageIO.read(is);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
ImageIcon orginalIcon = new ImageIcon(img);
orginalLabel.setIcon(orginalIcon);
frame.setVisible(true);
frame.pack();
BufferedImage procImg = null;
try {
//ImageFilterProcessor processor = new SpotLightFilter(img,100,100,100);
//ImageFilterProcessor processor = new ContrastBrightnessFilter(img);
//ImageFilterProcessor processor = new ColorIntensityFilter(img, true);
//ImageFilterProcessor processor = new BlurFilter(img);
//ImageFilterProcessor processor = new DitheringFilter(img);
//ImageFilterProcessor processor = new ResizeImage(img,100,100);
//ImageFilterProcessor processor = new MedianFilter(img);
ImageFilterProcessor processor = new FaceDetectionFilter(img);
processor.setProgressListener(this);
procImg = processor.process();
} catch (Exception e) {
e.printStackTrace();
}
ImageIcon processedIcon = new ImageIcon(procImg);
processedLabel.setIcon(processedIcon);
frame.pack();
}
private JFrame getJFrame() {
processedLabel = new JLabel("Processed");
orginalLabel = new JLabel("Orginal");
progress = new JProgressBar();
progress.setMaximum(100);
progress.setValue(0);
progress.setIndeterminate(false);
progress.setStringPainted(true);
JPanel jPanel = new JPanel();
jPanel.setLayout(new BorderLayout());
jPanel.add(orginalLabel, BorderLayout.NORTH);
jPanel.add(processedLabel, BorderLayout.CENTER);
jPanel.add(progress, BorderLayout.SOUTH);
JFrame jFrame = new JFrame("ImageProcessorTest");
jFrame.setSize(new Dimension(715, 361));
jFrame.setContentPane(jPanel);
jFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
return jFrame;
}
public void progressUpdate(Object source, Object info, double percent) {
progress.setValue((int)percent);
}
}

View file

@ -0,0 +1,37 @@
package zutil.test;
import java.io.IOException;
import java.net.InetAddress;
import java.security.NoSuchAlgorithmException;
import zutil.Encrypter;
import zutil.network.nio.NioClient;
import zutil.network.nio.message.StringMessage;
import zutil.network.nio.response.PrintRsp;
@SuppressWarnings("unused")
public class NetworkClientTest {
public static void main(String[] args) throws NoSuchAlgorithmException {
try {
int count = 0;
long time = System.currentTimeMillis()+1000*60;
NioClient client = new NioClient(InetAddress.getByName("localhost"), 6056);
//client.setEncrypter(new Encrypter("lol", Encrypter.PASSPHRASE_DES_ALGO));
while(time > System.currentTimeMillis()){
PrintRsp handler = new PrintRsp();
client.send(handler, new StringMessage("StringMessage: "+count));
handler.waitForResponse();
//try {Thread.sleep(100);} catch (InterruptedException e) {}
//System.out.println("sending..");
count++;
}
System.out.println("Message Count 1m: "+count);
System.out.println("Message Count 1s: "+count/60);
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,20 @@
package zutil.test;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import zutil.Encrypter;
import zutil.network.nio.NioServer;
@SuppressWarnings("unused")
public class NetworkServerTest {
public static void main(String[] args) throws NoSuchAlgorithmException {
try {
NioServer server = new NioServer(6056);
//server.setEncrypter(new Encrypter("lol", Encrypter.PASSPHRASE_DES_ALGO));
} catch (IOException e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,26 @@
package zutil.test;
import java.util.Arrays;
import zutil.algo.QuickSelect;
import zutil.algo.sort.sortable.SortableIntArray;
public class QuickSelectTest {
public static void main(String[] args){
int[] array = {1,3,4,6,3,2,98,5,7,8,543,2,4,5,8,9,5,2,3,5,7,5,3,2,6,8,5,324,8,6};
//int[] array = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,17,18,19,20};
long time = System.currentTimeMillis();
int median = (Integer)QuickSelect.find(new SortableIntArray(array), array.length/2);
System.out.println("QuickSelection("+(System.currentTimeMillis()-time)+"ms): "+median);
time = System.currentTimeMillis();
Arrays.sort(array);
System.out.println("RightAnswer("+(System.currentTimeMillis()-time)+"ms): "+array[array.length/2]);
System.out.println("Sorted Array("+array.length+"): ");
for(int i=0; i<array.length ;i++){
System.out.println(array[i] +",");
}
}
}

View file

@ -0,0 +1,137 @@
package zutil.test;
import zutil.algo.sort.QuickSort;
import zutil.algo.sort.sortable.SortableIntArray;
import junit.framework.*;
public class QuickSortTest extends TestCase {
public static boolean debug = false;
//the size of the arrays to be tested
private static final int[] cases = new int[]{10000,100000,1000000,10000000};
//the type of array to be tested
// 0 = random
// 1 = mirror
// 2 = sorted
private static final int[] types = new int[]{0,1,2};
//the strings for the diffrent arrays
private static final String[] typesS = new String[]{"Random array","Mirrored array","Sorted array"};
//the pivots that will be tested
// 0 = random
// 1 = median
// 2 = middle
private static final int[] pivots = new int[]{0,1,2};
//the strings for the pivots
private static final String[] pivotsS = new String[]{"Random pivot","Median pivot","Half pivot"};
//the current array size index of cases
private static int currentCase = 0;
//the current type of arrays
private static int typeCase = 0;
//the current pivot to use
private static int pivotCase = 0;
//the current state of using insertionsort in quicksort
private static boolean insertSort;
//the temp array that will be sorted
private int[] array;
/**
*The main method to run the test. was going to use junit but did
*no find a way to loop the test like in this method.
*/
public static void main(String[] args){
QuickSortTest test = new QuickSortTest("Test");
insertSort = true;
//the insertion sort tests loop
for(int z=0; z<2 ; insertSort=false,z++){
System.out.println("**************** Whit insertionSort: "+insertSort+" *****************");
// the pivots tests loop
for(pivotCase=0; pivotCase<pivots.length ;pivotCase++){
System.out.println("********** "+pivotsS[pivots[pivotCase]]+" ***********");
//the array size tests loop
for(typeCase=0; typeCase<types.length ;typeCase++){
currentCase = 0;
System.out.println("**** "+typesS[types[typeCase]]+" ****");
//the array size loop
for(currentCase=0; currentCase<cases.length ;currentCase++){
try{
test.setUp();
test.TestSort();
test.tearDown();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
}
public QuickSortTest(String name){
super(name);
}
/**
* setUp() method that initializes common objects
*/
public void setUp() throws Exception{
super.setUp();
array = new int[cases[currentCase]];
switch(types[typeCase]){
//Randome Array test
case 0:
for(int i=0; i<array.length ;i++){
array[i] = (int)(Math.random()*array.length*10);
}
break;
//Mirrored Array test
case 1:
for(int i=0; i<array.length ;i++){
array[i] = array.length-i;
}
break;
//Sorted Array test
case 2:
for(int i=0; i<array.length ;i++){
array[i] = i;
}
break;
}
}
/**
* tearDown() method that cleanup the common objects
*/
public void tearDown() throws Exception {
super.tearDown();
}
/**
*Tests if the array is sorted
*/
public void TestSort() {
long time = System.currentTimeMillis();
if(debug){
for(int i=0; i<array.length ;i++)
System.out.print(array[i]+", ");
System.out.println("");
}
QuickSort.sort(new SortableIntArray(array),pivotCase,insertSort);
//Sort.insertionSort(array);
System.out.print("*");
//the time to sort
System.out.println(array.length+" elements: "+
((double)(System.currentTimeMillis()-time)/1000)+" sec("+
(System.currentTimeMillis()-time)+" ms)");
if(debug){
for(int i=0; i<array.length ;i++)
System.out.print(array[i]+", ");
System.out.println("");
}
//checking if sorted correct
for(int i=1; i<array.length ; i++){
if(array[i-1] > array[i]){
fail("Array not sorted!! ("+array[i-1]+" > "+array[i]+")");
}
}
}
}

View file

@ -0,0 +1,63 @@
package zutil.test;
import zutil.algo.sort.QuickSort;
import zutil.algo.sort.sortable.SortableIntArray;
import junit.framework.*;
public class QuickSortTestSimple extends TestCase {
public static void main(String[] args){
int[] array = new int[1000];
for(int i=0; i<array.length ;i++){
array[i] = (int)(Math.random()*10000);
}
for(int i=0; i<array.length ;i++){
System.out.println(array[i]);
}
//quicksort(array, 0, array.length-1);
QuickSort.sort(new SortableIntArray(array));
System.out.println("----------------------------------");
for(int i=0; i<array.length ;i++){
System.out.println(array[i]);
}
System.out.println("----------------------------------");
for(int i=1; i<array.length ; i++){
if(array[i-1] > array[i]){
System.out.println("Array not sorted!! ("+array[i-1]+" > "+array[i]+")");
}
}
}
static void quicksort (int[] a, int lo, int hi) {
// lo is the lower index, hi is the upper index
// of the region of array a that is to be sorted
int i=lo, j=hi, h;
int x=a[(lo+hi)/2];
// partition
do
{
while (a[i]<x){
i++;
}
while (a[j]>x){
j--;
}
if (i<=j)
{
h=a[i]; a[i]=a[j]; a[j]=h;
i++; j--;
}
} while (i<=j);
// recursion
if (lo<j) quicksort(a, lo, j);
if (i<hi) quicksort(a, i, hi);
}
}

View file

@ -0,0 +1,16 @@
package zutil.test;
import java.io.IOException;
import zutil.network.ServerFindClient;
public class ServerFindClientTest {
public static void main(String[] args){
try {
ServerFindClient client = new ServerFindClient(2000);
System.out.println(client.find().getHostAddress());
} catch (IOException e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,15 @@
package zutil.test;
import java.io.IOException;
import zutil.network.ServerFind;
public class ServerFindServerTest {
public static void main(String[] args){
try {
new ServerFind(2000);
} catch (IOException e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,28 @@
package zutil.test;
import zutil.ProgressListener;
import zutil.network.UpdateClient;
import zutil.network.Zupdater;
public class UpdateClientTest implements ProgressListener{
public static void main(String[] args){
UpdateClientTest client = new UpdateClientTest();
client.start();
}
public void start(){
try {
UpdateClient client = new UpdateClient("localhost", 2000, "client");
client.setProgressListener(new Zupdater());
//client.setProgressListener(this);
client.update();
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void progressUpdate(Object source, Object info, double percent) {
System.out.println(info+": "+percent+"%");
}
}

View file

@ -0,0 +1,13 @@
package zutil.test;
import zutil.network.UpdateServer;
public class UpdateServerTest {
public static void main(String[] args){
try {
new UpdateServer(2000, "server");
}catch (Exception e) {
e.printStackTrace();
}
}
}