reorganized the samples, again. Moved the programs that were more test cases to the unit test directory and repackaged the unit test directory.

This commit is contained in:
clay_shooter
2006-03-18 16:21:28 +00:00
parent 288b2da21a
commit 4eb49215cb
61 changed files with 767 additions and 711 deletions

View File

@@ -0,0 +1,276 @@
package com.jacob.test.safearray;
import com.jacob.com.*;
class SafeArrayContents
{
public static void printArray(boolean a[])
{
System.out.print("[");
for(int i=0;i<a.length;i++) {
System.out.print(" " + a[i] + " ");
}
System.out.println("]");
}
public static void printArray(int a[])
{
System.out.print("[");
for(int i=0;i<a.length;i++) {
System.out.print(" " + a[i] + " ");
}
System.out.println("]");
}
public static void printArray(short a[])
{
System.out.print("[");
for(int i=0;i<a.length;i++) {
System.out.print(" " + a[i] + " ");
}
System.out.println("]");
}
public static void printArray(byte a[])
{
System.out.print("[");
for(int i=0;i<a.length;i++) {
System.out.print(" " + a[i] + " ");
}
System.out.println("]");
}
public static void printArray(double a[])
{
System.out.print("[");
for(int i=0;i<a.length;i++) {
System.out.print(" " + a[i] + " ");
}
System.out.println("]");
}
public static void printArray(float a[])
{
System.out.print("[");
for(int i=0;i<a.length;i++) {
System.out.print(" " + a[i] + " ");
}
System.out.println("]");
}
public static void printArray(String a[])
{
System.out.print("[");
for(int i=0;i<a.length;i++) {
System.out.print(" " + a[i] + " ");
}
System.out.println("]");
}
public static void printArray(Variant a[])
{
System.out.print("[");
for(int i=0;i<a.length;i++) {
System.out.print(" " + a[i] + " ");
}
System.out.println("]");
}
public static void printArray(char a[])
{
System.out.print("[");
for(int i=0;i<a.length;i++) {
System.out.print(" " + a[i] + " ");
}
System.out.println("]");
}
public static void main(String[] args)
{
// int
System.out.println("Int");
SafeArray ia = new SafeArray(Variant.VariantInt,4);
System.out.println("elem size:"+ia.getElemSize());
int iack[] = new int[] {100000,200000,300000,400000};
printArray(iack);
ia.fromIntArray(iack);
iack = ia.toIntArray();
printArray(iack);
int i4[] = new int[4];
ia.getInts(0, 4, i4, 0);
printArray(i4);
SafeArray ia2 = new SafeArray(Variant.VariantInt,4);
ia2.setInts(0, 4, i4, 0);
iack = ia2.toIntArray();
printArray(iack);
// double
System.out.println("Double");
SafeArray da = new SafeArray(Variant.VariantDouble,4);
System.out.println("elem size:"+da.getElemSize());
double dack[] = new double[] {123.456,456.123,1234567.89,12.3456789};
printArray(dack);
da.fromDoubleArray(dack);
dack = da.toDoubleArray();
printArray(dack);
double d4[] = new double[4];
da.getDoubles(0, 4, d4, 0);
printArray(d4);
SafeArray da2 = new SafeArray(Variant.VariantDouble,4);
da2.setDoubles(0, 4, d4, 0);
dack = da2.toDoubleArray();
printArray(dack);
// float
System.out.println("Float");
SafeArray fa = new SafeArray(Variant.VariantFloat,4);
System.out.println("elem size:"+fa.getElemSize());
float fack[] = new float[] {123.456F,456.123F,1234567.89F,12.3456789F};
printArray(fack);
fa.fromFloatArray(fack);
fack = fa.toFloatArray();
printArray(fack);
float f4[] = new float[4];
fa.getFloats(0, 4, f4, 0);
printArray(f4);
SafeArray fa2 = new SafeArray(Variant.VariantFloat,4);
fa2.setFloats(0, 4, f4, 0);
fack = fa2.toFloatArray();
printArray(fack);
// boolean
System.out.println("Boolean");
SafeArray ba = new SafeArray(Variant.VariantBoolean,4);
System.out.println("elem size:"+ba.getElemSize());
boolean back[] = new boolean[] {true, false, true, false};
printArray(back);
ba.fromBooleanArray(back);
back = ba.toBooleanArray();
printArray(back);
boolean b4[] = new boolean[4];
ba.getBooleans(0, 4, b4, 0);
printArray(b4);
SafeArray ba2 = new SafeArray(Variant.VariantBoolean,4);
ba2.setBooleans(0, 4, b4, 0);
back = ba2.toBooleanArray();
printArray(back);
// char
System.out.println("Char");
SafeArray ca = new SafeArray(Variant.VariantShort,4);
System.out.println("elem size:"+ca.getElemSize());
char cack[] = new char[] {'a','b','c','d'};
printArray(cack);
ca.fromCharArray(cack);
cack = ca.toCharArray();
printArray(cack);
char c4[] = new char[4];
ca.getChars(0, 4, c4, 0);
printArray(c4);
SafeArray ca2 = new SafeArray(Variant.VariantShort,4);
ca2.setChars(0, 4, c4, 0);
cack = ca2.toCharArray();
printArray(cack);
// short
System.out.println("Short");
SafeArray sha = new SafeArray(Variant.VariantShort,4);
System.out.println("elem size:"+sha.getElemSize());
short shack[] = new short[] {1000,2000,3000,4000};
printArray(shack);
sha.fromShortArray(shack);
shack = sha.toShortArray();
printArray(shack);
short sh4[] = new short[4];
sha.getShorts(0, 4, sh4, 0);
printArray(sh4);
SafeArray sha2 = new SafeArray(Variant.VariantShort,4);
sha2.setShorts(0, 4, sh4, 0);
shack = sha2.toShortArray();
printArray(shack);
// string
System.out.println("String");
SafeArray sa = new SafeArray(Variant.VariantString,4);
System.out.println("elem size:"+sa.getElemSize());
String sack[] = new String[] {"aa","bb","cc","dd"};
printArray(sack);
sa.fromStringArray(sack);
sack = sa.toStringArray();
printArray(sack);
String s4[] = new String[4];
sa.getStrings(0, 4, s4, 0);
printArray(s4);
SafeArray sa2 = new SafeArray(Variant.VariantString,4);
sa2.setStrings(0, 4, s4, 0);
sack = sa2.toStringArray();
printArray(sack);
// variant
System.out.println("Variant");
SafeArray va = new SafeArray(Variant.VariantVariant,4);
System.out.println("elem size:"+va.getElemSize());
Variant vack[] = new Variant[]
{
new Variant(1),
new Variant(2.3),
new Variant(true),
new Variant("four"),
};
printArray(vack);
va.fromVariantArray(vack);
vack = va.toVariantArray();
printArray(vack);
Variant v4[] = new Variant[4];
va.getVariants(0, 4, v4, 0);
printArray(v4);
SafeArray va2 = new SafeArray(Variant.VariantVariant,4);
va2.setVariants(0, 4, v4, 0);
vack = va2.toVariantArray();
printArray(vack);
// byte
System.out.println("Byte");
SafeArray bba = new SafeArray(Variant.VariantByte,4);
System.out.println("elem size:"+bba.getElemSize());
byte bback[] = new byte[] {0x1,0x2,0x3,0x4};
printArray(bback);
bba.fromByteArray(bback);
bback = bba.toByteArray();
printArray(bback);
byte bb4[] = new byte[4];
bba.getBytes(0, 4, bb4, 0);
printArray(bb4);
SafeArray bba2 = new SafeArray(Variant.VariantByte,4);
bba2.setBytes(0, 4, bb4, 0);
bback = bba2.toByteArray();
printArray(bback);
try {
System.out.println("Should see a com exception right after this line...");
// this should throw ComException
bba2.fromCharArray(new char[] {'a'});
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,113 @@
package com.jacob.test.safearray;
import com.jacob.com.ComThread;
import com.jacob.com.SafeArray;
import com.jacob.com.Variant;
/**
* run with -Djava.library.path=d:/jacob/release -Dcom.jacob.autogc=false -Dcom.jacob.debug=false
* SF 1085370
In my understatnding, an instance of SafeArray java
class has a
value of a pointer to VARIANT structure that contains a
pointer to
a SAFEARRAY strucuture.
On the other hand, we can create a Variant object from
the
SafeArray object like this:
SafeArray sa = ...;
Variant val = new Variant(sa);
the val object has a pointer to another VARIANT
structure that
contains a pointer to the same SAFEARRAY structure.
In this case, the val object has a pointer to another
VARIANT that
contains a pointer to the same SAFEARRAY like this:
+-----------+
|SafeArray | +------------+
| m_pV--->VARIANT(a) |
+-----------+ | VT_ARRAY| +---------+
| parray---->SAFEARRAY|
+------------+ +^--------+
|
+-----------+ |
|Variant | +------------+ |
| m_pVariant--->VARIANT(b) | |
+-----------+ | VT_ARRAY| |
| parray-----+
+------------+
When previous objects are rereased by
ComThread.Release(),
first the VARIANT(a) is released by VariantClear()
function,
and second the VARIANT(b) is released by VariantClear()
function too.
But the SAFEARRAY was already released by the
VARIANT(a).
So, in my enviroment (WinXP + J2SDK 1.4.1) the
following java program
is sometimes crash with EXCEPTION_ACCESS_VIOLATION.
To solve this problem, it is nessesary to copy the
SAFEARRAY like this:
+-----------+
|Variant | +------------+
| m_pVariant--->VARIANT(a) |
+-----------+ | VT_ARRAY| +---------+
| parray---->SAFEARRAY|
+------------+ +|--------+
|
+-----------+ | copySA()
|SafeArray | +------------+ |
| m_pV--->VARIANT(b) | V
+-----------+ | VT_ARRAY| +---------+
| parray---->SAFEARRAY|
+------------+ +---------+
*/
public class SafeArrayReleaseTest
{
final static int MAX = 300;
public static void main(String[] args)
{
int count;
System.out.println("Starting test for max = "+MAX);
for(count = 1; count<MAX; count++)
{
int i = 0;
try
{
ComThread.InitMTA();
for (i = 0; i < count; i++)
{
SafeArray a1 = new SafeArray(Variant.VariantVariant, 2);
a1.setVariant(0, new Variant("foo"));
a1.setVariant(1, new Variant("bar"));
Variant v = new Variant(a1);
SafeArray a2 = v.toSafeArray(true);
if (a2 == null){
System.out.println("got null back from toSafeArray()");
}
}
ComThread.Release();
System.gc();
//System.out.print(".");
}
catch (Exception e)
{
System.out.println("\nTest fails with i = " + i + " (max = "+MAX+")");
e.printStackTrace();
break;
}
}
System.gc();
System.out.println("\nTest ends with count = " + count + " (max = "+MAX+")");
}
}

View File

@@ -0,0 +1,60 @@
package com.jacob.test.safearray;
import com.jacob.com.*;
import com.jacob.activeX.*;
public class SafeArrayViaExcel
{
public static void main(java.lang.String[] args)
{
//deprecated
//System.runFinalizersOnExit(true);
ActiveXComponent xl = new ActiveXComponent("Excel.Application");
try {
Dispatch cell;
SafeArray sAProdText;
Dispatch workbooks = xl.getProperty("Workbooks").toDispatch();
System.out.println("have workbooks");
Dispatch workbook = Dispatch.call(workbooks, "Open", "d:\\jacob\\samples\\test\\ExcelSafeArray" +
".xls").toDispatch();
System.out.println("Opened File - jacobtest.xls\n");
Dispatch sheet = Dispatch.get(workbook,"ActiveSheet").toDispatch();
cell = Dispatch.invoke(sheet,"Range",Dispatch.Get,new Object[] {"A1:D1000"},new int[1]).toDispatch();
System.out.println("have cell:"+cell);
sAProdText = Dispatch.get(cell,"Value").toSafeArray();
System.out.println("sa: dim="+sAProdText.getNumDim());
System.out.println("sa: start row="+sAProdText.getLBound(1));
System.out.println("sa: start col="+sAProdText.getLBound(2));
System.out.println("sa: end row="+sAProdText.getUBound(1));
System.out.println("sa: end col="+sAProdText.getUBound(2));
int i;
int lineNumber=1;
int n = 0;
for(lineNumber=1; lineNumber < 1000; lineNumber++)
{
for (i = 1 ; i < 4 ; i++ ) {
System.out.println((n++) + " " + lineNumber+" "+i+" " +sAProdText.getString(lineNumber,i));
/*
if (sAProdText.getString(lineNumber,i).compareTo("aaaa") != 0 ) {
System.out.println("Invalid String in line " + lineNumber + " Cell " + i + " Value = " + sAProdText.getString(lineNumber,i));
stringFound = false;
}
}
if (stringFound) {
System.out.println("Valid Strings in line " + lineNumber);
lineNumber++;
}
*/
}
}
Dispatch.call(workbook, "Close");
System.out.println("Closed File\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
xl.invoke("Quit", new Variant[] {});
}
}
}

View File

@@ -0,0 +1,42 @@
package com.jacob.test.safearray;
import com.jacob.com.*;
import com.jacob.activeX.*;
class sa_dispatch
{
public static void main(String args[])
{
// deprecated
//System.runFinalizersOnExit(true);
try {
String lang = "VBScript";
ActiveXComponent sC = new ActiveXComponent("ScriptControl");
Dispatch sControl = (Dispatch)sC.getObject();
Dispatch.put(sControl, "Language", lang);
Variant result = Dispatch.call(sControl, "Eval", args[0]);
System.out.println("eval("+args[0]+") = "+ result);
// wrap the script control in a variant
Variant v = new Variant(sControl);
// create a safe array of type dispatch
SafeArray sa = new SafeArray(Variant.VariantDispatch, 1);
// put the variant in the array
sa.setVariant(0, v);
// take it back out
Variant v2 = sa.getVariant(0);
Dispatch d = v2.toDispatch();
// make sure you can call eval on it
result = Dispatch.call(d, "Eval", args[0]);
System.out.println("eval("+args[0]+") = "+ result);
} catch (ComException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,98 @@
package com.jacob.test.safearray;
import com.jacob.com.*;
/**
* SafeArrayTest Program
*
* This is more of an exerciser. It doesn't verify that it gets back
* what it expects like a junit test would
*
*/
class sa_test {
public static void main(String[] args) {
//System.runFinalizersOnExit(true);
SafeArray sa = new SafeArray(Variant.VariantVariant, 3);
sa.fromShortArray(new short[] { 1, 2, 3 });
System.out.println("sa short=" + sa);
int[] ai = sa.toIntArray();
for (int i = 0; i < ai.length; i++) {
System.out.println("toInt=" + ai[i]);
}
double[] ad = sa.toDoubleArray();
for (int i = 0; i < ad.length; i++) {
System.out.println("toDouble=" + ad[i]);
}
sa.fromIntArray(new int[] { 100000, 200000, 300000 });
System.out.println("sa int=" + sa);
ai = sa.toIntArray();
for (int i = 0; i < ai.length; i++) {
System.out.println("toInt=" + ai[i]);
}
ad = sa.toDoubleArray();
for (int i = 0; i < ad.length; i++) {
System.out.println("toDouble=" + ad[i]);
}
Variant av[] = sa.toVariantArray();
for (int i = 0; i < av.length; i++) {
System.out.println("toVariant=" + av[i]);
}
sa.fromDoubleArray(new double[] { 1.5, 2.5, 3.5 });
System.out.println("sa double=" + sa);
sa.fromFloatArray(new float[] { 1.5F, 2.5F, 3.5F });
System.out.println("sa float=" + sa);
sa.fromBooleanArray(new boolean[] { true, false, true, false });
System.out.println("sa bool=" + sa);
av = sa.toVariantArray();
for (int i = 0; i < av.length; i++) {
System.out.println("toVariant=" + av[i]);
}
sa.fromCharArray(new char[] { 'a', 'b', 'c', 'd' });
System.out.println("sa char=" + sa);
sa.fromStringArray(new String[] { "hello", "from", "java", "com" });
System.out.println("sa string=" + sa);
av = sa.toVariantArray();
for (int i = 0; i < av.length; i++) {
System.out.println("toVariant=" + av[i]);
}
sa.fromVariantArray(new Variant[] {
new Variant(1), new Variant(2.3), new Variant("hi") });
System.out.println("sa variant=" + sa);
sa_ND_test();
}
public static void sa_ND_test() {
int[] lowerBounds = new int[] { 0, 0, 0 };
int[] dimensionSizes = new int[] { 3, 3, 3 };
SafeArray sa3x3 = new SafeArray(Variant.VariantVariant, lowerBounds,
dimensionSizes);
System.out.println("Num Dimensions = " + sa3x3.getNumDim());
for (int i = 1; i <= sa3x3.getNumDim(); i++) {
System.out.println("Dimension number = " + i);
System.out.println("Lower bound = " + sa3x3.getLBound(i));
System.out.println("Upper bound = " + sa3x3.getUBound(i));
}
int fill = 0;
int[] indices = new int[] { 0, 0, 0 };
for (int i = 0; i < 3; i++) {
indices[0] = i;
for (int j = 0; j < 3; j++) {
indices[1] = j;
for (int k = 0; k < 3; k++) {
indices[2] = k;
fill = i * 100 + j * 10 + k;
sa3x3.setInt(indices, fill);
System.out.println("sa[" + i + "][" + j + "][" + k + "] = "
+ sa3x3.getInt(indices));
}
}
}
}
}