Skip to content

Commit

Permalink
revert(examples): the ones we accidentally removed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeioth committed Jun 9, 2024
1 parent 97b1e0b commit cedc205
Show file tree
Hide file tree
Showing 29 changed files with 265 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/code samples/languages/asm/build/helper.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
section .data
hello db 'Hello, World!',0

section .text
global print_hello

print_hello:
; Write the message to stdout
mov eax, 1 ; Syscall number for write
mov edi, 1 ; File descriptor 1: stdout
mov rsi, hello ; Address of the string
mov edx, 13 ; Length of the string
syscall

ret

17 changes: 17 additions & 0 deletions tests/code samples/languages/asm/build/main.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
section .data
extern hello

section .text
extern print_hello

global _start

_start:
; Call the print_hello function
call print_hello

; Exit the program
mov eax, 60 ; Syscall number for exit
xor edi, edi ; Exit status 0
syscall

6 changes: 6 additions & 0 deletions tests/code samples/languages/c/build/helper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

void printHello() {
printf("Hello, World!\n");
}

7 changes: 7 additions & 0 deletions tests/code samples/languages/c/build/helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef HELPER_H
#define HELPER_H

void printHello();

#endif

7 changes: 7 additions & 0 deletions tests/code samples/languages/c/build/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdio.h>
#include "helper.h"

int main() {
printHello();
return 0;
}
6 changes: 6 additions & 0 deletions tests/code samples/languages/cpp/build/helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <iostream>

void printHello() {
std::cout << "Hello, World!" << std::endl;
}

7 changes: 7 additions & 0 deletions tests/code samples/languages/cpp/build/helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef HELPER_H
#define HELPER_H

void printHello();

#endif

8 changes: 8 additions & 0 deletions tests/code samples/languages/cpp/build/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <iostream>
#include "helper.h"

int main() {
printHello();
return 0;
}

10 changes: 10 additions & 0 deletions tests/code samples/languages/cs/build/Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

class Helper
{
public static void PrintHello()
{
Console.WriteLine("Hello, World!");
}
}

9 changes: 9 additions & 0 deletions tests/code samples/languages/cs/build/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

class Program
{
static void Main()
{
Helper.PrintHello();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
void printGreeting() {
print("Hello, World!");
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'helper.dart';

void main() {
printGreeting();
}
3 changes: 3 additions & 0 deletions tests/code samples/languages/go/build/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module main

go 1.22.3
8 changes: 8 additions & 0 deletions tests/code samples/languages/go/build/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// helper.go

package main

func getMessage() string {
return "Hello, World!"
}

11 changes: 11 additions & 0 deletions tests/code samples/languages/go/build/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// main.go

package main

import "fmt"

func main() {
msg := getMessage() // Calling the function from "helper.go"
fmt.Println(msg)
}

5 changes: 5 additions & 0 deletions tests/code samples/languages/java/build/Helper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class Helper {
public static void printHello() {
System.out.println("Hello, World!");
}
}
6 changes: 6 additions & 0 deletions tests/code samples/languages/java/build/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class Main {
public static void main(String[] args) {
Helper.printHello();
}
}

6 changes: 6 additions & 0 deletions tests/code samples/languages/kotlin/build/Helper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object Helper {
fun printMessage() {
println("Hello from helper.kt!")
}
}

3 changes: 3 additions & 0 deletions tests/code samples/languages/kotlin/build/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fun main() {
Helper.printMessage()
}
2 changes: 2 additions & 0 deletions tests/code samples/languages/python/bytecode/build/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def hello_world():
print('Hello, World!')
4 changes: 4 additions & 0 deletions tests/code samples/languages/python/bytecode/build/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import helper

helper.hello_world()

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def hello_world():
print('Hello, World!')
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import helper

helper.hello_world()

4 changes: 4 additions & 0 deletions tests/code samples/languages/rust/build/helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub fn print_hello() {
println!("Hello, World!");
}

6 changes: 6 additions & 0 deletions tests/code samples/languages/rust/build/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mod helper;

fn main() {
helper::print_hello();
}

3 changes: 3 additions & 0 deletions tests/code samples/languages/swift/build/helper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
func sayHello() {
print("Hello, World!")
}
2 changes: 2 additions & 0 deletions tests/code samples/languages/swift/build/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sayHello()

70 changes: 70 additions & 0 deletions tests/code samples/languages/zig/build/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const std = @import("std");

// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});

// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});

const exe = b.addExecutable(.{
.name = "build-and-run",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});

// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
b.installArtifact(exe);

// This *creates* a Run step in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
const run_cmd = b.addRunArtifact(exe);

// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
run_cmd.step.dependOn(b.getInstallStep());

// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
run_cmd.addArgs(args);
}

// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);

// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});

const run_unit_tests = b.addRunArtifact(unit_tests);

// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);
}
24 changes: 24 additions & 0 deletions tests/code samples/languages/zig/build/src/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const std = @import("std");

pub fn main() !void {
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});

// stdout is for the actual output of your application, for example if you
// are implementing gzip, then only the compressed bytes should be sent to
// stdout, not any debugging messages.
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();

try stdout.print("Run `zig build test` to run the tests.\n", .{});

try bw.flush(); // don't forget to flush!
}

test "simple test" {
var list = std.ArrayList(i32).init(std.testing.allocator);
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
try list.append(42);
try std.testing.expectEqual(@as(i32, 42), list.pop());
}

0 comments on commit cedc205

Please sign in to comment.