kazuyamaa/multi-language-messages-01 huggingface dataset:
Task: Variadic function
C: #include <stdio.h>
#include <stdarg.h>

void varstrings(int count, ...)   
{
    va_list args;
    va_start(args, count);
    while (count--)
        puts(va_arg(args, const char *));
    va_end(args);
}

varstrings(5, "Mary", "had", "a", "little", "lamb");

C++: #include <iostream>

template<typename T>
 void print(T const& t)
{
  std::cout << t;
}

template<typename First, typename ... Rest>
 void print(First const& first, Rest const& ... rest)
{
  std::cout << first;
  print(rest ...);
}

int main()
{
  int i = 10;
  std::string s = "Hello world";
  print("i = ", i, " and s = \"", s, "\"\n");
}
Task: Variadic function
C: #include <stdio.h>
#include <stdarg.h>

void varstrings(int count, ...)   
{
    va_list args;
    va_start(args, count);
    while (count--)
        puts(va_arg(args, const char *));
    va_end(args);
}

varstrings(5, "Mary", "had", "a", "little", "lamb");

Go: func printAll(things ... string) {
  
  for _, x := range things {
    fmt.Println(x)
  }
}
Task: Variadic function
C: #include <stdio.h>
#include <stdarg.h>

void varstrings(int count, ...)   
{
    va_list args;
    va_start(args, count);
    while (count--)
        puts(va_arg(args, const char *));
    va_end(args);
}

varstrings(5, "Mary", "had", "a", "little", "lamb");

Java: module VariadicFunction
    {
    void show(String[] strings)
        {
        @Inject Console console;
        strings.forEach(s -> console.print(s));
        }

    void run()
        {
        show(["hello", "world"]);

        String s1 = "not";
        String s2 = "a";
        String s3 = "constant";
        String s4 = "literal";
        show([s1, s2, s3, s4]);
        }
    }
Task: Variadic function
C: #include <stdio.h>
#include <stdarg.h>

void varstrings(int count, ...)   
{
    va_list args;
    va_start(args, count);
    while (count--)
        puts(va_arg(args, const char *));
    va_end(args);
}

varstrings(5, "Mary", "had", "a", "little", "lamb");

VB: Sub PrintArgs(ParamArray Args() As String)
  For i As Integer = 0 To Ubound(Args)
    Print(Args(i))
  Next
End Sub
Task: Variadic function
C#: using System;

class Program {
    static void Main(string[] args) {
        PrintAll("test", "rosetta code", 123, 5.6);
    }

    static void PrintAll(params object[] varargs) {
        foreach (var i in varargs) {
            Console.WriteLine(i);
        }
    }
}

C++: #include <iostream>

template<typename T>
 void print(T const& t)
{
  std::cout << t;
}

template<typename First, typename ... Rest>
 void print(First const& first, Rest const& ... rest)
{
  std::cout << first;
  print(rest ...);
}

int main()
{
  int i = 10;
  std::string s = "Hello world";
  print("i = ", i, " and s = \"", s, "\"\n");
}
Task: Variadic function
C++: #include <iostream>

template<typename T>
 void print(T const& t)
{
  std::cout << t;
}

template<typename First, typename ... Rest>
 void print(First const& first, Rest const& ... rest)
{
  std::cout << first;
  print(rest ...);
}

int main()
{
  int i = 10;
  std::string s = "Hello world";
  print("i = ", i, " and s = \"", s, "\"\n");
}

Go: func printAll(things ... string) {
  
  for _, x := range things {
    fmt.Println(x)
  }
}
Task: Variadic function
C++: #include <iostream>

template<typename T>
 void print(T const& t)
{
  std::cout << t;
}

template<typename First, typename ... Rest>
 void print(First const& first, Rest const& ... rest)
{
  std::cout << first;
  print(rest ...);
}

int main()
{
  int i = 10;
  std::string s = "Hello world";
  print("i = ", i, " and s = \"", s, "\"\n");
}

Java: module VariadicFunction
    {
    void show(String[] strings)
        {
        @Inject Console console;
        strings.forEach(s -> console.print(s));
        }

    void run()
        {
        show(["hello", "world"]);

        String s1 = "not";
        String s2 = "a";
        String s3 = "constant";
        String s4 = "literal";
        show([s1, s2, s3, s4]);
        }
    }
Task: Variadic function
C++: #include <iostream>

template<typename T>
 void print(T const& t)
{
  std::cout << t;
}

template<typename First, typename ... Rest>
 void print(First const& first, Rest const& ... rest)
{
  std::cout << first;
  print(rest ...);
}

int main()
{
  int i = 10;
  std::string s = "Hello world";
  print("i = ", i, " and s = \"", s, "\"\n");
}

Python: def print_all(*things):
    for x in things:
        print x
Task: Variadic function
C#: using System;

class Program {
    static void Main(string[] args) {
        PrintAll("test", "rosetta code", 123, 5.6);
    }

    static void PrintAll(params object[] varargs) {
        foreach (var i in varargs) {
            Console.WriteLine(i);
        }
    }
}

Go: func printAll(things ... string) {
  
  for _, x := range things {
    fmt.Println(x)
  }
}
Task: Variadic function
C#: using System;

class Program {
    static void Main(string[] args) {
        PrintAll("test", "rosetta code", 123, 5.6);
    }

    static void PrintAll(params object[] varargs) {
        foreach (var i in varargs) {
            Console.WriteLine(i);
        }
    }
}

Java: module VariadicFunction
    {
    void show(String[] strings)
        {
        @Inject Console console;
        strings.forEach(s -> console.print(s));
        }

    void run()
        {
        show(["hello", "world"]);

        String s1 = "not";
        String s2 = "a";
        String s3 = "constant";
        String s4 = "literal";
        show([s1, s2, s3, s4]);
        }
    }
Task: Variadic function
C#: using System;

class Program {
    static void Main(string[] args) {
        PrintAll("test", "rosetta code", 123, 5.6);
    }

    static void PrintAll(params object[] varargs) {
        foreach (var i in varargs) {
            Console.WriteLine(i);
        }
    }
}

Python: def print_all(*things):
    for x in things:
        print x
Task: Variadic function
C#: using System;

class Program {
    static void Main(string[] args) {
        PrintAll("test", "rosetta code", 123, 5.6);
    }

    static void PrintAll(params object[] varargs) {
        foreach (var i in varargs) {
            Console.WriteLine(i);
        }
    }
}

VB: Sub PrintArgs(ParamArray Args() As String)
  For i As Integer = 0 To Ubound(Args)
    Print(Args(i))
  Next
End Sub
Task: Variadic function
Go: func printAll(things ... string) {
  
  for _, x := range things {
    fmt.Println(x)
  }
}

Java: module VariadicFunction
    {
    void show(String[] strings)
        {
        @Inject Console console;
        strings.forEach(s -> console.print(s));
        }

    void run()
        {
        show(["hello", "world"]);

        String s1 = "not";
        String s2 = "a";
        String s3 = "constant";
        String s4 = "literal";
        show([s1, s2, s3, s4]);
        }
    }
Task: Variadic function
Java: module VariadicFunction
    {
    void show(String[] strings)
        {
        @Inject Console console;
        strings.forEach(s -> console.print(s));
        }

    void run()
        {
        show(["hello", "world"]);

        String s1 = "not";
        String s2 = "a";
        String s3 = "constant";
        String s4 = "literal";
        show([s1, s2, s3, s4]);
        }
    }

Python: def print_all(*things):
    for x in things:
        print x
Task: Variadic function
Java: module VariadicFunction
    {
    void show(String[] strings)
        {
        @Inject Console console;
        strings.forEach(s -> console.print(s));
        }

    void run()
        {
        show(["hello", "world"]);

        String s1 = "not";
        String s2 = "a";
        String s3 = "constant";
        String s4 = "literal";
        show([s1, s2, s3, s4]);
        }
    }

VB: Sub PrintArgs(ParamArray Args() As String)
  For i As Integer = 0 To Ubound(Args)
    Print(Args(i))
  Next
End Sub
Task: Variadic function
Go: func printAll(things ... string) {
  
  for _, x := range things {
    fmt.Println(x)
  }
}

Python: def print_all(*things):
    for x in things:
        print x
Task: Variadic function
Go: func printAll(things ... string) {
  
  for _, x := range things {
    fmt.Println(x)
  }
}

VB: Sub PrintArgs(ParamArray Args() As String)
  For i As Integer = 0 To Ubound(Args)
    Print(Args(i))
  Next
End Sub
Task: Variadic function
Python: def print_all(*things):
    for x in things:
        print x

VB: Sub PrintArgs(ParamArray Args() As String)
  For i As Integer = 0 To Ubound(Args)
    Print(Args(i))
  Next
End Sub
Task: Variadic function
C: #include <stdio.h>
#include <stdarg.h>

void varstrings(int count, ...)   
{
    va_list args;
    va_start(args, count);
    while (count--)
        puts(va_arg(args, const char *));
    va_end(args);
}

varstrings(5, "Mary", "had", "a", "little", "lamb");

Python: def print_all(*things):
    for x in things:
        print x
Task: Variadic function
C++: #include <iostream>

template<typename T>
 void print(T const& t)
{
  std::cout << t;
}

template<typename First, typename ... Rest>
 void print(First const& first, Rest const& ... rest)
{
  std::cout << first;
  print(rest ...);
}

int main()
{
  int i = 10;
  std::string s = "Hello world";
  print("i = ", i, " and s = \"", s, "\"\n");
}

VB: Sub PrintArgs(ParamArray Args() As String)
  For i As Integer = 0 To Ubound(Args)
    Print(Args(i))
  Next
End Sub
Task: Variadic function
C: #include <stdio.h>
#include <stdarg.h>

void varstrings(int count, ...)   
{
    va_list args;
    va_start(args, count);
    while (count--)
        puts(va_arg(args, const char *));
    va_end(args);
}

varstrings(5, "Mary", "had", "a", "little", "lamb");

PHP: <?php
function printAll() {
  foreach (func_get_args() as $x) // first way
    echo "$x\n";

  $numargs = func_num_args(); // second way
  for ($i = 0; $i < $numargs; $i++)
    echo func_get_arg($i), "\n";
}
printAll(4, 3, 5, 6, 4, 3);
printAll(4, 3, 5);
printAll("Rosetta", "Code", "Is", "Awesome!");
?>
Task: Variadic function
C#: using System;

class Program {
    static void Main(string[] args) {
        PrintAll("test", "rosetta code", 123, 5.6);
    }

    static void PrintAll(params object[] varargs) {
        foreach (var i in varargs) {
            Console.WriteLine(i);
        }
    }
}

PHP: <?php
function printAll() {
  foreach (func_get_args() as $x) // first way
    echo "$x\n";

  $numargs = func_num_args(); // second way
  for ($i = 0; $i < $numargs; $i++)
    echo func_get_arg($i), "\n";
}
printAll(4, 3, 5, 6, 4, 3);
printAll(4, 3, 5);
printAll("Rosetta", "Code", "Is", "Awesome!");
?>
Task: Variadic function
C++: #include <iostream>

template<typename T>
 void print(T const& t)
{
  std::cout << t;
}

template<typename First, typename ... Rest>
 void print(First const& first, Rest const& ... rest)
{
  std::cout << first;
  print(rest ...);
}

int main()
{
  int i = 10;
  std::string s = "Hello world";
  print("i = ", i, " and s = \"", s, "\"\n");
}

PHP: <?php
function printAll() {
  foreach (func_get_args() as $x) // first way
    echo "$x\n";

  $numargs = func_num_args(); // second way
  for ($i = 0; $i < $numargs; $i++)
    echo func_get_arg($i), "\n";
}
printAll(4, 3, 5, 6, 4, 3);
printAll(4, 3, 5);
printAll("Rosetta", "Code", "Is", "Awesome!");
?>
Task: Variadic function
Go: func printAll(things ... string) {
  
  for _, x := range things {
    fmt.Println(x)
  }
}

PHP: <?php
function printAll() {
  foreach (func_get_args() as $x) // first way
    echo "$x\n";

  $numargs = func_num_args(); // second way
  for ($i = 0; $i < $numargs; $i++)
    echo func_get_arg($i), "\n";
}
printAll(4, 3, 5, 6, 4, 3);
printAll(4, 3, 5);
printAll("Rosetta", "Code", "Is", "Awesome!");
?>
Task: Variadic function
Java: module VariadicFunction
    {
    void show(String[] strings)
        {
        @Inject Console console;
        strings.forEach(s -> console.print(s));
        }

    void run()
        {
        show(["hello", "world"]);

        String s1 = "not";
        String s2 = "a";
        String s3 = "constant";
        String s4 = "literal";
        show([s1, s2, s3, s4]);
        }
    }

PHP: <?php
function printAll() {
  foreach (func_get_args() as $x) // first way
    echo "$x\n";

  $numargs = func_num_args(); // second way
  for ($i = 0; $i < $numargs; $i++)
    echo func_get_arg($i), "\n";
}
printAll(4, 3, 5, 6, 4, 3);
printAll(4, 3, 5);
printAll("Rosetta", "Code", "Is", "Awesome!");
?>
Task: Variadic function
Python: def print_all(*things):
    for x in things:
        print x

PHP: <?php
function printAll() {
  foreach (func_get_args() as $x) // first way
    echo "$x\n";

  $numargs = func_num_args(); // second way
  for ($i = 0; $i < $numargs; $i++)
    echo func_get_arg($i), "\n";
}
printAll(4, 3, 5, 6, 4, 3);
printAll(4, 3, 5);
printAll("Rosetta", "Code", "Is", "Awesome!");
?>
Task: Variadic function
VB: Sub PrintArgs(ParamArray Args() As String)
  For i As Integer = 0 To Ubound(Args)
    Print(Args(i))
  Next
End Sub

PHP: <?php
function printAll() {
  foreach (func_get_args() as $x) // first way
    echo "$x\n";

  $numargs = func_num_args(); // second way
  for ($i = 0; $i < $numargs; $i++)
    echo func_get_arg($i), "\n";
}
printAll(4, 3, 5, 6, 4, 3);
printAll(4, 3, 5);
printAll("Rosetta", "Code", "Is", "Awesome!");
?>
Task: Call a function in a shared library
C: #include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

int myopenimage(const char *in)
{
  static int handle=0;
  fprintf(stderr, "internal openimage opens %s...\n", in);
  return handle++;
}

int main()
{
  void *imglib;
  int (*extopenimage)(const char *);
  int imghandle;

  imglib = dlopen("./fakeimglib.so", RTLD_LAZY);
  if ( imglib != NULL ) {
    
    *(void **)(&extopenimage) = dlsym(imglib, "openimage");
    
    
    imghandle = extopenimage("fake.img");
  } else {
    imghandle = myopenimage("fake.img");
  }
  printf("opened with handle %d\n", imghandle);
  
  if (imglib != NULL ) dlclose(imglib);
  return EXIT_SUCCESS;
}

C#: using System.Runtime.InteropServices;

class Program {
    [DllImport("fakelib.dll")]
    public static extern int fakefunction(int args);

    static void Main(string[] args) {
        int r = fakefunction(10);
    }
}
Task: Call a function in a shared library
C#: using System.Runtime.InteropServices;

class Program {
    [DllImport("fakelib.dll")]
    public static extern int fakefunction(int args);

    static void Main(string[] args) {
        int r = fakefunction(10);
    }
}

Go: package main


import "C"
import (
    "fmt"
    "os"
    "unsafe"
)

var handle = -1

func myOpenImage(s string) int {
    fmt.Fprintf(os.Stderr, "internal openImage opens %s...\n", s)
    handle++
    return handle
}

func main() {
    libpath := C.CString("./fakeimglib.so")
    defer C.free(unsafe.Pointer(libpath))
    imglib := C.dlopen(libpath, C.RTLD_LAZY)
    var imghandle int
    if imglib != nil {
        openimage := C.CString("openimage")
        defer C.free(unsafe.Pointer(openimage))
        fp := C.dlsym(imglib, openimage)
        if fp != nil {
            fi := C.CString("fake.img")
            defer C.free(unsafe.Pointer(fi))
            imghandle = int(C.bridge_someFunc(C.someFunc(fp), fi))

        } else {
            imghandle = myOpenImage("fake.img")
        }
        C.dlclose(imglib)
    } else {
        imghandle = myOpenImage("fake.img")
    }
    fmt.Printf("opened with handle %d\n", imghandle)
}
Task: Call a function in a shared library
C#: using System.Runtime.InteropServices;

class Program {
    [DllImport("fakelib.dll")]
    public static extern int fakefunction(int args);

    static void Main(string[] args) {
        int r = fakefunction(10);
    }
}

Python: import ctypes
  
user32_dll = ctypes.cdll.LoadLibrary('User32.dll')
print user32_dll.GetDoubleClickTime()
Task: Call a function in a shared library
C#: using System.Runtime.InteropServices;

class Program {
    [DllImport("fakelib.dll")]
    public static extern int fakefunction(int args);

    static void Main(string[] args) {
        int r = fakefunction(10);
    }
}

VB: Declare Dynamic Library "Kernel32"
    Sub SetLastError (ByVal dwErr As Long)
    Function GetLastError& ()
End Declare

SetLastError 20
Print GetLastError
Task: Call a function in a shared library
Java: 

import java.util.Collections;
import java.util.Random;

public class TrySort {
    static boolean useC;
    static {
	try {
	    System.loadLibrary("TrySort");
	    useC = true;
	} catch(UnsatisfiedLinkError e) {
	    useC = false;
	}
    }
    
    static native void sortInC(int[] ary);
    
    static class IntList extends java.util.AbstractList<Integer> {
	int[] ary;
	IntList(int[] ary) { this.ary = ary; }
	public Integer get(int i) { return ary[i]; }
	public Integer set(int i, Integer j) {
	    Integer o = ary[i]; ary[i] = j; return o;
	}
	public int size() { return ary.length; }
    }

    static class ReverseAbsCmp
	implements java.util.Comparator<Integer>
    {
	public int compare(Integer pa, Integer pb) {
	    
	    int a = pa > 0 ? -pa : pa;
	    int b = pb > 0 ? -pb : pb;
	    return a < b ? -1 : a > b ? 1 : 0;
	}
    }
    
    static void sortInJava(int[] ary) {
	Collections.sort(new IntList(ary), new ReverseAbsCmp());
    }

    public static void main(String[] args) {
	
	int[] ary = new int[1000000];
	Random rng = new Random();
	for (int i = 0; i < ary.length; i++)
	    ary[i] = rng.nextInt();

	
	if (useC) {
	    System.out.print("Sorting in C...  ");
	    sortInC(ary);
	} else {
	    System.out.print
		("Missing library for C!  Sorting in Java...  ");
	    sortInJava(ary);
	}

	for (int i = 0; i < ary.length - 1; i++) {
	    int a = ary[i];
	    int b = ary[i + 1];
	    if ((a > 0 ? -a : a) > (b > 0 ? -b : b)) {
		System.out.println("*BUG IN SORT*");
		System.exit(1);
	    }
	}
	System.out.println("ok");
    }
}

Python: import ctypes
  
user32_dll = ctypes.cdll.LoadLibrary('User32.dll')
print user32_dll.GetDoubleClickTime()
Task: Call a function in a shared library
Java: 

import java.util.Collections;
import java.util.Random;

public class TrySort {
    static boolean useC;
    static {
	try {
	    System.loadLibrary("TrySort");
	    useC = true;
	} catch(UnsatisfiedLinkError e) {
	    useC = false;
	}
    }
    
    static native void sortInC(int[] ary);
    
    static class IntList extends java.util.AbstractList<Integer> {
	int[] ary;
	IntList(int[] ary) { this.ary = ary; }
	public Integer get(int i) { return ary[i]; }
	public Integer set(int i, Integer j) {
	    Integer o = ary[i]; ary[i] = j; return o;
	}
	public int size() { return ary.length; }
    }

    static class ReverseAbsCmp
	implements java.util.Comparator<Integer>
    {
	public int compare(Integer pa, Integer pb) {
	    
	    int a = pa > 0 ? -pa : pa;
	    int b = pb > 0 ? -pb : pb;
	    return a < b ? -1 : a > b ? 1 : 0;
	}
    }
    
    static void sortInJava(int[] ary) {
	Collections.sort(new IntList(ary), new ReverseAbsCmp());
    }

    public static void main(String[] args) {
	
	int[] ary = new int[1000000];
	Random rng = new Random();
	for (int i = 0; i < ary.length; i++)
	    ary[i] = rng.nextInt();

	
	if (useC) {
	    System.out.print("Sorting in C...  ");
	    sortInC(ary);
	} else {
	    System.out.print
		("Missing library for C!  Sorting in Java...  ");
	    sortInJava(ary);
	}

	for (int i = 0; i < ary.length - 1; i++) {
	    int a = ary[i];
	    int b = ary[i + 1];
	    if ((a > 0 ? -a : a) > (b > 0 ? -b : b)) {
		System.out.println("*BUG IN SORT*");
		System.exit(1);
	    }
	}
	System.out.println("ok");
    }
}

VB: Declare Dynamic Library "Kernel32"
    Sub SetLastError (ByVal dwErr As Long)
    Function GetLastError& ()
End Declare

SetLastError 20
Print GetLastError
Task: Call a function in a shared library
Go: package main


import "C"
import (
    "fmt"
    "os"
    "unsafe"
)

var handle = -1

func myOpenImage(s string) int {
    fmt.Fprintf(os.Stderr, "internal openImage opens %s...\n", s)
    handle++
    return handle
}

func main() {
    libpath := C.CString("./fakeimglib.so")
    defer C.free(unsafe.Pointer(libpath))
    imglib := C.dlopen(libpath, C.RTLD_LAZY)
    var imghandle int
    if imglib != nil {
        openimage := C.CString("openimage")
        defer C.free(unsafe.Pointer(openimage))
        fp := C.dlsym(imglib, openimage)
        if fp != nil {
            fi := C.CString("fake.img")
            defer C.free(unsafe.Pointer(fi))
            imghandle = int(C.bridge_someFunc(C.someFunc(fp), fi))

        } else {
            imghandle = myOpenImage("fake.img")
        }
        C.dlclose(imglib)
    } else {
        imghandle = myOpenImage("fake.img")
    }
    fmt.Printf("opened with handle %d\n", imghandle)
}

Python: import ctypes
  
user32_dll = ctypes.cdll.LoadLibrary('User32.dll')
print user32_dll.GetDoubleClickTime()
Task: Call a function in a shared library
Go: package main


import "C"
import (
    "fmt"
    "os"
    "unsafe"
)

var handle = -1

func myOpenImage(s string) int {
    fmt.Fprintf(os.Stderr, "internal openImage opens %s...\n", s)
    handle++
    return handle
}

func main() {
    libpath := C.CString("./fakeimglib.so")
    defer C.free(unsafe.Pointer(libpath))
    imglib := C.dlopen(libpath, C.RTLD_LAZY)
    var imghandle int
    if imglib != nil {
        openimage := C.CString("openimage")
        defer C.free(unsafe.Pointer(openimage))
        fp := C.dlsym(imglib, openimage)
        if fp != nil {
            fi := C.CString("fake.img")
            defer C.free(unsafe.Pointer(fi))
            imghandle = int(C.bridge_someFunc(C.someFunc(fp), fi))

        } else {
            imghandle = myOpenImage("fake.img")
        }
        C.dlclose(imglib)
    } else {
        imghandle = myOpenImage("fake.img")
    }
    fmt.Printf("opened with handle %d\n", imghandle)
}

VB: Declare Dynamic Library "Kernel32"
    Sub SetLastError (ByVal dwErr As Long)
    Function GetLastError& ()
End Declare

SetLastError 20
Print GetLastError
Task: Call a function in a shared library
Python: import ctypes
  
user32_dll = ctypes.cdll.LoadLibrary('User32.dll')
print user32_dll.GetDoubleClickTime()

VB: Declare Dynamic Library "Kernel32"
    Sub SetLastError (ByVal dwErr As Long)
    Function GetLastError& ()
End Declare

SetLastError 20
Print GetLastError
Task: Call a function in a shared library
C: #include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

int myopenimage(const char *in)
{
  static int handle=0;
  fprintf(stderr, "internal openimage opens %s...\n", in);
  return handle++;
}

int main()
{
  void *imglib;
  int (*extopenimage)(const char *);
  int imghandle;

  imglib = dlopen("./fakeimglib.so", RTLD_LAZY);
  if ( imglib != NULL ) {
    
    *(void **)(&extopenimage) = dlsym(imglib, "openimage");
    
    
    imghandle = extopenimage("fake.img");
  } else {
    imghandle = myopenimage("fake.img");
  }
  printf("opened with handle %d\n", imghandle);
  
  if (imglib != NULL ) dlclose(imglib);
  return EXIT_SUCCESS;
}

Java: 

import java.util.Collections;
import java.util.Random;

public class TrySort {
    static boolean useC;
    static {
	try {
	    System.loadLibrary("TrySort");
	    useC = true;
	} catch(UnsatisfiedLinkError e) {
	    useC = false;
	}
    }
    
    static native void sortInC(int[] ary);
    
    static class IntList extends java.util.AbstractList<Integer> {
	int[] ary;
	IntList(int[] ary) { this.ary = ary; }
	public Integer get(int i) { return ary[i]; }
	public Integer set(int i, Integer j) {
	    Integer o = ary[i]; ary[i] = j; return o;
	}
	public int size() { return ary.length; }
    }

    static class ReverseAbsCmp
	implements java.util.Comparator<Integer>
    {
	public int compare(Integer pa, Integer pb) {
	    
	    int a = pa > 0 ? -pa : pa;
	    int b = pb > 0 ? -pb : pb;
	    return a < b ? -1 : a > b ? 1 : 0;
	}
    }
    
    static void sortInJava(int[] ary) {
	Collections.sort(new IntList(ary), new ReverseAbsCmp());
    }

    public static void main(String[] args) {
	
	int[] ary = new int[1000000];
	Random rng = new Random();
	for (int i = 0; i < ary.length; i++)
	    ary[i] = rng.nextInt();

	
	if (useC) {
	    System.out.print("Sorting in C...  ");
	    sortInC(ary);
	} else {
	    System.out.print
		("Missing library for C!  Sorting in Java...  ");
	    sortInJava(ary);
	}

	for (int i = 0; i < ary.length - 1; i++) {
	    int a = ary[i];
	    int b = ary[i + 1];
	    if ((a > 0 ? -a : a) > (b > 0 ? -b : b)) {
		System.out.println("*BUG IN SORT*");
		System.exit(1);
	    }
	}
	System.out.println("ok");
    }
}