Commit 14ac0f69 authored by Andrew Twyman's avatar Andrew Twyman

Support eq and ord on dates

Fixes #223

Turns out eq and ord were never implemented for dates in Java or ObjC.
I implemented, and added that type to the record_with_derivings test.
parent 6a2164ef
...@@ -277,11 +277,10 @@ class JavaGenerator(spec: Spec) extends Generator(spec) { ...@@ -277,11 +277,10 @@ class JavaGenerator(spec: Spec) extends Generator(spec) {
skipFirst { w.wl(" &&") } skipFirst { w.wl(" &&") }
f.ty.resolved.base match { f.ty.resolved.base match {
case MBinary => w.w(s"java.util.Arrays.equals(${idJava.field(f.ident)}, other.${idJava.field(f.ident)})") case MBinary => w.w(s"java.util.Arrays.equals(${idJava.field(f.ident)}, other.${idJava.field(f.ident)})")
case MList | MSet | MMap => w.w(s"this.${idJava.field(f.ident)}.equals(other.${idJava.field(f.ident)})") case MList | MSet | MMap | MString | MDate => w.w(s"this.${idJava.field(f.ident)}.equals(other.${idJava.field(f.ident)})")
case MOptional => case MOptional =>
w.w(s"((this.${idJava.field(f.ident)} == null && other.${idJava.field(f.ident)} == null) || ") w.w(s"((this.${idJava.field(f.ident)} == null && other.${idJava.field(f.ident)} == null) || ")
w.w(s"(this.${idJava.field(f.ident)} != null && this.${idJava.field(f.ident)}.equals(other.${idJava.field(f.ident)})))") w.w(s"(this.${idJava.field(f.ident)} != null && this.${idJava.field(f.ident)}.equals(other.${idJava.field(f.ident)})))")
case MString => w.w(s"this.${idJava.field(f.ident)}.equals(other.${idJava.field(f.ident)})")
case t: MPrimitive => w.w(s"this.${idJava.field(f.ident)} == other.${idJava.field(f.ident)}") case t: MPrimitive => w.w(s"this.${idJava.field(f.ident)} == other.${idJava.field(f.ident)}")
case df: MDef => df.defType match { case df: MDef => df.defType match {
case DRecord => w.w(s"this.${idJava.field(f.ident)}.equals(other.${idJava.field(f.ident)})") case DRecord => w.w(s"this.${idJava.field(f.ident)}.equals(other.${idJava.field(f.ident)})")
...@@ -378,7 +377,7 @@ class JavaGenerator(spec: Spec) extends Generator(spec) { ...@@ -378,7 +377,7 @@ class JavaGenerator(spec: Spec) extends Generator(spec) {
w.wl("int tempResult;") w.wl("int tempResult;")
for (f <- r.fields) { for (f <- r.fields) {
f.ty.resolved.base match { f.ty.resolved.base match {
case MString => w.wl(s"tempResult = this.${idJava.field(f.ident)}.compareTo(other.${idJava.field(f.ident)});") case MString | MDate => w.wl(s"tempResult = this.${idJava.field(f.ident)}.compareTo(other.${idJava.field(f.ident)});")
case t: MPrimitive => primitiveCompare(f.ident) case t: MPrimitive => primitiveCompare(f.ident)
case df: MDef => df.defType match { case df: MDef => df.defType match {
case DRecord => w.wl(s"tempResult = this.${idJava.field(f.ident)}.compareTo(other.${idJava.field(f.ident)});") case DRecord => w.wl(s"tempResult = this.${idJava.field(f.ident)}.compareTo(other.${idJava.field(f.ident)});")
......
...@@ -293,6 +293,7 @@ class ObjcGenerator(spec: Spec) extends Generator(spec) { ...@@ -293,6 +293,7 @@ class ObjcGenerator(spec: Spec) extends Generator(spec) {
w.w(s"(self.${idObjc.field(f.ident)} != nil && [self.${idObjc.field(f.ident)} isEqual:typedOther.${idObjc.field(f.ident)}]))") w.w(s"(self.${idObjc.field(f.ident)} != nil && [self.${idObjc.field(f.ident)} isEqual:typedOther.${idObjc.field(f.ident)}]))")
} }
case MString => w.w(s"[self.${idObjc.field(f.ident)} isEqualToString:typedOther.${idObjc.field(f.ident)}]") case MString => w.w(s"[self.${idObjc.field(f.ident)} isEqualToString:typedOther.${idObjc.field(f.ident)}]")
case MDate => w.w(s"[self.${idObjc.field(f.ident)} isEqualToDate:typedOther.${idObjc.field(f.ident)}]")
case t: MPrimitive => w.w(s"self.${idObjc.field(f.ident)} == typedOther.${idObjc.field(f.ident)}") case t: MPrimitive => w.w(s"self.${idObjc.field(f.ident)} == typedOther.${idObjc.field(f.ident)}")
case df: MDef => df.defType match { case df: MDef => df.defType match {
case DRecord => w.w(s"[self.${idObjc.field(f.ident)} isEqual:typedOther.${idObjc.field(f.ident)}]") case DRecord => w.w(s"[self.${idObjc.field(f.ident)} isEqual:typedOther.${idObjc.field(f.ident)}]")
...@@ -366,7 +367,7 @@ class ObjcGenerator(spec: Spec) extends Generator(spec) { ...@@ -366,7 +367,7 @@ class ObjcGenerator(spec: Spec) extends Generator(spec) {
w.wl("NSComparisonResult tempResult;") w.wl("NSComparisonResult tempResult;")
for (f <- r.fields) { for (f <- r.fields) {
f.ty.resolved.base match { f.ty.resolved.base match {
case MString => w.wl(s"tempResult = [self.${idObjc.field(f.ident)} compare:other.${idObjc.field(f.ident)}];") case MString | MDate => w.wl(s"tempResult = [self.${idObjc.field(f.ident)} compare:other.${idObjc.field(f.ident)}];")
case t: MPrimitive => generatePrimitiveOrder(f.ident, w) case t: MPrimitive => generatePrimitiveOrder(f.ident, w)
case df: MDef => df.defType match { case df: MDef => df.defType match {
case DRecord => w.wl(s"tempResult = [self.${idObjc.field(f.ident)} compare:other.${idObjc.field(f.ident)}];") case DRecord => w.wl(s"tempResult = [self.${idObjc.field(f.ident)} compare:other.${idObjc.field(f.ident)}];")
......
record_with_derivings = record { record_with_derivings = record {
key1: i32; eight: i8;
key2: string; sixteen: i16;
thirtytwo: i32;
sixtyfour: i64;
fthirtytwo: f32;
fsixtyfour: f64;
d: date;
s: string;
} deriving (eq, ord) } deriving (eq, ord)
record_with_nested_derivings = record { record_with_nested_derivings = record {
......
...@@ -7,8 +7,14 @@ namespace testsuite { ...@@ -7,8 +7,14 @@ namespace testsuite {
bool operator==(const RecordWithDerivings& lhs, const RecordWithDerivings& rhs) { bool operator==(const RecordWithDerivings& lhs, const RecordWithDerivings& rhs) {
return lhs.key1 == rhs.key1 && return lhs.eight == rhs.eight &&
lhs.key2 == rhs.key2; lhs.sixteen == rhs.sixteen &&
lhs.thirtytwo == rhs.thirtytwo &&
lhs.sixtyfour == rhs.sixtyfour &&
lhs.fthirtytwo == rhs.fthirtytwo &&
lhs.fsixtyfour == rhs.fsixtyfour &&
lhs.d == rhs.d &&
lhs.s == rhs.s;
} }
bool operator!=(const RecordWithDerivings& lhs, const RecordWithDerivings& rhs) { bool operator!=(const RecordWithDerivings& lhs, const RecordWithDerivings& rhs) {
...@@ -16,16 +22,52 @@ bool operator!=(const RecordWithDerivings& lhs, const RecordWithDerivings& rhs) ...@@ -16,16 +22,52 @@ bool operator!=(const RecordWithDerivings& lhs, const RecordWithDerivings& rhs)
} }
bool operator<(const RecordWithDerivings& lhs, const RecordWithDerivings& rhs) { bool operator<(const RecordWithDerivings& lhs, const RecordWithDerivings& rhs) {
if (lhs.key1 < rhs.key1) { if (lhs.eight < rhs.eight) {
return true; return true;
} }
if (rhs.key1 < lhs.key1) { if (rhs.eight < lhs.eight) {
return false; return false;
} }
if (lhs.key2 < rhs.key2) { if (lhs.sixteen < rhs.sixteen) {
return true; return true;
} }
if (rhs.key2 < lhs.key2) { if (rhs.sixteen < lhs.sixteen) {
return false;
}
if (lhs.thirtytwo < rhs.thirtytwo) {
return true;
}
if (rhs.thirtytwo < lhs.thirtytwo) {
return false;
}
if (lhs.sixtyfour < rhs.sixtyfour) {
return true;
}
if (rhs.sixtyfour < lhs.sixtyfour) {
return false;
}
if (lhs.fthirtytwo < rhs.fthirtytwo) {
return true;
}
if (rhs.fthirtytwo < lhs.fthirtytwo) {
return false;
}
if (lhs.fsixtyfour < rhs.fsixtyfour) {
return true;
}
if (rhs.fsixtyfour < lhs.fsixtyfour) {
return false;
}
if (lhs.d < rhs.d) {
return true;
}
if (rhs.d < lhs.d) {
return false;
}
if (lhs.s < rhs.s) {
return true;
}
if (rhs.s < lhs.s) {
return false; return false;
} }
return false; return false;
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#pragma once #pragma once
#include <chrono>
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <utility> #include <utility>
...@@ -10,8 +11,14 @@ ...@@ -10,8 +11,14 @@
namespace testsuite { namespace testsuite {
struct RecordWithDerivings final { struct RecordWithDerivings final {
int32_t key1; int8_t eight;
std::string key2; int16_t sixteen;
int32_t thirtytwo;
int64_t sixtyfour;
float fthirtytwo;
double fsixtyfour;
std::chrono::system_clock::time_point d;
std::string s;
friend bool operator==(const RecordWithDerivings& lhs, const RecordWithDerivings& rhs); friend bool operator==(const RecordWithDerivings& lhs, const RecordWithDerivings& rhs);
friend bool operator!=(const RecordWithDerivings& lhs, const RecordWithDerivings& rhs); friend bool operator!=(const RecordWithDerivings& lhs, const RecordWithDerivings& rhs);
...@@ -22,10 +29,22 @@ struct RecordWithDerivings final { ...@@ -22,10 +29,22 @@ struct RecordWithDerivings final {
friend bool operator<=(const RecordWithDerivings& lhs, const RecordWithDerivings& rhs); friend bool operator<=(const RecordWithDerivings& lhs, const RecordWithDerivings& rhs);
friend bool operator>=(const RecordWithDerivings& lhs, const RecordWithDerivings& rhs); friend bool operator>=(const RecordWithDerivings& lhs, const RecordWithDerivings& rhs);
RecordWithDerivings(int32_t key1_, RecordWithDerivings(int8_t eight_,
std::string key2_) int16_t sixteen_,
: key1(std::move(key1_)) int32_t thirtytwo_,
, key2(std::move(key2_)) int64_t sixtyfour_,
float fthirtytwo_,
double fsixtyfour_,
std::chrono::system_clock::time_point d_,
std::string s_)
: eight(std::move(eight_))
, sixteen(std::move(sixteen_))
, thirtytwo(std::move(thirtytwo_))
, sixtyfour(std::move(sixtyfour_))
, fthirtytwo(std::move(fthirtytwo_))
, fsixtyfour(std::move(fsixtyfour_))
, d(std::move(d_))
, s(std::move(s_))
{} {}
}; };
......
...@@ -3,30 +3,80 @@ ...@@ -3,30 +3,80 @@
package com.dropbox.djinni.test; package com.dropbox.djinni.test;
import java.util.Date;
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
public class RecordWithDerivings implements Comparable<RecordWithDerivings> { public class RecordWithDerivings implements Comparable<RecordWithDerivings> {
/*package*/ final int mKey1; /*package*/ final byte mEight;
/*package*/ final String mKey2; /*package*/ final short mSixteen;
/*package*/ final int mThirtytwo;
/*package*/ final long mSixtyfour;
/*package*/ final float mFthirtytwo;
/*package*/ final double mFsixtyfour;
/*package*/ final Date mD;
/*package*/ final String mS;
public RecordWithDerivings( public RecordWithDerivings(
int key1, byte eight,
@Nonnull String key2) { short sixteen,
this.mKey1 = key1; int thirtytwo,
this.mKey2 = key2; long sixtyfour,
float fthirtytwo,
double fsixtyfour,
@Nonnull Date d,
@Nonnull String s) {
this.mEight = eight;
this.mSixteen = sixteen;
this.mThirtytwo = thirtytwo;
this.mSixtyfour = sixtyfour;
this.mFthirtytwo = fthirtytwo;
this.mFsixtyfour = fsixtyfour;
this.mD = d;
this.mS = s;
}
public byte getEight() {
return mEight;
}
public short getSixteen() {
return mSixteen;
}
public int getThirtytwo() {
return mThirtytwo;
}
public long getSixtyfour() {
return mSixtyfour;
}
public float getFthirtytwo() {
return mFthirtytwo;
} }
public int getKey1() { public double getFsixtyfour() {
return mKey1; return mFsixtyfour;
} }
@Nonnull @Nonnull
public String getKey2() { public Date getD() {
return mKey2; return mD;
}
@Nonnull
public String getS() {
return mS;
} }
@Override @Override
...@@ -35,24 +85,42 @@ public class RecordWithDerivings implements Comparable<RecordWithDerivings> { ...@@ -35,24 +85,42 @@ public class RecordWithDerivings implements Comparable<RecordWithDerivings> {
return false; return false;
} }
RecordWithDerivings other = (RecordWithDerivings) obj; RecordWithDerivings other = (RecordWithDerivings) obj;
return this.mKey1 == other.mKey1 && return this.mEight == other.mEight &&
this.mKey2.equals(other.mKey2); this.mSixteen == other.mSixteen &&
this.mThirtytwo == other.mThirtytwo &&
this.mSixtyfour == other.mSixtyfour &&
this.mFthirtytwo == other.mFthirtytwo &&
this.mFsixtyfour == other.mFsixtyfour &&
this.mD.equals(other.mD) &&
this.mS.equals(other.mS);
} }
@Override @Override
public int hashCode() { public int hashCode() {
// Pick an arbitrary non-zero starting value // Pick an arbitrary non-zero starting value
int hashCode = 17; int hashCode = 17;
hashCode = hashCode * 31 + mKey1; hashCode = hashCode * 31 + mEight;
hashCode = hashCode * 31 + mKey2.hashCode(); hashCode = hashCode * 31 + mSixteen;
hashCode = hashCode * 31 + mThirtytwo;
hashCode = hashCode * 31 + ((int) (mSixtyfour ^ (mSixtyfour >>> 32)));
hashCode = hashCode * 31 + Float.floatToIntBits(mFthirtytwo);
hashCode = hashCode * 31 + ((int) (Double.doubleToLongBits(mFsixtyfour) ^ (Double.doubleToLongBits(mFsixtyfour) >>> 32)));
hashCode = hashCode * 31 + mD.hashCode();
hashCode = hashCode * 31 + mS.hashCode();
return hashCode; return hashCode;
} }
@Override @Override
public String toString() { public String toString() {
return "RecordWithDerivings{" + return "RecordWithDerivings{" +
"mKey1=" + mKey1 + "mEight=" + mEight +
"," + "mKey2=" + mKey2 + "," + "mSixteen=" + mSixteen +
"," + "mThirtytwo=" + mThirtytwo +
"," + "mSixtyfour=" + mSixtyfour +
"," + "mFthirtytwo=" + mFthirtytwo +
"," + "mFsixtyfour=" + mFsixtyfour +
"," + "mD=" + mD +
"," + "mS=" + mS +
"}"; "}";
} }
...@@ -60,9 +128,39 @@ public class RecordWithDerivings implements Comparable<RecordWithDerivings> { ...@@ -60,9 +128,39 @@ public class RecordWithDerivings implements Comparable<RecordWithDerivings> {
@Override @Override
public int compareTo(@Nonnull RecordWithDerivings other) { public int compareTo(@Nonnull RecordWithDerivings other) {
int tempResult; int tempResult;
if (this.mKey1 < other.mKey1) { if (this.mEight < other.mEight) {
tempResult = -1;
} else if (this.mEight > other.mEight) {
tempResult = 1;
} else {
tempResult = 0;
}
if (tempResult != 0) {
return tempResult;
}
if (this.mSixteen < other.mSixteen) {
tempResult = -1;
} else if (this.mSixteen > other.mSixteen) {
tempResult = 1;
} else {
tempResult = 0;
}
if (tempResult != 0) {
return tempResult;
}
if (this.mThirtytwo < other.mThirtytwo) {
tempResult = -1;
} else if (this.mThirtytwo > other.mThirtytwo) {
tempResult = 1;
} else {
tempResult = 0;
}
if (tempResult != 0) {
return tempResult;
}
if (this.mSixtyfour < other.mSixtyfour) {
tempResult = -1; tempResult = -1;
} else if (this.mKey1 > other.mKey1) { } else if (this.mSixtyfour > other.mSixtyfour) {
tempResult = 1; tempResult = 1;
} else { } else {
tempResult = 0; tempResult = 0;
...@@ -70,7 +168,31 @@ public class RecordWithDerivings implements Comparable<RecordWithDerivings> { ...@@ -70,7 +168,31 @@ public class RecordWithDerivings implements Comparable<RecordWithDerivings> {
if (tempResult != 0) { if (tempResult != 0) {
return tempResult; return tempResult;
} }
tempResult = this.mKey2.compareTo(other.mKey2); if (this.mFthirtytwo < other.mFthirtytwo) {
tempResult = -1;
} else if (this.mFthirtytwo > other.mFthirtytwo) {
tempResult = 1;
} else {
tempResult = 0;
}
if (tempResult != 0) {
return tempResult;
}
if (this.mFsixtyfour < other.mFsixtyfour) {
tempResult = -1;
} else if (this.mFsixtyfour > other.mFsixtyfour) {
tempResult = 1;
} else {
tempResult = 0;
}
if (tempResult != 0) {
return tempResult;
}
tempResult = this.mD.compareTo(other.mD);
if (tempResult != 0) {
return tempResult;
}
tempResult = this.mS.compareTo(other.mS);
if (tempResult != 0) { if (tempResult != 0) {
return tempResult; return tempResult;
} }
......
...@@ -13,18 +13,30 @@ NativeRecordWithDerivings::~NativeRecordWithDerivings() = default; ...@@ -13,18 +13,30 @@ NativeRecordWithDerivings::~NativeRecordWithDerivings() = default;
auto NativeRecordWithDerivings::fromCpp(JNIEnv* jniEnv, const CppType& c) -> ::djinni::LocalRef<JniType> { auto NativeRecordWithDerivings::fromCpp(JNIEnv* jniEnv, const CppType& c) -> ::djinni::LocalRef<JniType> {
const auto& data = ::djinni::JniClass<NativeRecordWithDerivings>::get(); const auto& data = ::djinni::JniClass<NativeRecordWithDerivings>::get();
auto r = ::djinni::LocalRef<JniType>{jniEnv->NewObject(data.clazz.get(), data.jconstructor, auto r = ::djinni::LocalRef<JniType>{jniEnv->NewObject(data.clazz.get(), data.jconstructor,
::djinni::get(::djinni::I32::fromCpp(jniEnv, c.key1)), ::djinni::get(::djinni::I8::fromCpp(jniEnv, c.eight)),
::djinni::get(::djinni::String::fromCpp(jniEnv, c.key2)))}; ::djinni::get(::djinni::I16::fromCpp(jniEnv, c.sixteen)),
::djinni::get(::djinni::I32::fromCpp(jniEnv, c.thirtytwo)),
::djinni::get(::djinni::I64::fromCpp(jniEnv, c.sixtyfour)),
::djinni::get(::djinni::F32::fromCpp(jniEnv, c.fthirtytwo)),
::djinni::get(::djinni::F64::fromCpp(jniEnv, c.fsixtyfour)),
::djinni::get(::djinni::Date::fromCpp(jniEnv, c.d)),
::djinni::get(::djinni::String::fromCpp(jniEnv, c.s)))};
::djinni::jniExceptionCheck(jniEnv); ::djinni::jniExceptionCheck(jniEnv);
return r; return r;
} }
auto NativeRecordWithDerivings::toCpp(JNIEnv* jniEnv, JniType j) -> CppType { auto NativeRecordWithDerivings::toCpp(JNIEnv* jniEnv, JniType j) -> CppType {
::djinni::JniLocalScope jscope(jniEnv, 3); ::djinni::JniLocalScope jscope(jniEnv, 9);
assert(j != nullptr); assert(j != nullptr);
const auto& data = ::djinni::JniClass<NativeRecordWithDerivings>::get(); const auto& data = ::djinni::JniClass<NativeRecordWithDerivings>::get();
return {::djinni::I32::toCpp(jniEnv, jniEnv->GetIntField(j, data.field_mKey1)), return {::djinni::I8::toCpp(jniEnv, jniEnv->GetByteField(j, data.field_mEight)),
::djinni::String::toCpp(jniEnv, (jstring)jniEnv->GetObjectField(j, data.field_mKey2))}; ::djinni::I16::toCpp(jniEnv, jniEnv->GetShortField(j, data.field_mSixteen)),
::djinni::I32::toCpp(jniEnv, jniEnv->GetIntField(j, data.field_mThirtytwo)),
::djinni::I64::toCpp(jniEnv, jniEnv->GetLongField(j, data.field_mSixtyfour)),
::djinni::F32::toCpp(jniEnv, jniEnv->GetFloatField(j, data.field_mFthirtytwo)),
::djinni::F64::toCpp(jniEnv, jniEnv->GetDoubleField(j, data.field_mFsixtyfour)),
::djinni::Date::toCpp(jniEnv, jniEnv->GetObjectField(j, data.field_mD)),
::djinni::String::toCpp(jniEnv, (jstring)jniEnv->GetObjectField(j, data.field_mS))};
} }
} // namespace djinni_generated } // namespace djinni_generated
...@@ -25,9 +25,15 @@ private: ...@@ -25,9 +25,15 @@ private:
friend ::djinni::JniClass<NativeRecordWithDerivings>; friend ::djinni::JniClass<NativeRecordWithDerivings>;
const ::djinni::GlobalRef<jclass> clazz { ::djinni::jniFindClass("com/dropbox/djinni/test/RecordWithDerivings") }; const ::djinni::GlobalRef<jclass> clazz { ::djinni::jniFindClass("com/dropbox/djinni/test/RecordWithDerivings") };
const jmethodID jconstructor { ::djinni::jniGetMethodID(clazz.get(), "<init>", "(ILjava/lang/String;)V") }; const jmethodID jconstructor { ::djinni::jniGetMethodID(clazz.get(), "<init>", "(BSIJFDLjava/util/Date;Ljava/lang/String;)V") };
const jfieldID field_mKey1 { ::djinni::jniGetFieldID(clazz.get(), "mKey1", "I") }; const jfieldID field_mEight { ::djinni::jniGetFieldID(clazz.get(), "mEight", "B") };
const jfieldID field_mKey2 { ::djinni::jniGetFieldID(clazz.get(), "mKey2", "Ljava/lang/String;") }; const jfieldID field_mSixteen { ::djinni::jniGetFieldID(clazz.get(), "mSixteen", "S") };
const jfieldID field_mThirtytwo { ::djinni::jniGetFieldID(clazz.get(), "mThirtytwo", "I") };
const jfieldID field_mSixtyfour { ::djinni::jniGetFieldID(clazz.get(), "mSixtyfour", "J") };
const jfieldID field_mFthirtytwo { ::djinni::jniGetFieldID(clazz.get(), "mFthirtytwo", "F") };
const jfieldID field_mFsixtyfour { ::djinni::jniGetFieldID(clazz.get(), "mFsixtyfour", "D") };
const jfieldID field_mD { ::djinni::jniGetFieldID(clazz.get(), "mD", "Ljava/util/Date;") };
const jfieldID field_mS { ::djinni::jniGetFieldID(clazz.get(), "mS", "Ljava/lang/String;") };
}; };
} // namespace djinni_generated } // namespace djinni_generated
...@@ -10,14 +10,26 @@ namespace djinni_generated { ...@@ -10,14 +10,26 @@ namespace djinni_generated {
auto RecordWithDerivings::toCpp(ObjcType obj) -> CppType auto RecordWithDerivings::toCpp(ObjcType obj) -> CppType
{ {
assert(obj); assert(obj);
return {::djinni::I32::toCpp(obj.key1), return {::djinni::I8::toCpp(obj.eight),
::djinni::String::toCpp(obj.key2)}; ::djinni::I16::toCpp(obj.sixteen),
::djinni::I32::toCpp(obj.thirtytwo),
::djinni::I64::toCpp(obj.sixtyfour),
::djinni::F32::toCpp(obj.fthirtytwo),
::djinni::F64::toCpp(obj.fsixtyfour),
::djinni::Date::toCpp(obj.d),
::djinni::String::toCpp(obj.s)};
} }
auto RecordWithDerivings::fromCpp(const CppType& cpp) -> ObjcType auto RecordWithDerivings::fromCpp(const CppType& cpp) -> ObjcType
{ {
return [[DBRecordWithDerivings alloc] initWithKey1:(::djinni::I32::fromCpp(cpp.key1)) return [[DBRecordWithDerivings alloc] initWithEight:(::djinni::I8::fromCpp(cpp.eight))
key2:(::djinni::String::fromCpp(cpp.key2))]; sixteen:(::djinni::I16::fromCpp(cpp.sixteen))
thirtytwo:(::djinni::I32::fromCpp(cpp.thirtytwo))
sixtyfour:(::djinni::I64::fromCpp(cpp.sixtyfour))
fthirtytwo:(::djinni::F32::fromCpp(cpp.fthirtytwo))
fsixtyfour:(::djinni::F64::fromCpp(cpp.fsixtyfour))
d:(::djinni::Date::fromCpp(cpp.d))
s:(::djinni::String::fromCpp(cpp.s))];
} }
} // namespace djinni_generated } // namespace djinni_generated
...@@ -4,14 +4,38 @@ ...@@ -4,14 +4,38 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
@interface DBRecordWithDerivings : NSObject @interface DBRecordWithDerivings : NSObject
- (nonnull instancetype)initWithKey1:(int32_t)key1 - (nonnull instancetype)initWithEight:(int8_t)eight
key2:(nonnull NSString *)key2; sixteen:(int16_t)sixteen
+ (nonnull instancetype)recordWithDerivingsWithKey1:(int32_t)key1 thirtytwo:(int32_t)thirtytwo
key2:(nonnull NSString *)key2; sixtyfour:(int64_t)sixtyfour
fthirtytwo:(float)fthirtytwo
fsixtyfour:(double)fsixtyfour
d:(nonnull NSDate *)d
s:(nonnull NSString *)s;
+ (nonnull instancetype)recordWithDerivingsWithEight:(int8_t)eight
sixteen:(int16_t)sixteen
thirtytwo:(int32_t)thirtytwo
sixtyfour:(int64_t)sixtyfour
fthirtytwo:(float)fthirtytwo
fsixtyfour:(double)fsixtyfour
d:(nonnull NSDate *)d
s:(nonnull NSString *)s;
@property (nonatomic, readonly) int32_t key1; @property (nonatomic, readonly) int8_t eight;
@property (nonatomic, readonly, nonnull) NSString * key2; @property (nonatomic, readonly) int16_t sixteen;
@property (nonatomic, readonly) int32_t thirtytwo;
@property (nonatomic, readonly) int64_t sixtyfour;
@property (nonatomic, readonly) float fthirtytwo;
@property (nonatomic, readonly) double fsixtyfour;
@property (nonatomic, readonly, nonnull) NSDate * d;
@property (nonatomic, readonly, nonnull) NSString * s;
- (NSComparisonResult)compare:(nonnull DBRecordWithDerivings *)other; - (NSComparisonResult)compare:(nonnull DBRecordWithDerivings *)other;
......
...@@ -6,21 +6,45 @@ ...@@ -6,21 +6,45 @@
@implementation DBRecordWithDerivings @implementation DBRecordWithDerivings
- (nonnull instancetype)initWithKey1:(int32_t)key1 - (nonnull instancetype)initWithEight:(int8_t)eight
key2:(nonnull NSString *)key2 sixteen:(int16_t)sixteen
thirtytwo:(int32_t)thirtytwo
sixtyfour:(int64_t)sixtyfour
fthirtytwo:(float)fthirtytwo
fsixtyfour:(double)fsixtyfour
d:(nonnull NSDate *)d
s:(nonnull NSString *)s
{ {
if (self = [super init]) { if (self = [super init]) {
_key1 = key1; _eight = eight;
_key2 = [key2 copy]; _sixteen = sixteen;
_thirtytwo = thirtytwo;
_sixtyfour = sixtyfour;
_fthirtytwo = fthirtytwo;
_fsixtyfour = fsixtyfour;
_d = d;
_s = [s copy];
} }
return self; return self;
} }
+ (nonnull instancetype)recordWithDerivingsWithKey1:(int32_t)key1 + (nonnull instancetype)recordWithDerivingsWithEight:(int8_t)eight
key2:(nonnull NSString *)key2 sixteen:(int16_t)sixteen
thirtytwo:(int32_t)thirtytwo
sixtyfour:(int64_t)sixtyfour
fthirtytwo:(float)fthirtytwo
fsixtyfour:(double)fsixtyfour
d:(nonnull NSDate *)d
s:(nonnull NSString *)s
{ {
return [[self alloc] initWithKey1:key1 return [[self alloc] initWithEight:eight
key2:key2]; sixteen:sixteen
thirtytwo:thirtytwo
sixtyfour:sixtyfour
fthirtytwo:fthirtytwo
fsixtyfour:fsixtyfour
d:d
s:s];
} }
- (BOOL)isEqual:(id)other - (BOOL)isEqual:(id)other
...@@ -29,23 +53,35 @@ ...@@ -29,23 +53,35 @@
return NO; return NO;
} }
DBRecordWithDerivings *typedOther = (DBRecordWithDerivings *)other; DBRecordWithDerivings *typedOther = (DBRecordWithDerivings *)other;
return self.key1 == typedOther.key1 && return self.eight == typedOther.eight &&
[self.key2 isEqualToString:typedOther.key2]; self.sixteen == typedOther.sixteen &&
self.thirtytwo == typedOther.thirtytwo &&
self.sixtyfour == typedOther.sixtyfour &&
self.fthirtytwo == typedOther.fthirtytwo &&
self.fsixtyfour == typedOther.fsixtyfour &&
[self.d isEqualToDate:typedOther.d] &&
[self.s isEqualToString:typedOther.s];
} }
- (NSUInteger)hash - (NSUInteger)hash
{ {
return NSStringFromClass([self class]).hash ^ return NSStringFromClass([self class]).hash ^
(NSUInteger)self.key1 ^ (NSUInteger)self.eight ^
self.key2.hash; (NSUInteger)self.sixteen ^
(NSUInteger)self.thirtytwo ^
(NSUInteger)self.sixtyfour ^
(NSUInteger)self.fthirtytwo ^
(NSUInteger)self.fsixtyfour ^
self.d.hash ^
self.s.hash;
} }
- (NSComparisonResult)compare:(DBRecordWithDerivings *)other - (NSComparisonResult)compare:(DBRecordWithDerivings *)other
{ {
NSComparisonResult tempResult; NSComparisonResult tempResult;
if (self.key1 < other.key1) { if (self.eight < other.eight) {
tempResult = NSOrderedAscending; tempResult = NSOrderedAscending;
} else if (self.key1 > other.key1) { } else if (self.eight > other.eight) {
tempResult = NSOrderedDescending; tempResult = NSOrderedDescending;
} else { } else {
tempResult = NSOrderedSame; tempResult = NSOrderedSame;
...@@ -53,7 +89,61 @@ ...@@ -53,7 +89,61 @@
if (tempResult != NSOrderedSame) { if (tempResult != NSOrderedSame) {
return tempResult; return tempResult;
} }
tempResult = [self.key2 compare:other.key2]; if (self.sixteen < other.sixteen) {
tempResult = NSOrderedAscending;
} else if (self.sixteen > other.sixteen) {
tempResult = NSOrderedDescending;
} else {
tempResult = NSOrderedSame;
}
if (tempResult != NSOrderedSame) {
return tempResult;
}
if (self.thirtytwo < other.thirtytwo) {
tempResult = NSOrderedAscending;
} else if (self.thirtytwo > other.thirtytwo) {
tempResult = NSOrderedDescending;
} else {
tempResult = NSOrderedSame;
}
if (tempResult != NSOrderedSame) {
return tempResult;
}
if (self.sixtyfour < other.sixtyfour) {
tempResult = NSOrderedAscending;
} else if (self.sixtyfour > other.sixtyfour) {
tempResult = NSOrderedDescending;
} else {
tempResult = NSOrderedSame;
}
if (tempResult != NSOrderedSame) {
return tempResult;
}
if (self.fthirtytwo < other.fthirtytwo) {
tempResult = NSOrderedAscending;
} else if (self.fthirtytwo > other.fthirtytwo) {
tempResult = NSOrderedDescending;
} else {
tempResult = NSOrderedSame;
}
if (tempResult != NSOrderedSame) {
return tempResult;
}
if (self.fsixtyfour < other.fsixtyfour) {
tempResult = NSOrderedAscending;
} else if (self.fsixtyfour > other.fsixtyfour) {
tempResult = NSOrderedDescending;
} else {
tempResult = NSOrderedSame;
}
if (tempResult != NSOrderedSame) {
return tempResult;
}
tempResult = [self.d compare:other.d];
if (tempResult != NSOrderedSame) {
return tempResult;
}
tempResult = [self.s compare:other.s];
if (tempResult != NSOrderedSame) { if (tempResult != NSOrderedSame) {
return tempResult; return tempResult;
} }
...@@ -62,7 +152,7 @@ ...@@ -62,7 +152,7 @@
- (NSString *)description - (NSString *)description
{ {
return [NSString stringWithFormat:@"<%@ %p key1:%@ key2:%@>", self.class, (void *)self, @(self.key1), self.key2]; return [NSString stringWithFormat:@"<%@ %p eight:%@ sixteen:%@ thirtytwo:%@ sixtyfour:%@ fthirtytwo:%@ fsixtyfour:%@ d:%@ s:%@>", self.class, (void *)self, @(self.eight), @(self.sixteen), @(self.thirtytwo), @(self.sixtyfour), @(self.fthirtytwo), @(self.fsixtyfour), self.d, self.s];
} }
@end @end
...@@ -2,12 +2,18 @@ package com.dropbox.djinni.test; ...@@ -2,12 +2,18 @@ package com.dropbox.djinni.test;
import junit.framework.TestCase; import junit.framework.TestCase;
import java.util.Date;
public class RecordWithDerivingsTest extends TestCase { public class RecordWithDerivingsTest extends TestCase {
private final RecordWithDerivings record1 = new RecordWithDerivings(1, "String1"); private final RecordWithDerivings record1 = new RecordWithDerivings((byte)1, (short)2, 3, 4, 5.0f, 6.0,
private final RecordWithDerivings record1A = new RecordWithDerivings(1, "String1"); new Date(7), "String8");
private final RecordWithDerivings record2 = new RecordWithDerivings(1, "String2"); private final RecordWithDerivings record1A = new RecordWithDerivings((byte)1, (short)2, 3, 4, 5.0f, 6.0,
private final RecordWithDerivings record3 = new RecordWithDerivings(2, "String1"); new Date(7), "String8");
private final RecordWithDerivings record2 = new RecordWithDerivings((byte)1, (short)2, 3, 4, 5.0f, 6.0,
new Date(7), "String888");
private final RecordWithDerivings record3 = new RecordWithDerivings((byte)111, (short)2, 3, 4, 5.0f, 6.0,
new Date(7), "String8");
public void testRecordOrd() { public void testRecordOrd() {
assertTrue(record1.compareTo(record1A) == 0); assertTrue(record1.compareTo(record1A) == 0);
......
...@@ -3,13 +3,22 @@ ...@@ -3,13 +3,22 @@
#import <XCTest/XCTest.h> #import <XCTest/XCTest.h>
using namespace testsuite; #import <chrono>
static RecordWithDerivings record1(1, "String1"); using namespace testsuite;
static RecordWithDerivings record1A(1, "String1");
static RecordWithDerivings record2(1, "String2");
static RecordWithDerivings record3(2, "String1");
static RecordWithDerivings record1(1, 2, 3, 4, 5.0f, 6.0,
std::chrono::system_clock::time_point(std::chrono::milliseconds(7)),
"String8");
static RecordWithDerivings record1A(1, 2, 3, 4, 5.0f, 6.0,
std::chrono::system_clock::time_point(std::chrono::milliseconds(7)),
"String8");
static RecordWithDerivings record2(1, 2, 3, 4, 5.0f, 6.0,
std::chrono::system_clock::time_point(std::chrono::milliseconds(7)),
"String8888");
static RecordWithDerivings record3(111, 2, 3, 4, 5.0f, 6.0,
std::chrono::system_clock::time_point(std::chrono::milliseconds(7)),
"String8");
static RecordWithNestedDerivings nestedRecord1(1, record1); static RecordWithNestedDerivings nestedRecord1(1, record1);
static RecordWithNestedDerivings nestedRecord1A(1, record1A); static RecordWithNestedDerivings nestedRecord1A(1, record1A);
static RecordWithNestedDerivings nestedRecord2(1, record2); static RecordWithNestedDerivings nestedRecord2(1, record2);
......
...@@ -3,10 +3,42 @@ ...@@ -3,10 +3,42 @@
#import <XCTest/XCTest.h> #import <XCTest/XCTest.h>
static DBRecordWithDerivings *record1 = [DBRecordWithDerivings recordWithDerivingsWithKey1:1 key2:@"String1"]; static DBRecordWithDerivings *record1 = [DBRecordWithDerivings
static DBRecordWithDerivings *record1A = [DBRecordWithDerivings recordWithDerivingsWithKey1:1 key2:@"String1"]; recordWithDerivingsWithEight:1
static DBRecordWithDerivings *record2 = [DBRecordWithDerivings recordWithDerivingsWithKey1:1 key2:@"String2"]; sixteen:2
static DBRecordWithDerivings *record3 = [DBRecordWithDerivings recordWithDerivingsWithKey1:2 key2:@"String1"]; thirtytwo:3
sixtyfour:4
fthirtytwo:5.0f
fsixtyfour:6.0
d:[NSDate dateWithTimeIntervalSince1970:7]
s:@"String8"];
static DBRecordWithDerivings *record1A = [DBRecordWithDerivings
recordWithDerivingsWithEight:1
sixteen:2
thirtytwo:3
sixtyfour:4
fthirtytwo:5.0f
fsixtyfour:6.0
d:[NSDate dateWithTimeIntervalSince1970:7]
s:@"String8"];
static DBRecordWithDerivings *record2 = [DBRecordWithDerivings
recordWithDerivingsWithEight:1
sixteen:2
thirtytwo:3
sixtyfour:4
fthirtytwo:5.0f
fsixtyfour:6.0
d:[NSDate dateWithTimeIntervalSince1970:7]
s:@"String888"];
static DBRecordWithDerivings *record3 = [DBRecordWithDerivings
recordWithDerivingsWithEight:111
sixteen:2
thirtytwo:3
sixtyfour:4
fthirtytwo:5.0f
fsixtyfour:6.0
d:[NSDate dateWithTimeIntervalSince1970:7]
s:@"String8"];
static DBRecordWithNestedDerivings *nestedRecord1 = [DBRecordWithNestedDerivings recordWithNestedDerivingsWithKey:1 rec:record1]; static DBRecordWithNestedDerivings *nestedRecord1 = [DBRecordWithNestedDerivings recordWithNestedDerivingsWithKey:1 rec:record1];
static DBRecordWithNestedDerivings *nestedRecord1A = [DBRecordWithNestedDerivings recordWithNestedDerivingsWithKey:1 rec:record1A]; static DBRecordWithNestedDerivings *nestedRecord1A = [DBRecordWithNestedDerivings recordWithNestedDerivingsWithKey:1 rec:record1A];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment