More number types

master
Rémi Calixte 2018-12-06 12:24:59 -05:00
parent d7e8062229
commit 0f4ff7b994
No known key found for this signature in database
GPG Key ID: 9D8CE6B3C11285A0
4 changed files with 157 additions and 0 deletions

36
complex.go Normal file
View File

@ -0,0 +1,36 @@
package python3
/*
#include "Python.h"
#include "macro.h"
#include "type.h"
*/
import "C"
//Complex : https://docs.python.org/3/c-api/complex.html#c.PyComplex_Type
var Complex = togo(C._go_PyComplex_Type)
//PyComplex_Check : https://docs.python.org/3/c-api/complex.html#c.PyComplex_Check
func PyComplex_Check(p *PyObject) bool {
return C._go_PyComplex_Check(toc(p)) != 0
}
//PyComplex_CheckExact : https://docs.python.org/3/c-api/complex.html#c.PyComplex_CheckExact
func PyComplex_CheckExact(p *PyObject) bool {
return C._go_PyComplex_CheckExact(toc(p)) != 0
}
//PyComplex_FromDoubles : https://docs.python.org/3/c-api/complex.html#c.PyComplex_FromDoubles
func PyComplex_FromDoubles(real, imag float64) *PyObject {
return togo(C.PyComplex_FromDoubles(C.double(real), C.double(imag)))
}
//PyComplex_RealAsDouble : https://docs.python.org/3/c-api/complex.html#c.PyComplex_RealAsDouble
func PyComplex_RealAsDouble(op *PyObject) float64 {
return float64(C.PyComplex_RealAsDouble(toc(op)))
}
//PyComplex_ImagAsDouble : https://docs.python.org/3/c-api/complex.html#c.PyComplex_ImagAsDouble
func PyComplex_ImagAsDouble(op *PyObject) float64 {
return float64(C.PyComplex_ImagAsDouble(toc(op)))
}

20
complex_test.go Normal file
View File

@ -0,0 +1,20 @@
package python3
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestComplex(t *testing.T) {
real := 2.
imaginary := 5.
complex := PyComplex_FromDoubles(real, imaginary)
assert.True(t, PyComplex_Check(complex))
assert.True(t, PyComplex_CheckExact(complex))
defer complex.DecRef()
assert.Equal(t, real, PyComplex_RealAsDouble(complex))
assert.Equal(t, imaginary, PyComplex_ImagAsDouble(complex))
}

56
float.go Normal file
View File

@ -0,0 +1,56 @@
package python3
/*
#include "Python.h"
#include "macro.h"
#include "type.h"
*/
import "C"
//Float : https://docs.python.org/3/c-api/float.html#c.PyFloat_Type
var Float = togo(C._go_PyFloat_Type)
//PyFloat_Check : https://docs.python.org/3/c-api/float.html#c.PyFloat_Check
func PyFloat_Check(p *PyObject) bool {
return C._go_PyFloat_Check(toc(p)) != 0
}
//PyFloat_CheckExact : https://docs.python.org/3/c-api/float.html#c.PyFloat_CheckExact
func PyFloat_CheckExact(p *PyObject) bool {
return C._go_PyFloat_CheckExact(toc(p)) != 0
}
//PyFloat_FromDouble : https://docs.python.org/3/c-api/float.html#c.PyFloat_FromDouble
func PyFloat_FromDouble(v float64) *PyObject {
return togo(C.PyFloat_FromDouble(C.double(v)))
}
//PyFloat_FromString : https://docs.python.org/3/c-api/float.html#c.PyFloat_FromString
func PyFloat_FromString(str *PyObject) *PyObject {
return togo(C.PyFloat_FromString(toc(str)))
}
//PyFloat_AsDouble : https://docs.python.org/3/c-api/float.html#c.PyFloat_AsDouble
func PyFloat_AsDouble(obj *PyObject) float64 {
return float64(C.PyFloat_AsDouble(toc(obj)))
}
//PyFloat_GetInfo : https://docs.python.org/3/c-api/float.html#c.PyFloat_GetInfo
func PyFloat_GetInfo() *PyObject {
return togo(C.PyFloat_GetInfo())
}
//PyFloat_GetMax : https://docs.python.org/3/c-api/float.html#c.PyFloat_GetMax
func PyFloat_GetMax() float64 {
return float64(C.PyFloat_GetMax())
}
//PyFloat_GetMin : https://docs.python.org/3/c-api/float.html#c.PyFloat_GetMin
func PyFloat_GetMin() float64 {
return float64(C.PyFloat_GetMin())
}
//PyFloat_ClearFreeList : https://docs.python.org/3/c-api/float.html#c.PyFloat_ClearFreeList
func PyFloat_ClearFreeList() int {
return int(C.PyFloat_ClearFreeList())
}

45
float_test.go Normal file
View File

@ -0,0 +1,45 @@
package python3
import (
"math"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPyFloatCheck(t *testing.T) {
Py_Initialize()
pyFloat := PyFloat_FromDouble(345.)
assert.True(t, PyFloat_Check(pyFloat))
assert.True(t, PyFloat_CheckExact(pyFloat))
pyFloat.DecRef()
}
func TestPyFloatFromAsDouble(t *testing.T) {
Py_Initialize()
v := 2354.
pyFloat := PyFloat_FromDouble(v)
assert.NotNil(t, pyFloat)
assert.Equal(t, v, PyFloat_AsDouble(pyFloat))
pyFloat.DecRef()
}
func TestPyFloatFromAsString(t *testing.T) {
Py_Initialize()
pyString := PyUnicode_FromString("2354")
defer pyString.DecRef()
pyFloat := PyFloat_FromString(pyString)
assert.NotNil(t, pyFloat)
assert.Equal(t, 2354., PyFloat_AsDouble(pyFloat))
pyFloat.DecRef()
}
func TestPyFloatMinMax(t *testing.T) {
Py_Initialize()
assert.Equal(t, math.MaxFloat64, PyFloat_GetMax())
assert.Equal(t, 2.2250738585072014e-308, PyFloat_GetMin())
}