-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GEODE-8562: Adds new C++ test for using a class as a key (#714)
* Add new ClassAsKey test * Use new classId for PositionKey (resolves conflict with existing classId) * Make hashcode matche Java side hashcode * Convert to C++11 style constructors (use explicit and default) * Remove unneccessary headers * Add explicit to constructor. * Don't use c-style casts * Improved variable names Co-authored-by: Jacob Barrett <[email protected]>
- Loading branch information
1 parent
59cd01d
commit 5c65fc7
Showing
9 changed files
with
341 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include "Position.hpp" | ||
|
||
#include <geode/DataInput.hpp> | ||
#include <geode/DataOutput.hpp> | ||
|
||
namespace DataSerializableTest { | ||
|
||
int32_t Position::count = 0; | ||
|
||
Position::Position() | ||
: volumeAverageOver20Days_(0), | ||
conversionRatio_(0.0), | ||
valueGain_(0.0), | ||
industry_(0), | ||
issuer_(0), | ||
marketValue_(0.0), | ||
quantity_(0.0), | ||
sharesOutstanding_(0), | ||
volatility_(0), | ||
positionId_(0) {} | ||
|
||
Position::Position(std::string id, int32_t outstandingShares) : Position() { | ||
securityId_ = std::move(id); | ||
securityType_ = "a"; | ||
sharesOutstanding_ = outstandingShares; | ||
quantity_ = outstandingShares - (count % 2 == 0 ? 1000 : 100); | ||
marketValue_ = quantity_ * 1.2345998; | ||
positionId_ = count++; | ||
} | ||
|
||
void Position::toData(apache::geode::client::DataOutput& output) const { | ||
output.writeInt(volumeAverageOver20Days_); | ||
output.writeString(bondRating_); | ||
output.writeDouble(conversionRatio_); | ||
output.writeString(country_); | ||
output.writeDouble(valueGain_); | ||
output.writeInt(industry_); | ||
output.writeInt(issuer_); | ||
output.writeDouble(marketValue_); | ||
output.writeDouble(quantity_); | ||
output.writeString(securityId_); | ||
output.writeString(securityLinks_); | ||
output.writeUTF(securityType_); | ||
output.writeInt(sharesOutstanding_); | ||
output.writeString(underlyingSecurity_); | ||
output.writeInt(volatility_); | ||
output.writeInt(positionId_); | ||
} | ||
|
||
void Position::fromData(apache::geode::client::DataInput& input) { | ||
volumeAverageOver20Days_ = input.readInt64(); | ||
bondRating_ = input.readString(); | ||
conversionRatio_ = input.readDouble(); | ||
country_ = input.readString(); | ||
valueGain_ = input.readDouble(); | ||
industry_ = input.readInt64(); | ||
issuer_ = input.readInt64(); | ||
marketValue_ = input.readDouble(); | ||
quantity_ = input.readDouble(); | ||
securityId_ = input.readString(); | ||
securityLinks_ = input.readString(); | ||
securityType_ = input.readUTF(); | ||
sharesOutstanding_ = input.readInt32(); | ||
underlyingSecurity_ = input.readString(); | ||
volatility_ = input.readInt64(); | ||
positionId_ = input.readInt32(); | ||
} | ||
|
||
} // namespace DataSerializableTest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#ifndef POSITION_H | ||
#define POSITION_H | ||
|
||
/* | ||
* @brief User class for testing the put functionality for object. | ||
*/ | ||
|
||
#include <string> | ||
|
||
#include <geode/CacheableString.hpp> | ||
#include <geode/DataSerializable.hpp> | ||
|
||
namespace DataSerializableTest { | ||
|
||
using apache::geode::client::CacheableString; | ||
using apache::geode::client::DataInput; | ||
using apache::geode::client::DataOutput; | ||
using apache::geode::client::DataSerializable; | ||
|
||
class Position : public DataSerializable { | ||
private: | ||
int64_t volumeAverageOver20Days_; | ||
std::string bondRating_; | ||
double conversionRatio_; | ||
std::string country_; | ||
double valueGain_; | ||
int64_t industry_; | ||
int64_t issuer_; | ||
double marketValue_; | ||
double quantity_; | ||
std::string securityId_; | ||
std::string securityLinks_; | ||
std::string securityType_; | ||
int32_t sharesOutstanding_; | ||
std::string underlyingSecurity_; | ||
int64_t volatility_; | ||
int32_t positionId_; | ||
|
||
public: | ||
static int32_t count; | ||
|
||
Position(); | ||
explicit Position(std::string id, int32_t out); | ||
~Position() override = default; | ||
void toData(DataOutput& output) const override; | ||
void fromData(DataInput& input) override; | ||
|
||
static void resetCounter() { count = 0; } | ||
std::string getSecurityId() { return securityId_; } | ||
int32_t getPOsitionId() { return positionId_; } | ||
int32_t getSharesOutstanding() { return sharesOutstanding_; } | ||
static std::shared_ptr<Serializable> createDeserializable() { | ||
return std::make_shared<Position>(); | ||
} | ||
}; | ||
|
||
} // namespace DataSerializableTest | ||
|
||
#endif // POSITION_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include "PositionKey.hpp" | ||
|
||
#include <geode/DataInput.hpp> | ||
#include <geode/DataOutput.hpp> | ||
|
||
namespace DataSerializableTest { | ||
|
||
void PositionKey::toData(DataOutput& output) const { | ||
output.writeInt(positionId_); | ||
} | ||
|
||
void PositionKey::fromData(apache::geode::client::DataInput& input) { | ||
positionId_ = input.readInt64(); | ||
} | ||
|
||
bool PositionKey::operator==(const CacheableKey& other) const { | ||
return positionId_ == | ||
(static_cast<const PositionKey&>(other)).getPositionId(); | ||
} | ||
|
||
int PositionKey::hashcode() const { | ||
int prime = 31; | ||
int result = prime * static_cast<int32_t>(positionId_); | ||
return result; | ||
} | ||
|
||
} // namespace DataSerializableTest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#ifndef POSITIONKEY_H_ | ||
#define POSITIONKEY_H_ | ||
|
||
#include <string> | ||
|
||
#include <geode/CacheableString.hpp> | ||
#include <geode/DataSerializable.hpp> | ||
|
||
namespace DataSerializableTest { | ||
|
||
using apache::geode::client::CacheableKey; | ||
using apache::geode::client::DataInput; | ||
using apache::geode::client::DataOutput; | ||
using apache::geode::client::DataSerializable; | ||
|
||
class PositionKey : public DataSerializable, public CacheableKey { | ||
private: | ||
int64_t positionId_; | ||
|
||
public: | ||
PositionKey() = default; | ||
explicit PositionKey(int64_t positionId) : positionId_(positionId) {} | ||
~PositionKey() override = default; | ||
|
||
bool operator==(const CacheableKey& other) const override; | ||
int32_t hashcode() const override; | ||
|
||
void toData(DataOutput& output) const override; | ||
void fromData(DataInput& input) override; | ||
|
||
int64_t getPositionId() const { return positionId_; } | ||
static std::shared_ptr<Serializable> createDeserializable() { | ||
return std::make_shared<PositionKey>(); | ||
} | ||
}; | ||
|
||
} // namespace DataSerializableTest | ||
|
||
#endif // POSITIONKEY_H_ |
Oops, something went wrong.