1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using libsbmlcs;
namespace ConsoleApplication1
{
public class TestConstructors
{
private static bool HasLevelAndVersionConstructor(Type type)
{
var constructors = type.GetConstructors();
foreach (var item in constructors)
{
var parameters = item.GetParameters();
if (parameters.Length == 2 && parameters[0].ParameterType == typeof(long)
&& parameters[1].ParameterType == typeof(long)) return true;
}
return false;
}
private static List<Type> FilterTypes(List<Type> result)
{
for (int i = result.Count - 1; i >= 0; i--)
{
if (!HasLevelAndVersionConstructor(result[i]))
result.RemoveAt(i);
}
return result;
}
private static List<Type> FindConstructorsWithLevelAndVersion()
{
var assembly = typeof(libsbml).Assembly;
var allTypes = assembly.GetExportedTypes();
var result = new List<Type>();
result.AddRange(allTypes);
return FilterTypes(result);
}
private static string TestLevelVersionConstructor(Type item, int level, int version)
{
bool exceptionOccurred = false;
bool wrapped = false;
var result = new StringBuilder();
Exception ex1 = null;
try
{
Activator.CreateInstance(item, level, version);
}
catch (Exception ex)
{
exceptionOccurred = true;
wrapped = (ex.InnerException != null && ex.InnerException is SBMLConstructorException);
ex1 = ex.InnerException;
}
if (exceptionOccurred && !wrapped)
{
result.AppendLine(String.Format("\tCreate L{0}V{1}", level, version));
result.AppendLine(String.Format("\tFailed to create L{0}V{1} for {2}", level, version, item));
if (ex1 != null)
{
result.AppendLine(String.Format("\tWARNING: This exception is NOT wrapped correctly. Type is: {0}", ex1.GetType()));
result.AppendLine();
}
}
if (!exceptionOccurred && level > 3)
{
result.AppendLine(String.Format("\tCreate L{0}V{1}", level, version));
result.AppendLine("\tWARNING: Exception should have been thrown.");
result.AppendLine();
}
return result.ToString();
}
private static void TestType(Type item)
{
var m1 = TestLevelVersionConstructor(item, 3, 1);
var m2 = TestLevelVersionConstructor(item, 2, 3);
var m3 = TestLevelVersionConstructor(item, 10, 10);
if (!string.IsNullOrWhiteSpace(m1) ||
!string.IsNullOrWhiteSpace(m2) ||
!string.IsNullOrWhiteSpace(m3))
{
Console.WriteLine("Testing Type: {0}", item);
Console.WriteLine("=============================================");
if (!string.IsNullOrWhiteSpace(m1))
Console.WriteLine(m1);
if (!string.IsNullOrWhiteSpace(m2))
Console.WriteLine(m2);
if (!string.IsNullOrWhiteSpace(m3))
Console.WriteLine(m3);
}
}
private static void TestClasses(IEnumerable<Type> classes)
{
foreach (var item in classes)
{
TestType(item);
}
}
public static void Main(string[] args)
{
var classes = FindConstructorsWithLevelAndVersion();
TestClasses(classes);
WriteCSFile("testConstructors.cs", classes);
WriteJavaFile("testConstructors.java", classes);
WriteRBFile("testConstructors.rb", classes);
WritePLFile("testConstructors.pl", classes);
WritePYFile("testConstructors.py", classes);
}
private static void WritePYFile(string fileName, IEnumerable<Type> classes)
{
var versions = new List<Tuple<int, int>> { new Tuple<int, int>(3, 1), new Tuple<int, int> (2, 4), new Tuple<int, int> (10, 10)};
var builder = new StringBuilder();
builder.AppendLine("#!/usr/bin/env python3");
builder.AppendLine("#");
builder.AppendLine("# This file has been autogenerated to test all constructors.");
builder.AppendLine("# ");
builder.AppendLine("import sys");
builder.AppendLine("import time");
builder.AppendLine("from libsbml import *");
builder.AppendLine("");
foreach (var item in classes)
{
builder.AppendLine(string.Format("print 'Testing: {0}'", item.Name));
foreach (var combo in versions)
{
builder.AppendLine("try:");
builder.AppendLine(string.Format("\ta = {0}({1}, {2})", item.Name, combo.Item1, combo.Item2));
builder.AppendLine("except:");
builder.AppendLine(string.Format("\tprint ' Failed to create L{0}V{1} for {2}'", combo.Item1, combo.Item2, item.Name));
}
}
File.WriteAllText(fileName, builder.ToString());
}
private static void WritePLFile(string fileName, IEnumerable<Type> classes)
{
var versions = new List<Tuple<int, int>> { new Tuple<int, int>(3, 1), new Tuple<int, int> (2, 4), new Tuple<int, int> (10, 10)};
var builder = new StringBuilder();
builder.AppendLine("#!/usr/bin/env perl");
builder.AppendLine("#");
builder.AppendLine("# This file has been autogenerated to test all constructors.");
builder.AppendLine("# ");
builder.AppendLine("use LibSBML;");
builder.AppendLine("");
foreach (var item in classes)
{
builder.AppendLine(string.Format("print (\"Testing: {0}\\n\");", item.Name));
foreach (var combo in versions)
{
builder.AppendLine("eval {");
builder.AppendLine(string.Format("\t$a = new LibSBML::{0}({1}, {2});", item.Name, combo.Item1, combo.Item2));
builder.AppendLine("};");
builder.AppendLine("if ($@) {");
builder.AppendLine(string.Format("\tprint (\" Failed to create L{0}V{1} for {2}\\n\");", combo.Item1, combo.Item2, item.Name));
builder.AppendLine("} ");
}
}
File.WriteAllText(fileName, builder.ToString());
}
private static void WriteRBFile(string fileName, IEnumerable<Type> classes)
{
var versions = new List<Tuple<int, int>> { new Tuple<int, int>(3, 1), new Tuple<int, int> (2, 4), new Tuple<int, int> (10, 10)};
var builder = new StringBuilder();
builder.AppendLine("#!/usr/bin/env ruby");
builder.AppendLine("#");
builder.AppendLine("# This file has been autogenerated to test all constructors.");
builder.AppendLine("# ");
builder.AppendLine("require 'libSBML'");
builder.AppendLine("");
foreach (var item in classes)
{
builder.AppendLine(string.Format("puts 'Testing: {0}'", item.Name));
foreach (var combo in versions)
{
builder.AppendLine("begin");
builder.AppendLine(string.Format("\ta = LibSBML::{0}.new({1}, {2})", item.Name, combo.Item1, combo.Item2));
builder.AppendLine("rescue");
builder.AppendLine(string.Format("\tputs ' Failed to create L{0}V{1} for {2}'", combo.Item1, combo.Item2, item.Name));
builder.AppendLine("end");
}
}
File.WriteAllText(fileName, builder.ToString());
}
private static void WriteJavaFile(string fileName, IEnumerable<Type> classes)
{
var versions = new List<Tuple<int, int>> { new Tuple<int, int>(3, 1), new Tuple<int, int>(2, 4), new Tuple<int, int>(10, 10) };
var builder = new StringBuilder();
builder.AppendLine("//");
builder.AppendLine("//");
builder.AppendLine("// This file has been autogenerated to test all constructors.");
builder.AppendLine("// ");
builder.AppendLine("");
builder.AppendLine("import org.sbml.libsbml.*;");
builder.AppendLine("");
builder.AppendLine("public class testConstructors {");
builder.AppendLine(" public static void main(String[] args) {");
builder.AppendLine(" System.loadLibrary(\"sbmlj\");");
foreach (var item in classes)
{
builder.AppendLine(string.Format(" System.out.println(\"Testing: {0}\");", item.Name));
foreach (var combo in versions)
{
builder.AppendLine(" try {");
builder.AppendLine(string.Format(" new {0}({1}, {2});", item.Name, combo.Item1, combo.Item2));
builder.AppendLine(" } catch (java.lang.Exception ex) {");
builder.AppendLine(string.Format(" System.out.println(\" Failed to create L{0}V{1} for {2}\");", combo.Item1, combo.Item2, item.Name));
builder.AppendLine(" } ");
}
}
builder.AppendLine(" } ");
builder.AppendLine("}");
File.WriteAllText(fileName, builder.ToString());
}
private static void WriteCSFile(string fileName, IEnumerable<Type> classes)
{
var versions = new List<Tuple<int, int>> { new Tuple<int, int>(3, 1), new Tuple<int, int> (2, 4), new Tuple<int, int> (10, 10)};
var builder = new StringBuilder();
builder.AppendLine("//");
builder.AppendLine("//");
builder.AppendLine("// This file has been autogenerated to test all constructors.");
builder.AppendLine("// ");
builder.AppendLine("");
builder.AppendLine("using libsbmlcs;");
builder.AppendLine("using System;");
builder.AppendLine("");
builder.AppendLine("public class TestConstructors {");
builder.AppendLine(" public static void Main(string[] args) {");
foreach (var item in classes)
{
builder.AppendLine(string.Format(" Console.WriteLine(\"Testing: {0}\");", item.Name));
foreach (var combo in versions)
{
builder.AppendLine(" try {");
builder.AppendLine(string.Format(" new {0}({1}, {2});", item.Name, combo.Item1, combo.Item2));
builder.AppendLine(" } catch {");
builder.AppendLine(string.Format(" Console.WriteLine(\" Failed to create L{0}V{1} for {2}\");", combo.Item1, combo.Item2, item.Name));
builder.AppendLine(" } ");
}
}
builder.AppendLine(" } ");
builder.AppendLine("}");
File.WriteAllText(fileName, builder.ToString());
}
}
}
|