summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2015-07-09 00:57:38 -0300
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2015-08-29 16:40:10 -0300
commite0b12153c84446c0588e5f163a3e86f4cb75f620 (patch)
tree56ce6f84af9b838171071b54469d45293b9d9660
parentd71182af7a3c787b30cd6a5747b9d5f055fee003 (diff)
eina: Created EINA_VALUE_TYPE_OPTIONAL that is a single-element container that can be empty
Eina Value Optional can be used to create a eina value that can be set or be empty and can be embedded in a eina_value_struct.
-rw-r--r--src/lib/eina/eina_inline_value.x68
-rw-r--r--src/lib/eina/eina_value.c227
-rw-r--r--src/lib/eina/eina_value.h90
-rw-r--r--src/tests/eina/eina_test_value.c144
4 files changed, 529 insertions, 0 deletions
diff --git a/src/lib/eina/eina_inline_value.x b/src/lib/eina/eina_inline_value.x
index 9aff401168..a9ceea16ad 100644
--- a/src/lib/eina/eina_inline_value.x
+++ b/src/lib/eina/eina_inline_value.x
@@ -1652,6 +1652,74 @@ eina_value_struct_member_value_set(Eina_Value *dst, const Eina_Value_Struct_Memb
1652 1652
1653#undef EINA_VALUE_TYPE_STRUCT_CHECK_RETURN_VAL 1653#undef EINA_VALUE_TYPE_STRUCT_CHECK_RETURN_VAL
1654 1654
1655#define EINA_VALUE_TYPE_OPTIONAL_CHECK_RETURN_VAL(value, retval) \
1656 EINA_SAFETY_ON_NULL_RETURN_VAL(value, retval); \
1657 EINA_SAFETY_ON_FALSE_RETURN_VAL(value->type->setup == EINA_VALUE_TYPE_OPTIONAL->setup, retval)
1658
1659static inline Eina_Value*
1660eina_value_optional_empty_new()
1661{
1662 return eina_value_new(EINA_VALUE_TYPE_OPTIONAL);
1663}
1664
1665struct _Eina_Value_Optional_Outer
1666{
1667 Eina_Value_Type const* subtype;
1668 void* value;
1669};
1670typedef struct _Eina_Value_Optional_Outer Eina_Value_Optional_Outer;
1671
1672struct _Eina_Value_Optional_Inner
1673{
1674 Eina_Value_Type const* subtype;
1675 char value[];
1676};
1677typedef struct _Eina_Value_Optional_Inner Eina_Value_Optional_Inner;
1678
1679static inline Eina_Bool
1680eina_value_optional_empty_is(const Eina_Value *value, Eina_Bool *is_empty)
1681{
1682 EINA_VALUE_TYPE_OPTIONAL_CHECK_RETURN_VAL(value, EINA_FALSE);
1683 EINA_SAFETY_ON_NULL_RETURN_VAL(is_empty, EINA_FALSE);
1684
1685 void *mem = eina_value_memory_get(value);
1686 if (!mem)
1687 return EINA_FALSE;
1688 if(2*sizeof(void*) <= sizeof(Eina_Value_Union))
1689 {
1690 Eina_Value_Optional_Outer* opt = (Eina_Value_Optional_Outer*)mem;
1691 *is_empty = !opt->subtype;
1692 }
1693 else
1694 {
1695 *is_empty = ! *(void**)mem;
1696 }
1697 return EINA_TRUE;
1698}
1699
1700static inline const Eina_Value_Type *
1701eina_value_optional_type_get(Eina_Value *value)
1702{
1703 EINA_VALUE_TYPE_OPTIONAL_CHECK_RETURN_VAL(value, (const Eina_Value_Type *)NULL);
1704
1705 void *mem = eina_value_memory_get(value);
1706 if (!mem)
1707 return EINA_FALSE;
1708
1709 if(2*sizeof(void*) <= sizeof(Eina_Value_Union))
1710 {
1711 Eina_Value_Optional_Outer* opt = (Eina_Value_Optional_Outer*)mem;
1712 return opt->subtype;
1713 }
1714 else
1715 {
1716 Eina_Value_Optional_Inner* opt = *(Eina_Value_Optional_Inner**)mem;
1717 if(!opt)
1718 return NULL;
1719 return opt->subtype;
1720 }
1721}
1722#undef EINA_VALUE_TYPE_OPTIONAL_CHECK_RETURN_VAL
1655 1723
1656static inline Eina_Bool 1724static inline Eina_Bool
1657eina_value_type_setup(const Eina_Value_Type *type, void *mem) 1725eina_value_type_setup(const Eina_Value_Type *type, void *mem)
diff --git a/src/lib/eina/eina_value.c b/src/lib/eina/eina_value.c
index 824f11d7bd..8b45856cb9 100644
--- a/src/lib/eina/eina_value.c
+++ b/src/lib/eina/eina_value.c
@@ -4349,6 +4349,229 @@ static const Eina_Value_Type _EINA_VALUE_TYPE_STRUCT = {
4349 _eina_value_type_struct_pget 4349 _eina_value_type_struct_pget
4350}; 4350};
4351 4351
4352static Eina_Bool
4353_eina_value_type_optional_setup(const Eina_Value_Type *type EINA_UNUSED, void *mem)
4354{
4355 memset(mem, 0, type->value_size);
4356 return EINA_TRUE;
4357}
4358
4359static Eina_Bool
4360_eina_value_type_optional_flush(const Eina_Value_Type *type EINA_UNUSED, void *mem EINA_UNUSED)
4361{
4362 if(sizeof(Eina_Value_Optional_Outer) <= sizeof(Eina_Value_Union))
4363 {
4364 Eina_Value_Optional_Outer* opt = mem;
4365 if(opt->subtype)
4366 {
4367 if(!eina_value_type_flush(opt->subtype, opt->value))
4368 return EINA_FALSE;
4369 free(opt->value);
4370 memset(mem, 0, sizeof(Eina_Value_Optional_Outer));
4371 }
4372 }
4373 else
4374 {
4375 Eina_Value_Optional_Inner* opt = *(void**)mem;
4376 if(opt)
4377 {
4378 if(!eina_value_type_flush(opt->subtype, opt->value))
4379 return EINA_FALSE;
4380 free(*(void**)mem);
4381 *(void**)mem = NULL;
4382 }
4383 }
4384 return EINA_TRUE;
4385}
4386
4387EAPI Eina_Bool
4388eina_value_optional_reset(Eina_Value *value)
4389{
4390 void *mem = eina_value_memory_get(value);
4391 if (!mem)
4392 return EINA_FALSE;
4393 return _eina_value_type_optional_flush(EINA_VALUE_TYPE_OPTIONAL, mem);
4394}
4395
4396EAPI Eina_Value*
4397eina_value_optional_new(Eina_Value_Type const *subtype,
4398 const void* initial_value) EINA_ARG_NONNULL(1, 2)
4399{
4400 Eina_Value *value;
4401
4402 value = eina_mempool_malloc(_eina_value_mp, sizeof(Eina_Value));
4403 if (!value)
4404 return NULL;
4405
4406 if (!eina_value_setup(value, EINA_VALUE_TYPE_OPTIONAL))
4407 {
4408 eina_mempool_free(_eina_value_mp, value);
4409 return EINA_FALSE;
4410 }
4411 if (!eina_value_optional_pset(value, subtype, initial_value))
4412 {
4413 eina_mempool_free(_eina_value_mp, value);
4414 return EINA_FALSE;
4415 }
4416 return value;
4417}
4418
4419EAPI Eina_Bool
4420eina_value_optional_pset(Eina_Value *value,
4421 Eina_Value_Type const* subtype,
4422 const void *subvalue) EINA_ARG_NONNULL(1, 2, 3)
4423{
4424 eina_value_optional_reset(value);
4425
4426 if(sizeof(Eina_Value_Optional_Outer) <= sizeof(Eina_Value_Union))
4427 {
4428 Eina_Value_Optional_Outer outer;
4429 outer.subtype = subtype;
4430 outer.value = malloc(subtype->value_size);
4431 eina_value_type_setup(subtype, outer.value);
4432 eina_value_type_pset(subtype, outer.value, subvalue);
4433 if (!eina_value_pset(value, &outer))
4434 {
4435 return EINA_FALSE;
4436 }
4437 }
4438 else
4439 {
4440 Eina_Value_Optional_Inner *inner =
4441 malloc(sizeof(Eina_Value_Optional_Inner) + subtype->value_size);
4442 inner->subtype = subtype;
4443 eina_value_type_setup(subtype, inner->value);
4444 eina_value_type_pset(subtype, inner->value, subvalue);
4445 if (!eina_value_pset(value, &inner))
4446 return EINA_FALSE;
4447 }
4448
4449 return EINA_TRUE;
4450}
4451
4452EAPI Eina_Bool
4453eina_value_optional_pget(Eina_Value *value, void *subvalue) EINA_ARG_NONNULL(1, 2, 3)
4454{
4455 if(sizeof(Eina_Value_Optional_Outer) <= sizeof(Eina_Value_Union))
4456 {
4457 Eina_Value_Optional_Outer outer;
4458
4459 if (!eina_value_pget(value, &outer))
4460 return EINA_FALSE;
4461
4462 if(outer.subtype)
4463 eina_value_type_copy(outer.subtype, outer.value, subvalue);
4464 else
4465 return EINA_FALSE;
4466 }
4467 else
4468 {
4469 Eina_Value_Optional_Inner *inner;
4470
4471 if (!eina_value_pget(value, &inner))
4472 return EINA_FALSE;
4473
4474 if(inner)
4475 eina_value_type_copy(inner->subtype, inner->value, subvalue);
4476 else
4477 return EINA_FALSE;
4478 }
4479
4480 return EINA_TRUE;
4481}
4482
4483static Eina_Bool
4484_eina_value_type_optional_copy(const Eina_Value_Type *type EINA_UNUSED, const void *src_raw, void *dst_raw)
4485{
4486 if(sizeof(Eina_Value_Optional_Outer) <= sizeof(Eina_Value_Union))
4487 {
4488 Eina_Value_Optional_Outer const* src = src_raw;
4489 Eina_Value_Optional_Outer* dst = dst_raw;
4490 if(src->subtype)
4491 {
4492 dst->subtype = src->subtype;
4493 dst->value = malloc(src->subtype->value_size);
4494 eina_value_type_copy(src->subtype, src->value, dst->value);
4495 }
4496 else
4497 memset(dst_raw, 0, sizeof(Eina_Value_Optional_Outer));
4498 }
4499 else if(src_raw)
4500 {
4501 Eina_Value_Optional_Inner* src = *(void**)src_raw;
4502 Eina_Value_Optional_Inner* dst = *(void**)dst_raw
4503 = malloc(sizeof(Eina_Value_Optional_Inner) + src->subtype->value_size);
4504 dst->subtype = src->subtype;
4505
4506 eina_value_type_copy(src->subtype, src->value, dst->value);
4507 }
4508 else
4509 *(void**)dst_raw = NULL;
4510 return EINA_TRUE;
4511}
4512
4513static int
4514_eina_value_type_optional_compare(const Eina_Value_Type *type EINA_UNUSED, const void *lhs_raw, const void *rhs_raw)
4515{
4516 if(sizeof(Eina_Value_Optional_Outer) <= sizeof(Eina_Value_Union))
4517 {
4518 Eina_Value_Optional_Outer const *lhs = lhs_raw
4519 , *rhs = rhs_raw;
4520 if(!lhs->subtype)
4521 return !rhs->subtype ? 0 : -1;
4522 else if(!rhs->subtype)
4523 return 1;
4524 else if(lhs->subtype != rhs->subtype)
4525 return lhs->subtype < rhs->subtype ? -1 : 1;
4526 else
4527 return eina_value_type_compare(lhs->subtype, lhs->value, rhs->value);
4528 }
4529 else
4530 {
4531 Eina_Value_Optional_Inner const * const* lhs_p = lhs_raw;
4532 Eina_Value_Optional_Inner const * const* rhs_p = rhs_raw;
4533
4534 if(!*lhs_p)
4535 return *rhs_p ? -1 : 0;
4536 else if(!*rhs_p)
4537 return 1;
4538 else if((*lhs_p)->subtype != (*rhs_p)->subtype)
4539 return (*lhs_p)->subtype < (*rhs_p)->subtype ? -1 : 1;
4540 else
4541 return eina_value_type_compare((*lhs_p)->subtype, (*lhs_p)->value, (*rhs_p)->value);
4542 }
4543}
4544
4545static Eina_Bool
4546_eina_value_type_optional_pset(const Eina_Value_Type *type EINA_UNUSED, void *mem, const void *ptr)
4547{
4548 _eina_value_type_optional_flush(type, mem);
4549 _eina_value_type_optional_copy(type, ptr, mem);
4550 return EINA_TRUE;
4551}
4552
4553static Eina_Bool
4554_eina_value_type_optional_pget(const Eina_Value_Type *type EINA_UNUSED, const void *mem, void *ptr)
4555{
4556 memcpy(ptr, mem, sizeof(Eina_Value_Union));
4557 return EINA_TRUE;
4558}
4559
4560EAPI const Eina_Value_Type _EINA_VALUE_TYPE_OPTIONAL = {
4561 EINA_VALUE_TYPE_VERSION,
4562 sizeof(Eina_Value_Union),
4563 "optional",
4564 _eina_value_type_optional_setup,
4565 _eina_value_type_optional_flush,
4566 _eina_value_type_optional_copy,
4567 _eina_value_type_optional_compare,
4568 NULL,
4569 NULL,
4570 NULL,
4571 _eina_value_type_optional_pset,
4572 _eina_value_type_optional_pget
4573};
4574
4352/* no model for now 4575/* no model for now
4353static Eina_Bool 4576static Eina_Bool
4354_eina_value_type_model_setup(const Eina_Value_Type *type EINA_UNUSED, void *mem) 4577_eina_value_type_model_setup(const Eina_Value_Type *type EINA_UNUSED, void *mem)
@@ -4911,6 +5134,8 @@ eina_value_init(void)
4911 EINA_VALUE_TYPE_MODEL = &_EINA_VALUE_TYPE_MODEL; 5134 EINA_VALUE_TYPE_MODEL = &_EINA_VALUE_TYPE_MODEL;
4912 */ 5135 */
4913 5136
5137 EINA_VALUE_TYPE_OPTIONAL = &_EINA_VALUE_TYPE_OPTIONAL;
5138
4914 EINA_VALUE_BLOB_OPERATIONS_MALLOC = &_EINA_VALUE_BLOB_OPERATIONS_MALLOC; 5139 EINA_VALUE_BLOB_OPERATIONS_MALLOC = &_EINA_VALUE_BLOB_OPERATIONS_MALLOC;
4915 5140
4916 EINA_VALUE_STRUCT_OPERATIONS_BINSEARCH = &_EINA_VALUE_STRUCT_OPERATIONS_BINSEARCH; 5141 EINA_VALUE_STRUCT_OPERATIONS_BINSEARCH = &_EINA_VALUE_STRUCT_OPERATIONS_BINSEARCH;
@@ -4995,6 +5220,8 @@ EAPI const Eina_Value_Type *EINA_VALUE_TYPE_STRUCT = NULL;
4995/* no model for now 5220/* no model for now
4996EAPI const Eina_Value_Type *EINA_VALUE_TYPE_MODEL = NULL; 5221EAPI const Eina_Value_Type *EINA_VALUE_TYPE_MODEL = NULL;
4997 */ 5222 */
5223EAPI const Eina_Value_Type *EINA_VALUE_TYPE_OPTIONAL = NULL;
5224
4998 5225
4999EAPI const Eina_Value_Blob_Operations *EINA_VALUE_BLOB_OPERATIONS_MALLOC = NULL; 5226EAPI const Eina_Value_Blob_Operations *EINA_VALUE_BLOB_OPERATIONS_MALLOC = NULL;
5000 5227
diff --git a/src/lib/eina/eina_value.h b/src/lib/eina/eina_value.h
index a64d3b4dd8..b273673df5 100644
--- a/src/lib/eina/eina_value.h
+++ b/src/lib/eina/eina_value.h
@@ -3482,6 +3482,96 @@ static inline Eina_Bool eina_value_type_pget(const Eina_Value_Type *type, const
3482 * @} 3482 * @}
3483 */ 3483 */
3484 3484
3485/**
3486 * @defgroup Eina_Value_Optional_Group Generic Value Optional management
3487 *
3488 * @{
3489 */
3490
3491/**
3492 * @var EINA_VALUE_TYPE_OPTIONAL
3493 * manages optional type.
3494 *
3495 * @since 1.16
3496 */
3497EAPI extern const Eina_Value_Type *EINA_VALUE_TYPE_OPTIONAL;
3498
3499/**
3500 * @typedef Eina_Value_Optional type to be used with Eina_Value_Struct
3501 *
3502 * @since 1.16
3503 */
3504typedef Eina_Value_Union Eina_Value_Optional;
3505
3506/**
3507 * @brief Create an empty optional. This is the same as eina_value_new(EINA_VALUE_TYPE_OPTIONAL).
3508 * @return returns an empty optional eina value.
3509 */
3510static inline Eina_Value *eina_value_optional_empty_new();
3511
3512/**
3513 * @brief Create an optional eina value with the passed value
3514 * @param subtype Eina_Value_Type of parameter value
3515 * @param value The value to be used to construct optional eina value
3516 * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
3517 * @since 1.16
3518 */
3519EAPI Eina_Value *eina_value_optional_new(const Eina_Value_Type *subtype,
3520 const void* value) EINA_ARG_NONNULL(1, 2);
3521
3522/**
3523 * @brief Function to know if an eina optional is empty or not
3524 * @param value Eina Value Optional
3525 * @param is_empty #EINA_TRUE if optional is empty, #EINA_FALSE otherwise.
3526 * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
3527 * @since 1.16
3528 */
3529static inline Eina_Bool eina_value_optional_empty_is(const Eina_Value *value,
3530 Eina_Bool *is_empty) EINA_ARG_NONNULL(1, 2);
3531
3532/**
3533 * @brief Set the optional with a value
3534 * @param value Eina Value Optional to be set with subvalue
3535 * @param subtype Type of subvalue
3536 * @param subvalue Value to be set in optional
3537 * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
3538 * @since 1.16
3539 */
3540EAPI Eina_Bool eina_value_optional_pset(Eina_Value *value,
3541 Eina_Value_Type const* subtype,
3542 const void *subvalue) EINA_ARG_NONNULL(1, 2, 3);
3543
3544/**
3545 * @brief Get the value from an optional
3546 * @param value Eina Value Optional to get value from
3547 * @param subvalue Pointer to where value is to be copied to. You must use
3548 * the correct type according to eina_value_optional_type_get
3549 * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
3550 * @since 1.16
3551 */
3552EAPI Eina_Bool eina_value_optional_pget(Eina_Value *value,
3553 void *subvalue) EINA_ARG_NONNULL(1, 2);
3554
3555/**
3556 * @brief Resets eina optional to empty
3557 * @param value Eina Value Optional
3558 * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
3559 * @since 1.16
3560 */
3561EAPI Eina_Bool eina_value_optional_reset(Eina_Value *value) EINA_ARG_NONNULL(1);
3562
3563/**
3564 * @brief Get type from value that is stored on Eina Value Optional
3565 * @param value Eina Value Optional
3566 * @return The optional sub-type.
3567 * @since 1.16
3568 */
3569static inline const Eina_Value_Type *eina_value_optional_type_get(Eina_Value *value) EINA_ARG_NONNULL(1);
3570
3571/**
3572 * @}
3573 */
3574
3485#include "eina_inline_value.x" 3575#include "eina_inline_value.x"
3486 3576
3487/** 3577/**
diff --git a/src/tests/eina/eina_test_value.c b/src/tests/eina/eina_test_value.c
index 454aa6011d..9e1b8081eb 100644
--- a/src/tests/eina/eina_test_value.c
+++ b/src/tests/eina/eina_test_value.c
@@ -2731,6 +2731,147 @@ START_TEST(eina_value_test_array_of_struct)
2731} 2731}
2732END_TEST 2732END_TEST
2733 2733
2734START_TEST(eina_value_test_optional_int)
2735{
2736 eina_init();
2737
2738 /* Eina_Value *value = eina_value_new(EINA_VALUE_TYPE_OPTIONAL); */
2739 /* Eina_Bool is_empty; */
2740 /* ck_assert(eina_value_optional_empty_is(value, &is_empty)); */
2741 /* ck_assert(is_empty); */
2742
2743 /* // sets expectation */
2744 /* int expected_value = -12345; */
2745 /* ck_assert(eina_value_optional_pset(value, EINA_VALUE_TYPE_INT, &expected_value)); */
2746 /* ck_assert(eina_value_optional_empty_is(value, &is_empty)); */
2747 /* ck_assert(!is_empty); */
2748
2749 /* // gets the actual value */
2750 /* int actual_value; */
2751 /* ck_assert(eina_value_optional_pget(value, &actual_value)); */
2752 /* ck_assert_int_eq(expected_value, actual_value); */
2753
2754 /* // resets the optional */
2755 /* ck_assert(eina_value_optional_reset(value)); */
2756 /* ck_assert(eina_value_optional_empty_is(value, &is_empty)); */
2757 /* ck_assert(is_empty); */
2758
2759 /* // Sets expectation again after reset */
2760 /* expected_value = -54321; */
2761 /* ck_assert(eina_value_optional_pset(value, EINA_VALUE_TYPE_INT, &expected_value)); */
2762 /* ck_assert(eina_value_optional_empty_is(value, &is_empty)); */
2763 /* ck_assert(!is_empty); */
2764
2765 /* // gets the actual value */
2766 /* ck_assert(eina_value_optional_pget(value, &actual_value)); */
2767 /* ck_assert_int_eq(expected_value, actual_value); */
2768
2769 /* eina_value_free(value); */
2770 eina_shutdown();
2771}
2772END_TEST
2773
2774START_TEST(eina_value_test_optional_string)
2775{
2776 eina_init();
2777
2778 Eina_Value *value = eina_value_new(EINA_VALUE_TYPE_OPTIONAL);
2779 Eina_Bool is_empty;
2780 ck_assert(eina_value_optional_empty_is(value, &is_empty));
2781 ck_assert(is_empty);
2782 ck_assert(EINA_VALUE_TYPE_OPTIONAL);
2783
2784 // sets expectation
2785 const char *expected_value = "Hello world!";
2786 ck_assert(eina_value_optional_pset(value, EINA_VALUE_TYPE_STRING, &expected_value));
2787 ck_assert(eina_value_optional_empty_is(value, &is_empty));
2788 ck_assert(!is_empty);
2789
2790 // gets the actual value
2791 const char *actual_value;
2792 ck_assert(eina_value_optional_pget(value, &actual_value));
2793 ck_assert_str_eq(expected_value, actual_value);
2794
2795 // resets the optional
2796 ck_assert(eina_value_optional_reset(value));
2797 ck_assert(eina_value_optional_empty_is(value, &is_empty));
2798 ck_assert(is_empty);
2799
2800 // Sets expectation again after reset
2801 expected_value = "!dlrow olleH";
2802 ck_assert(eina_value_optional_pset(value, EINA_VALUE_TYPE_STRING, &expected_value));
2803 ck_assert(eina_value_optional_empty_is(value, &is_empty));
2804 ck_assert(!is_empty);
2805
2806 // gets the actual value
2807 ck_assert(eina_value_optional_pget(value, &actual_value));
2808 ck_assert_str_eq(expected_value, actual_value);
2809
2810 eina_value_free(value);
2811 eina_shutdown();
2812}
2813END_TEST
2814
2815START_TEST(eina_value_test_optional_struct_members)
2816{
2817 eina_init();
2818
2819 struct s {
2820 int64_t a;
2821 Eina_Value_Optional text;
2822 int64_t b;
2823 };
2824 const Eina_Value_Struct_Member members[] = {
2825 EINA_VALUE_STRUCT_MEMBER(EINA_VALUE_TYPE_INT64, struct s, a),
2826 EINA_VALUE_STRUCT_MEMBER(EINA_VALUE_TYPE_OPTIONAL, struct s, text),
2827 EINA_VALUE_STRUCT_MEMBER(EINA_VALUE_TYPE_INT64, struct s, b),
2828 EINA_VALUE_STRUCT_MEMBER_SENTINEL
2829 };
2830 const Eina_Value_Struct_Desc desc = {
2831 EINA_VALUE_STRUCT_DESC_VERSION,
2832 NULL, members, 3, sizeof(struct s)
2833 };
2834
2835 Eina_Value *value = eina_value_struct_new(&desc);
2836 ck_assert_ptr_ne(NULL, value);
2837
2838 int64_t expected_a = 0x1234567887654321ll;
2839 fail_unless(eina_value_struct_set(value, "a", expected_a));
2840 int64_t actual_a;
2841 fail_unless(eina_value_struct_get(value, "a", &actual_a));
2842 ck_assert_int_eq(expected_a, actual_a);
2843
2844 int64_t expected_b = 0xEEDCBA9889ABCDEFll;
2845 fail_unless(eina_value_struct_set(value, "b", expected_b));
2846 int64_t actual_b;
2847 fail_unless(eina_value_struct_get(value, "b", &actual_b));
2848 ck_assert_int_eq(expected_b, actual_b);
2849
2850 Eina_Value expected_value;
2851 fail_unless(eina_value_setup(&expected_value, EINA_VALUE_TYPE_OPTIONAL));
2852 const char* str = "Hello world!";
2853 fail_unless(eina_value_optional_pset(&expected_value, EINA_VALUE_TYPE_STRING, &str));
2854 fail_unless(eina_value_struct_value_set(value, "text", &expected_value));
2855
2856 Eina_Value actual_value;
2857 fail_unless(eina_value_struct_value_get(value, "text", &actual_value));
2858 fail_unless(eina_value_compare(&expected_value, &actual_value) == 0);
2859
2860 // tests if the value have been overriden
2861 fail_unless(eina_value_struct_get(value, "a", &actual_a));
2862 ck_assert_int_eq(expected_a, actual_a);
2863 fail_unless(eina_value_struct_get(value, "b", &actual_b));
2864 ck_assert_int_eq(expected_b, actual_b);
2865
2866 eina_value_flush(&actual_value);
2867 eina_value_flush(&expected_value);
2868
2869 eina_value_free(value);
2870
2871 eina_shutdown();
2872}
2873END_TEST
2874
2734#if 0 2875#if 0
2735START_TEST(eina_value_test_model) 2876START_TEST(eina_value_test_model)
2736{ 2877{
@@ -2801,6 +2942,9 @@ eina_test_value(TCase *tc)
2801 tcase_add_test(tc, eina_value_test_blob); 2942 tcase_add_test(tc, eina_value_test_blob);
2802 tcase_add_test(tc, eina_value_test_struct); 2943 tcase_add_test(tc, eina_value_test_struct);
2803 tcase_add_test(tc, eina_value_test_array_of_struct); 2944 tcase_add_test(tc, eina_value_test_array_of_struct);
2945 tcase_add_test(tc, eina_value_test_optional_int);
2946 tcase_add_test(tc, eina_value_test_optional_string);
2947 tcase_add_test(tc, eina_value_test_optional_struct_members);
2804#if 0 2948#if 0
2805 tcase_add_test(tc, eina_value_test_model); 2949 tcase_add_test(tc, eina_value_test_model);
2806#endif 2950#endif