public void RC_EachToken() { EachTokenTestModel model = new EachTokenTestModel(); RuntimeCompiler compiler = new RuntimeCompiler(new CompilerOptions()); Assert.AreEqual("", compiler.Compile(model.GetType(), "{each:EmptyList}text{/each}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("(1/7)2 (2/7)1 (3/7)2 (4/7)8 (5/7)5 (6/7)0 (7/7)6 ", compiler.Compile(model.GetType(), "{each:ValueList}({text:thisNum}/{text:thisCount}){text:this} {/each}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("0:Item 1; 1:Item 2; 2:Item 3; ", compiler.Compile(model.GetType(), "{each:ModelList}{text:thisIndex}:{text:Text}; {/each}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("to be or not to be ", compiler.Compile(model.GetType(), "{each:ComplexModel}{each:Children}{text:this}{/each} {/each}", OutputFormat.Text).DynamicInvoke(model, null)); }
public void RC_Syntax() { String[] errorTemplates = new String[] { "{if:Condition}", "{if:Condition}test{/if}{/if}", "{when:Something}{if:Condition}test{/if}{/when}", "{when:Something}{ifnot:Condition}test{/ifnot}{/when}", "{when:Something}{text:VarPath}{/when}", "{when:Something}{html:VarPath}{/when}", "{when:Something}{url:VarPath}{/when}", "{when:Something}{eq:@Const}{/eq}{each:Collection}{/each}{else}{/else}{/when}", "{when:Something}{when:Foo}{eq:@Const}test{/eq}{/when}{/when}", "{when:Something}test{/when}", "{eq:@Const}test{/eq}", "{begins:@Const}test{/begins}", "{ends:@Const}test{/ends}", "{contains:@Const}test{/contains}", "{else}test{/else}" }; RuntimeCompiler compiler = new RuntimeCompiler(new CompilerOptions()); foreach (String template in errorTemplates) { try { compiler.Compile <Object>(template); throw new AssertionException(String.Format("Syntax exception was expected, but it was not thrown. Template: \"{0}\".", template)); } catch (SyntaxException) { } } }
public void RC_HtmlToken() { var model = new TextTokenTestModel(); var cultureArr = new[] { CultureInfo.InvariantCulture, CultureInfo.GetCultureInfo(1049) }; foreach (CultureInfo culture in cultureArr) { RuntimeCompiler compiler = new RuntimeCompiler(new CompilerOptions()); Assert.AreEqual(model.TextProperty, compiler.Compile(model.GetType(), "{html:TextProperty}", OutputFormat.Text).DynamicInvoke(model, culture)); Assert.AreEqual(model.HtmlProperty, compiler.Compile(model.GetType(), "{html:HtmlProperty}", OutputFormat.Text).DynamicInvoke(model, culture)); Assert.AreEqual(model.HtmlProperty, compiler.Compile(model.GetType(), "{html:HtmlProperty}", OutputFormat.Html).DynamicInvoke(model, culture)); Assert.AreEqual(String.Format(culture, "{0}", model.IntProperty), compiler.Compile(model.GetType(), "{html:IntProperty}", OutputFormat.Text).DynamicInvoke(model, culture)); Assert.AreEqual(String.Format(culture, "{0:0000.00}", model.IntProperty), compiler.Compile(model.GetType(), "{html:IntProperty:0000.00}", OutputFormat.Text).DynamicInvoke(model, culture)); Assert.AreEqual(String.Format(culture, "{0}", model.FloatProperty), compiler.Compile(model.GetType(), "{html:FloatProperty}", OutputFormat.Text).DynamicInvoke(model, culture)); Assert.AreEqual(String.Format(culture, "{0:0.##}", model.FloatProperty), compiler.Compile(model.GetType(), "{html:FloatProperty:0.##}", OutputFormat.Text).DynamicInvoke(model, culture)); Assert.AreEqual(String.Format(culture, "{0:dd.MM.yyyy}", model.DateTimeProperty), compiler.Compile(model.GetType(), "{html:DateTimeProperty:dd.MM.yyyy}", OutputFormat.Text).DynamicInvoke(model, culture)); Assert.AreEqual(String.Format(culture, "{0:dd.MM.yyyy HH:mm:ss}", model.DateTimeProperty), compiler.Compile(model.GetType(), "{html:DateTimeProperty:dd.MM.yyyy hh:mm:ss}", OutputFormat.Text).DynamicInvoke(model, culture)); } }
public void RC_Order() { OrderTestModel model = new OrderTestModel(); RuntimeCompiler compiler = new RuntimeCompiler(new CompilerOptions()); var cultureArr = new[] { CultureInfo.InvariantCulture, CultureInfo.GetCultureInfo(1049) }; foreach (CultureInfo culture in cultureArr) { String templateString = @" Dear {text:Customer} Your order: {each:OrderList}{text:thisNum}. {text:Title} x {text:Quantity} : {text:Total:0.##$} {/each} {if:Discount}Order total without discount: {text:OrderTotal:0.##$} Discount: {text:Discount:0'%'}{/if}{ifnot:Discount}You should order more products to get discount.{/ifnot} Total: {text:OrderTotalWithDiscount:0.##$} "; StringBuilder expectedMessage = new StringBuilder(); expectedMessage.AppendFormat(culture, "\r\nDear {0}\r\n\r\nYour order:\r\n", model.Customer); Int32 itemNum = 0; foreach (OrderItem item in model.OrderList) { expectedMessage.AppendFormat(culture, "{0}. {1} x {2} : {3:0.##$}\r\n", ++itemNum, item.Title, item.Quantity, item.Total); } if (model.Discount != 0.0) { expectedMessage.AppendFormat(culture, "\r\nOrder total without discount: {0:0.##$}\r\n", model.OrderTotal); expectedMessage.AppendFormat(culture, "Discount: {0:0'%'}\r\n", model.Discount); } else { expectedMessage.AppendFormat(culture, "\r\nYou should order more products to get discount.\r\n"); } expectedMessage.AppendFormat(culture, "Total: {0:0.##$}\r\n", model.OrderTotalWithDiscount); Assert.AreEqual(expectedMessage.ToString(), compiler.Compile(model.GetType(), templateString, OutputFormat.Text).DynamicInvoke(model, culture)); } }
private Type CompileAndGetType() { compiler = RuntimeCompiler.Compile(translation, Path.GetDirectoryName(OpenFileDialog.FileName)); if (compiler == null) { MessageBox.Show("Unnable to compile", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (compiler.Errors.Count > 0) { string errors = "Compile error:\r\n"; for (int i = 0; i < compiler.Errors.Count; i++) { errors += compiler.Errors[i].ErrorText; } } Type type = RuntimeCompiler.GetCompiledClass(compiler); compileCounter++; return(type); }
string Compile(string name, string src) { RuntimeCompiler compiler = new RuntimeCompiler(); compiler.AddOptions( RuntimeCompiler.OPTION_TARGET_20 //RuntimeCompiler.OPTION_FMAD_FALSE, //RuntimeCompiler.OPTION_LINE_INFO, //RuntimeCompiler.OPTION_DEVICE_AS_DEFAULT_EXECUTION_SPACE ); string ptx = compiler.Compile(name, src); if (ptx == null) { Console.WriteLine("Compile Error:"); Console.WriteLine(); Console.WriteLine(compiler.Log); return(null); } return(ptx); }
public static int Compile() { var randomizer = new SourceRandomizer(Seed); var compiler = new RuntimeCompiler(); var primaryColorValue = new Random(Environment.TickCount).Next(10, 14 + 1); var secondaryColorValue = primaryColorValue - 8; Console.ForegroundColor = (ConsoleColor)primaryColorValue; Out($"-=: SharpLoader v{Version}"); Console.ForegroundColor = (ConsoleColor)secondaryColorValue; Out($"-=: Created by {Author}"); Console.ForegroundColor = ConsoleColor.DarkGray; Out($"-=: Seed : {Seed}"); Out(""); // Data file not found if (!File.Exists(ConfigPath)) { Console.ForegroundColor = ConsoleColor.Red; Out($"-=: Config file not found ({ConfigFileName})"); return(1); } Console.ForegroundColor = ConsoleColor.White; Out("-=: Reading..."); var userReferencesReadSb = new StringBuilder(ReadBufferSize); var baseDirectoryReadSb = new StringBuilder(ReadBufferSize); var sourceFilesReadSb = new StringBuilder(ReadBufferSize); var outputNameReadSb = new StringBuilder(ReadBufferSize); var compilerArgumentsReadSb = new StringBuilder(ReadBufferSize); var autoRunReadSb = new StringBuilder(ReadBufferSize); WinApi.GetPrivateProfileString("SharpLoader", "References", string.Empty, userReferencesReadSb, ReadBufferSize, ConfigPath); WinApi.GetPrivateProfileString("SharpLoader", "Directory", string.Empty, baseDirectoryReadSb, ReadBufferSize, ConfigPath); WinApi.GetPrivateProfileString("SharpLoader", "Sources", string.Empty, sourceFilesReadSb, ReadBufferSize, ConfigPath); WinApi.GetPrivateProfileString("SharpLoader", "Output", string.Empty, outputNameReadSb, ReadBufferSize, ConfigPath); WinApi.GetPrivateProfileString("SharpLoader", "Arguments", string.Empty, compilerArgumentsReadSb, ReadBufferSize, ConfigPath); WinApi.GetPrivateProfileString("SharpLoader", "AutoRun", string.Empty, autoRunReadSb, ReadBufferSize, ConfigPath); var userReferences = userReferencesReadSb.ToString().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); var baseDirectory = baseDirectoryReadSb.ToString(); string[] userSourcePaths; var outputName = $"{outputNameReadSb}-{DateTime.Now:dd-MM-yyyy}.exe"; var compilerArguments = compilerArgumentsReadSb.ToString(); var autoRun = autoRunReadSb.ToString().Equals("true", StringComparison.OrdinalIgnoreCase); // Drag n Drop if (DragDropPaths.Count != 0) { userSourcePaths = DragDropPaths.ToArray(); } // Read from config else { userSourcePaths = sourceFilesReadSb.ToString().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); } // Check values if (userReferences.Length == 0) { Console.ForegroundColor = ConsoleColor.Red; Out($"-=: References are not given"); return(2); } if (string.IsNullOrWhiteSpace(outputName)) { Console.ForegroundColor = ConsoleColor.Red; Out($"-=: Output name is not given"); return(2); } outputName = Path.Combine(SaveDir, outputName); // Read sources var userSourceFiles = new List <string>(); // Add from config foreach (var path in userSourcePaths) { if (!File.Exists(path)) { Console.ForegroundColor = ConsoleColor.Red; Out($"-=: Source file not found ({path})"); return(5); } if (path.EndsWith(".cs")) { userSourceFiles.Add(File.ReadAllText(path)); } } // Add from directory if (!string.IsNullOrWhiteSpace(baseDirectory)) { userSourceFiles.AddRange(from path in GetFilesFromDirectory(baseDirectory) where path.EndsWith(".cs") select File.ReadAllText(path)); } if (userSourceFiles.Count == 0) { Console.ForegroundColor = ConsoleColor.Red; Out($"-=: Source files not found"); return(2); } Console.ForegroundColor = ConsoleColor.White; Out("-=: Randomizing..."); // Randomize for (var i = 0; i < userSourceFiles.Count; i++) { var tmp = userSourceFiles[i]; randomizer.Randomize(ref tmp); userSourceFiles[i] = tmp; } for (var i = 0; i < randomizer.InjectSources.Count; i++) { var tmp = randomizer.InjectSources[i]; randomizer.Randomize(ref tmp); randomizer.InjectSources[i] = tmp; } // Inject sources var compileSourceFiles = userSourceFiles.ToList(); compileSourceFiles.AddRange(randomizer.InjectSources); // Inject bytes if (randomizer.InjectBytes.Count > 0) { var output = new StringBuilder("new byte[] {"); foreach (var b in randomizer.InjectBytes) { output.Append($"{b},"); } output.Remove(output.Length - 1, 1); output.Append("}"); compileSourceFiles.Add($"namespace {randomizer.InjectBytesNamespace}" + $"{{" + $"public static class {randomizer.InjectBytesClass}" + $"{{" + $"public static byte[] {randomizer.InjectBytesProperty} = {output};" + $"}}" + $"}}"); } // Inject bools if (randomizer.InjectBools.Count > 0) { var output = new StringBuilder("new bool[] {"); foreach (var b in randomizer.InjectBools) { output.Append($"{b.ToString().ToLower()},"); } output.Remove(output.Length - 1, 1); output.Append("}"); compileSourceFiles.Add($"namespace {randomizer.InjectBoolsNamespace}" + $"{{" + $"public static class {randomizer.InjectBoolsClass}" + $"{{" + $"public static bool[] {randomizer.InjectBoolsProperty} = {output};" + $"}}" + $"}}"); } // Inject ints if (randomizer.InjectInts.Count > 0) { var output = new StringBuilder("new int[] {"); foreach (var b in randomizer.InjectInts) { output.Append($"{b},"); } output.Remove(output.Length - 1, 1); output.Append("}"); compileSourceFiles.Add($"namespace {randomizer.InjectIntsNamespace}" + $"{{" + $"public static class {randomizer.InjectIntsClass}" + $"{{" + $"public static int[] {randomizer.InjectIntsProperty} = {output};" + $"}}" + $"}}"); } // Inject strings if (randomizer.InjectStrings.Count > 0) { var output = new StringBuilder("new string[] {"); foreach (var b in randomizer.InjectStrings) { output.Append($"\"{b}\","); } output.Remove(output.Length - 1, 1); output.Append("}"); compileSourceFiles.Add($"namespace {randomizer.InjectStringsNamespace}" + $"{{" + $"public static class {randomizer.InjectStringsClass}" + $"{{" + $"public static string[] {randomizer.InjectStringsProperty} = {output};" + $"}}" + $"}}"); } // Inject references var compileReferences = userReferences.ToList(); foreach (var t in randomizer.InjectReferences) { if (compileReferences.All(a => a != t)) { compileReferences.Add(t); } } Console.ForegroundColor = ConsoleColor.White; Out("-=: Compiling..."); if (File.Exists(outputName)) { try { File.Delete(outputName); } catch (UnauthorizedAccessException) { Console.ForegroundColor = ConsoleColor.Red; Out($"-=: File in use"); return(6); } } if (!compiler.Compile( outputName, compilerArguments, compileReferences.ToArray(), compileSourceFiles.ToArray())) { return(4); } Console.ForegroundColor = ConsoleColor.Green; Out($"-=: Done [{Path.GetFileName(outputName)}] {(_outToConsole ? "(press any key to exit)" : string.Empty)}"); var sourceBytes = new List <byte>(); foreach (var s in compileSourceFiles) { sourceBytes.AddRange(Encoding.Default.GetBytes(s)); } var sourceHash = MD5.Create().ComputeHash(sourceBytes.ToArray()); Console.ForegroundColor = ConsoleColor.Yellow; Out($"-=: Hash [{Hash = ByteArrayToString(sourceHash)}]"); if (autoRun && File.Exists(outputName)) { Out("-=: Starting..."); Process.Start(outputName); } return(0); }
public void RC_ModelAccess() { ModelAccessTestModel model = new ModelAccessTestModel(); RuntimeCompiler compiler = new RuntimeCompiler(new CompilerOptions()); Assert.AreEqual(model.StrFld, compiler.Compile <ModelAccessTestModel>("{text:StrFld}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.IntFld.ToString(), compiler.Compile <ModelAccessTestModel>("{text:IntFld}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.NullableIntFld.ToString(), compiler.Compile <ModelAccessTestModel>("{text:NullableIntFld}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.NullableIntFld.HasValue.ToString(), compiler.Compile <ModelAccessTestModel>("{text:NullableIntFld.HasValue}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.NullableIntFldNull.ToString(), compiler.Compile <ModelAccessTestModel>("{text:NullableIntFldNull}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.NullableIntFldNull.HasValue.ToString(), compiler.Compile <ModelAccessTestModel>("{text:NullableIntFldNull.HasValue}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.StrProp, compiler.Compile <ModelAccessTestModel>("{text:StrProp}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.IntProp.ToString(), compiler.Compile <ModelAccessTestModel>("{text:IntProp}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.NullableIntProp.ToString(), compiler.Compile <ModelAccessTestModel>("{text:NullableIntProp}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.NullableIntProp.HasValue.ToString(), compiler.Compile <ModelAccessTestModel>("{text:NullableIntProp.HasValue}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.NullableIntPropNull.ToString(), compiler.Compile <ModelAccessTestModel>("{text:NullableIntPropNull}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.NullableIntPropNull.HasValue.ToString(), compiler.Compile <ModelAccessTestModel>("{text:NullableIntPropNull.HasValue}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.RefProp.StrFld, compiler.Compile <ModelAccessTestModel>("{text:RefProp.StrFld}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.RefProp.IntFld.ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefProp.IntFld}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.RefProp.NullableIntFld.ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefProp.NullableIntFld}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.RefProp.NullableIntFld.HasValue.ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefProp.NullableIntFld.HasValue}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.RefProp.NullableIntFldNull.ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefProp.NullableIntFldNull}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.RefProp.NullableIntFldNull.HasValue.ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefProp.NullableIntFldNull.HasValue}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.RefProp.StrProp, compiler.Compile <ModelAccessTestModel>("{text:RefProp.StrProp}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.RefProp.IntProp.ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefProp.IntProp}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.RefProp.NullableIntProp.ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefProp.NullableIntProp}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.RefProp.NullableIntPropNull.ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefProp.NullableIntPropNull}", OutputFormat.Text)(model, null)); Assert.AreEqual("", compiler.Compile <ModelAccessTestModel>("{text:RefPropNull.StrFld}", OutputFormat.Text)(model, null)); Assert.AreEqual(default(Int32).ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefPropNull.IntFld}", OutputFormat.Text)(model, null)); Assert.AreEqual(default(Int32?).ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefPropNull.NullableIntFld}", OutputFormat.Text)(model, null)); Assert.AreEqual(default(Boolean).ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefPropNull.NullableIntFld.HasValue}", OutputFormat.Text)(model, null)); Assert.AreEqual(default(Int32?).ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefPropNull.NullableIntFldNull}", OutputFormat.Text)(model, null)); Assert.AreEqual(default(Boolean).ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefPropNull.NullableIntFldNull.HasValue}", OutputFormat.Text)(model, null)); Assert.AreEqual("", compiler.Compile <ModelAccessTestModel>("{text:RefPropNull.StrProp}", OutputFormat.Text)(model, null)); Assert.AreEqual(default(Int32).ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefPropNull.IntProp}", OutputFormat.Text)(model, null)); Assert.AreEqual(default(Int32?).ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefPropNull.NullableIntProp}", OutputFormat.Text)(model, null)); Assert.AreEqual(default(Boolean).ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefPropNull.NullableIntProp.HasValue}", OutputFormat.Text)(model, null)); Assert.AreEqual(default(Int32?).ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefPropNull.NullableIntPropNull}", OutputFormat.Text)(model, null)); Assert.AreEqual(default(Boolean).ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefPropNull.NullableIntPropNull.HasValue}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.StrArrFld[1], compiler.Compile <ModelAccessTestModel>("{text:StrArrFld[1]}", OutputFormat.Text)(model, null)); Assert.AreEqual("", compiler.Compile <ModelAccessTestModel>("{text:StrArrFldNull[1]}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.StrArrProp[1], compiler.Compile <ModelAccessTestModel>("{text:StrArrProp[1]}", OutputFormat.Text)(model, null)); Assert.AreEqual("", compiler.Compile <ModelAccessTestModel>("{text:StrArrPropNull[1]}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.StrArrFld.Length.ToString(), compiler.Compile <ModelAccessTestModel>("{text:StrArrFld.Length}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.IntArrFld[1].ToString(), compiler.Compile <ModelAccessTestModel>("{text:IntArrFld[1]}", OutputFormat.Text)(model, null)); Assert.AreEqual(default(Int32).ToString(), compiler.Compile <ModelAccessTestModel>("{text:IntArrFldNull[1]}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.IntArrProp[1].ToString(), compiler.Compile <ModelAccessTestModel>("{text:IntArrProp[1]}", OutputFormat.Text)(model, null)); Assert.AreEqual(default(Int32).ToString(), compiler.Compile <ModelAccessTestModel>("{text:IntArrPropNull[1]}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.RefArrProp[2].StrValue, compiler.Compile <ModelAccessTestModel>("{text:RefArrProp[2].StrValue}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.RefArrProp[0].IntValue.ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefArrProp[0].IntValue}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.ValArrProp[2].StrValue, compiler.Compile <ModelAccessTestModel>("{text:ValArrProp[2].StrValue}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.ValArrProp[0].IntValue.ToString(), compiler.Compile <ModelAccessTestModel>("{text:ValArrProp[0].IntValue}", OutputFormat.Text)(model, null)); Assert.AreEqual(String.Join(";", model.StrArrFld) + ";", compiler.Compile <ModelAccessTestModel>("{each:StrArrFld}{text:this};{/each}", OutputFormat.Text)(model, null)); Assert.AreEqual(String.Join(";", model.StrArrProp) + ";", compiler.Compile <ModelAccessTestModel>("{each:StrArrProp}{text:this};{/each}", OutputFormat.Text)(model, null)); Assert.AreEqual(String.Join(";", Enumerable.Range(0, model.StrArrFld.Length).Select(n => n.ToString()).ToArray()) + ";", compiler.Compile <ModelAccessTestModel>("{each:StrArrFld}{text:thisIndex};{/each}", OutputFormat.Text)(model, null)); Assert.AreEqual(String.Join(";", Enumerable.Range(0, model.StrArrProp.Length).Select(n => n.ToString()).ToArray()) + ";", compiler.Compile <ModelAccessTestModel>("{each:StrArrProp}{text:thisIndex};{/each}", OutputFormat.Text)(model, null)); Assert.AreEqual(String.Join(";", Enumerable.Range(1, model.StrArrFld.Length).Select(n => n.ToString()).ToArray()) + ";", compiler.Compile <ModelAccessTestModel>("{each:StrArrFld}{text:thisNum};{/each}", OutputFormat.Text)(model, null)); Assert.AreEqual(String.Join(";", Enumerable.Range(1, model.StrArrProp.Length).Select(n => n.ToString()).ToArray()) + ";", compiler.Compile <ModelAccessTestModel>("{each:StrArrProp}{text:thisNum};{/each}", OutputFormat.Text)(model, null)); Assert.AreEqual(String.Join(";", Enumerable.Range(0, model.StrArrFld.Length).Select(n => model.StrArrFld.Length.ToString()).ToArray()) + ";", compiler.Compile <ModelAccessTestModel>("{each:StrArrFld}{text:thisCount};{/each}", OutputFormat.Text)(model, null)); Assert.AreEqual(String.Join(";", Enumerable.Range(0, model.StrArrProp.Length).Select(n => model.StrArrProp.Length.ToString()).ToArray()) + ";", compiler.Compile <ModelAccessTestModel>("{each:StrArrProp}{text:thisCount};{/each}", OutputFormat.Text)(model, null)); Assert.AreEqual(String.Join(";", model.RefArrProp.Select(e => e.StrValue + "+" + e.IntValue).ToArray()) + ";", compiler.Compile <ModelAccessTestModel>("{each:RefArrProp}{text:StrValue}+{text:IntValue};{/each}", OutputFormat.Text)(model, null)); Assert.AreEqual(String.Join(";", model.ValArrProp.Select(e => e.StrValue + "+" + e.IntValue).ToArray()) + ";", compiler.Compile <ModelAccessTestModel>("{each:ValArrProp}{text:StrValue}+{text:IntValue};{/each}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.RefListProp[2].StrValue, compiler.Compile <ModelAccessTestModel>("{text:RefListProp[2].StrValue}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.RefListProp[0].IntValue.ToString(), compiler.Compile <ModelAccessTestModel>("{text:RefListProp[0].IntValue}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.ValListProp[2].StrValue, compiler.Compile <ModelAccessTestModel>("{text:ValListProp[2].StrValue}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.ValListProp[0].IntValue.ToString(), compiler.Compile <ModelAccessTestModel>("{text:ValListProp[0].IntValue}", OutputFormat.Text)(model, null)); Assert.AreEqual(String.Join(";", model.RefListProp.Select(e => e.StrValue + "+" + e.IntValue).ToArray()) + ";", compiler.Compile <ModelAccessTestModel>("{each:RefListProp}{text:StrValue}+{text:IntValue};{/each}", OutputFormat.Text)(model, null)); Assert.AreEqual(String.Join(";", model.ValListProp.Select(e => e.StrValue + "+" + e.IntValue).ToArray()) + ";", compiler.Compile <ModelAccessTestModel>("{each:ValListProp}{text:StrValue}+{text:IntValue};{/each}", OutputFormat.Text)(model, null)); Assert.AreEqual(String.Join(";", model.RefEnumProp.Select(e => e.StrValue + "+" + e.IntValue).ToArray()) + ";", compiler.Compile <ModelAccessTestModel>("{each:RefEnumProp}{text:StrValue}+{text:IntValue};{/each}", OutputFormat.Text)(model, null)); Assert.AreEqual(String.Join(";", model.ValEnumProp.Select(e => e.StrValue + "+" + e.IntValue).ToArray()) + ";", compiler.Compile <ModelAccessTestModel>("{each:ValEnumProp}{text:StrValue}+{text:IntValue};{/each}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.IndexedRefProp["Test"], compiler.Compile <ModelAccessTestModel>("{text:IndexedRefProp[\"Test\"]}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.IndexedRefProp[3].ToString(), compiler.Compile <ModelAccessTestModel>("{text:IndexedRefProp[3]}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.IndexedValProp["tseT"], compiler.Compile <ModelAccessTestModel>("{text:IndexedValProp[\"tseT\"]}", OutputFormat.Text)(model, null)); Assert.AreEqual(model.IndexedValProp[7].ToString(), compiler.Compile <ModelAccessTestModel>("{text:IndexedValProp[7]}", OutputFormat.Text)(model, null)); }
public void RC_IfNotToken() { ConditionalTokenTestModel model = new ConditionalTokenTestModel(); RuntimeCompiler compiler = new RuntimeCompiler(new CompilerOptions()); Assert.AreEqual("", compiler.Compile(model.GetType(), "{ifnot:Bool_True}condition text{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("", compiler.Compile(model.GetType(), "{ifnot:Int32_True}condition text{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("", compiler.Compile(model.GetType(), "{ifnot:UInt32_True}condition text{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("", compiler.Compile(model.GetType(), "{ifnot:Int16_True}condition text{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("", compiler.Compile(model.GetType(), "{ifnot:UInt16_True}condition text{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("", compiler.Compile(model.GetType(), "{ifnot:Int64_True}condition text{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("", compiler.Compile(model.GetType(), "{ifnot:UInt64_True}condition text{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("", compiler.Compile(model.GetType(), "{ifnot:String_True}condition text{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("", compiler.Compile(model.GetType(), "{ifnot:Object_True}condition text{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("condition text", compiler.Compile(model.GetType(), "{ifnot:Bool_False}condition text{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("condition text", compiler.Compile(model.GetType(), "{ifnot:Int32_False}condition text{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("condition text", compiler.Compile(model.GetType(), "{ifnot:UInt32_False}condition text{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("condition text", compiler.Compile(model.GetType(), "{ifnot:Int16_False}condition text{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("condition text", compiler.Compile(model.GetType(), "{ifnot:UInt16_False}condition text{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("condition text", compiler.Compile(model.GetType(), "{ifnot:Int64_False}condition text{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("condition text", compiler.Compile(model.GetType(), "{ifnot:UInt64_False}condition text{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("condition text", compiler.Compile(model.GetType(), "{ifnot:String_False}condition text{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("condition text", compiler.Compile(model.GetType(), "{ifnot:Object_False}condition text{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("[Level1][Level2][/Level2][/Level1]", compiler.Compile(model.GetType(), "{ifnot:Object_False}[Level1]{ifnot:Int32_False}[Level2][/Level2]{/ifnot}[/Level1]{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("[Level1][/Level1]", compiler.Compile(model.GetType(), "{ifnot:Object_False}[Level1]{ifnot:Int32_True}[Level2][/Level2]{/ifnot}[/Level1]{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("", compiler.Compile(model.GetType(), "{ifnot:Object_True}[Level1]{ifnot:Int32_False}[Level2][/Level2]{/ifnot}[/Level1]{/ifnot}", OutputFormat.Text).DynamicInvoke(model, null)); }
public void RC_UrlToken() { UrlTokenTestModel model = new UrlTokenTestModel(); RuntimeCompiler compiler = new RuntimeCompiler(new CompilerOptions()); Assert.AreEqual("%22english%20text%2C%20%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9%20%D1%82%D0%B5%D0%BA%D1%81%D1%82%22", (String)compiler.Compile(model.GetType(), "{url/p:TextProperty}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual("%22english%20text,%20%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9%20%D1%82%D0%B5%D0%BA%D1%81%D1%82%22", (String)compiler.Compile(model.GetType(), "{url:TextProperty}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual(EncodeUtility.UrlEncodeAsUrl(model.UrlProperty, false), (String)compiler.Compile(model.GetType(), "{url:UrlProperty}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual(EncodeUtility.UrlEncode(model.UrlProperty, false), (String)compiler.Compile(model.GetType(), "{url/p:UrlProperty}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual(EncodeUtility.UrlEncodeAsUrl(model.UrlProperty, true), (String)compiler.Compile(model.GetType(), "{url/a:UrlProperty}", OutputFormat.Text).DynamicInvoke(model, null)); Assert.AreEqual(EncodeUtility.HtmlEncode(EncodeUtility.UrlEncodeAsUrl(model.UrlProperty, false)), (String)compiler.Compile(model.GetType(), "{url:UrlProperty}", OutputFormat.Html).DynamicInvoke(model, null)); Assert.AreEqual(EncodeUtility.HtmlEncode(EncodeUtility.UrlEncodeAsUrl(model.UrlProperty, true)), (String)compiler.Compile(model.GetType(), "{url/a:UrlProperty}", OutputFormat.Html).DynamicInvoke(model, null)); }
private void TestCompile() { // プログラムのコンパイル (cu から PTX へ) RuntimeCompiler compiler = new RuntimeCompiler(); //compiler.AddHeader("curand_kernel.h", @"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include\curand_kernel.h"); compiler.AddOptions( RuntimeCompiler.OPTION_TARGET_30, RuntimeCompiler.OPTION_FMAD_FALSE, RuntimeCompiler.OPTION_LINE_INFO, RuntimeCompiler.OPTION_DEVICE_AS_DEFAULT_EXECUTION_SPACE //RuntimeCompiler.OPTION_INCLUDE_PATH_ + @"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include\" ); string ptx = compiler.Compile("addKernel.cu", addKernelString); if (ptx == null) { Console.WriteLine("Compile Error:"); Console.WriteLine(); Console.WriteLine(compiler.Log); return; } // コンパイル時のログを出力画面に表示 Console.WriteLine("----- <Compile Log>"); Console.WriteLine(compiler.Log); Console.WriteLine("----- </Compile Log>"); // コンパイル済みプログラムを出力画面に表示 Console.WriteLine("----- <PTX>"); Console.WriteLine(ptx); Console.WriteLine("----- </PTX>"); // プログラムの実行準備 Device device = new Device(0); Context context = device.CreateContext(); Module module = new Module(); Console.WriteLine(device.Name); Console.WriteLine(device.PCIBusId); Console.WriteLine(device.TotalMem); //Console.WriteLine(device.GetProperties()); Console.WriteLine(context.ApiVersion); //return; // PTX データをロード module.LoadData(ptx); // いったんメインメモリ上に変数を準備 const int arraySize = 5; List <int> a = new List <int>(); List <int> b = new List <int>(); for (int i = 0; i < arraySize; i++) { a.Add(i + 1); b.Add((i + 1) * 10); } // デバイス上にメモリを転送 DeviceMemory memory = new DeviceMemory(); memory.Add <int>("a", a); memory.Add <int>("b", b); memory.Alloc <int>("c", arraySize); // 関数の実行 module.SetBlockCount(1, 1, 1); module.SetThreadCount(arraySize, 1, 1); module.Excecute( "addKernel", memory["c"], memory["a"], memory["b"] ); // 全てのスレッドが終了するまで待つ context.Synchronize(); // 結果を取得して出力画面に表示 int[] results = memory.Read <int>("c", arraySize); Console.WriteLine("----- <Execute Log>"); for (int i = 0; i < arraySize; i++) { Console.WriteLine("{0} + {1} = {2}", a[i], b[i], results[i]); } Console.WriteLine("----- </Execute Log>"); // リソースを解放する memory.Dispose(); module.Dispose(); context.Dispose(); }